Send email

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.

Tags: , , ,

8 Responses to “Send email”

  1. Gaurav Shukla Says:

    How can i make sure that my mail has been successfully received by the recipient ??

  2. Limilabs support Says:

    @Gaurav,

    The short answer is: you can not.

    You can not influence recipient to receive your email, he/she may decide to delete it without reading or the message may be marked as spam, or his/hers email server may be down.
    You can of course try to avoid being marked as spam by sending legitimate content, adding DKIM signature or signing your email. You should handle bounced messages and react on failure.

    Finally you can request read receipt and process it when it arrives.

  3. Steven Says:

    Hi
    How can I detect whether recipient email address is valid or not before sending the email?
    So far I encounter error when sending email to invalid address.

  4. Limilabs support Says:

    @Steven,

    You can use AddressValidator class to validate email syntax:

    AddressValidationResult result = new AddressValidator().ValidateFormat("pat@example.com");
    
  5. Gyula Says:

    Hi there,

    I’ve copied the code above, and my VS2012 says that:

    Cannot find type System.Runtime.InteropServices.ExternalException in module mscorlib.dll.

    or

    „System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089” Couldn’t load file or dependency. The system cannot find the file specified.

    What could be the problem?

  6. Limilabs support Says:

    @Gyula,

    What project type have you created?
    To resolve this issue, simply add a reference to this assembly.

  7. Gyula Says:

    It’s a Windows Store application, created on Win8. I’ve read on a forum, that this package should work for sending emails from code.

  8. Limilabs support Says:

    @Gyula,

    Use MailForWindowsStore.dll (Windows Store version of Mail.dll):
    http://www.limilabs.com/mail-for-windows-store

    Remember to use await on all async methods (e.g.: Connect, UseBestLogin, …)

Questions?

Consider using our Q&A forum for asking any questions.