I'm working on one ( a sample that is ). My favorite topic, sending SMTP email. I find that you can't directly import most .Net mail libraries. Instead, I wrote a wrapper class and then imported that. Microsoft has deprecated their own SmtpClient class in favor of an open source package (MailKit), so I used that.
C# Code looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit;
namespace AssemblyDemo
{
public class MailClient
{
public string userEmail
{ private get; set; }
public string password
{ private get; set; }
public string userName
{ private get; set; }
public string subject
{ private get; set; }
public string body
{ private get; set; }
private MailRecipient to;
public int setRecipient( string name, string address )
{
to = new MailRecipient();
this.to.name = name;
this.to.address = address;
return 1;
}
public int sendMail()
{
var message = new MimeMessage();
message.From.Add(new MailboxAddress(userName, userEmail));
message.To.Add(new MailboxAddress(to.name, to.address));
message.Subject = subject;
message.Body = new TextPart("plain") { Text = body };
using (var client = new SmtpClient())
{
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
client.Connect("smtp.gmail.com", 465, SecureSocketOptions.SslOnConnect);
client.Authenticate(userEmail,password);
client.Send(message);
client.Disconnect(true);
}
return 1;
}
}
public class MailRecipient
{
public string address
{ get; set; }
public string name
{ get; set; }
}
}
I run the .Net Importer on that and get a nvo_mailclient class. I chose the options in the Advanced section to encapsulate each DotNetAssemblyClass in a DotNetObject and to incorporate try catch error handling in the DotNetObject.
I declare the nvo_mailclient as an instance variable on the window, because I want to reference it in the error handling:
nvo_mailclient mailclient
In the open event of the window, I instantiate the nvo and then call the of_seterrorhandler to point to the window:
mailclient = create nvo_mailclient
mailclient.of_seterrorhandler( this, 'assemblyerror' )
In the close event I destroy the instance variable (not shown). In the custom 'assemblyerror' event, I display any exceptions raised by the assembly to the user.
MessageBox ( "Error", mailclient.is_errortext )
I have a datawindow on the window that has tentry columns for the user email, user name, recipient email, recipient name, user password, mail subject and mail body. Then I have a function that reads the values and interacts with the assembly through the nvo:
mailclient.of_set_body( dw_1.getItemString ( 1, "messagebody"))
mailclient.of_set_password( dw_1.getItemString( 1, "password"))
mailclient.of_set_subject( dw_1.getItemString ( 1, "subject" ))
mailclient.of_set_useremail( dw_1.getItemString ( 1, "fromaddress"))
mailclient.of_set_username( dw_1.getItemString( 1, "fromname" ))
mailclient.of_setrecipient( dw_1.getItemString( 1, "toname"), dw_1.getItemString( 1, "toaddress" ))
SetPointer ( HourGlass! )
mailclient.of_sendmail( )
This particular example was designed to use Google mail, hence the username/password authentication and SSl.
In order to use this in a deployed application you would need to distribute the wrapper assembly and all the assemblies that it references. That would include MailKit.dll, MimeKit.dll, System.Text.Encoding.Codepages.dll and BouncyCastle.Crypto.dll.
What's nice about this approach is that all you need to do is deploy them all in the directory that your application is in. No more dealing with the GAC, REGASM, registry entries, manifest files, etc. You also don't have to deal with the late binding issues involved with COM Callable Wrappers.
It should be done soon... maybe a week or so. Please check back.