This article describes how to create and send email message using Mail.dll .NET email library.
As a prerequisite you need to add reference to Mail.dll to your project. Please check MSDN how to add reference article for details.
When your reference is added you need to import appropriate namespaces both for email handling and SMTP component:
// C#
using Limilabs.Mail;
using Limilabs.Mail.Headers;
using Limilabs.Client.SMTP;
' VB
Imports Limilabs.Mail
Imports Limilabs.Mail.Headers
Imports Limilabs.Client.SMTP
First thing you need to do is to create email message. You can use MailBuilder class for that. We’ll add add from and to addresses, set subject and plain text email content. We could also set Html property to create HTML email. Mail.dll automatically generates Message-ID and Date headers.
// C#
MailBuilder builder = new MailBuilder();
builder.From.Add(new MailBox("alice@mail.com", "Alice"));
builder.To.Add(new MailBox("bob@mail.com", "Bob"));
builder.Subject = "Test";
builder.Text = "This is plain text message.";
IMail email = builder.Create();
' VB
Dim builder As New MailBuilder()
builder.From.Add(New MailBox("alice@mail.com", "Alice"))
builder.[To].Add(New MailBox("bob@mail.com", "Bob"))
builder.Subject = "Test"
builder.Text = "This is plain text message."
Next step is to connect to your SMTP server and send your message. Use Connect(string host) method to establish connection. Typically SMTP server is working on port 587. You can use Connect(string host, int port) overload when you need to specify different port (for example 25). If your server requires SSL use ConnectSSL method (Here you can find more info on using SMTP with SSL).
// C#
using (Smtp smtp = new Smtp())
{
smtp.Connect("server.example.com"); // or ConnectSSL for SSL
smtp.UseBestLogin("user", "password"); // remove if authentication is not needed
ISendMessageResult result = smtp.SendMessage(email);
if (result.Status == SendMessageStatus.Success)
{
// Message was sent.
}
smtp.Close();
}
' VB
Using smtp As New Smtp()
smtp.Connect("server.example.com") ' or ConnectSSL for SSL
smtp.UseBestLogin("user", "password") ' remove if authentication is not needed
Dim result As ISendMessageResult = smtp.SendMessage(email)
If result.Status = SendMessageStatus.Success Then
' Message was sent.
End If
smtp.Close()
End Using
Here are the full samples:
// C# version
using Limilabs.Mail;
using Limilabs.Mail.Headers;
using Limilabs.Client.SMTP;
class Program
{
static void Main(string[] args)
{
// Use builder class to create new email
MailBuilder builder = new MailBuilder();
builder.From.Add(new MailBox("alice@mail.com", "Alice"));
builder.To.Add(new MailBox("bob@mail.com", "Bob"));
builder.Subject = "Test";
builder.Text = "This is plain text message.";
IMail email = builder.Create();
// Send the message
using (Smtp smtp = new Smtp())
{
smtp.Connect("server.example.com"); // or ConnectSSL for SSL
smtp.UseBestLogin("user", "password"); // remove if not needed
ISendMessageResult result = smtp.SendMessage(email);
if (result.Status == SendMessageStatus.Success)
{
// Message was sent.
}
smtp.Close();
}
}
};
' VB.NET version
Imports Limilabs.Mail
Imports Limilabs.Mail.Headers
Imports Limilabs.Client.SMTP
Public Module Module1
Public Sub Main(ByVal args As String())
' Use builder class to create new email
Dim builder As New MailBuilder()
builder.From.Add(New MailBox("alice@mail.com", "Alice"))
builder.[To].Add(New MailBox("bob@mail.com", "Bob"))
builder.Subject = "Test"
builder.Text = "This is plain text message."
Dim email As IMail = builder.Create()
' Send the message
Using smtp As New Smtp()
smtp.Connect("server.example.com") ' or ConnectSSL for SSL
smtp.UseBestLogin("user", "password") ' remove if not needed
Dim result As ISendMessageResult = smtp.SendMessage(email)
If result.Status = SendMessageStatus.Success Then
' Message was sent.
End If
smtp.Close()
End Using
End Sub
End Module
Please note that some error handling is missing for simplicity and you should examine ISendMessageResult result object returned by SendMessage method to be sure that email sending was successful.