Mail.dll - How to start

Mail.dll 3d box

After installation, this class reference, C# and VB.NET examples
can be found in your Start menu.

For frequently asked questions (FAQ) visit: www.lesnikowski.com/mail/faq.aspx

For ordering info please go to: www.lesnikowski.com/mail/buy.aspx

How to start using Mail.dll
First you have to add reference to Mail.dll to your project. See MSDN how to. Then add all namespaces:
C# code:
using Lesnikowski.Client;
using Lesnikowski.Client.IMAP;
using Lesnikowski.Mail;
using Lesnikowski.Mail.Fluent
using Lesnikowski.Mail.Headers;
using Lesnikowski.Mail.Headers.Constants;
VB.NET code:
Imports Lesnikowski.Client
Imports Lesnikowski.Client.IMAP
Imports Lesnikowski.Mail
Imports Lesnikowski.Mail.Fluent
Imports Lesnikowski.Mail.Headers
Imports Lesnikowski.Mail.Headers.Constants
Receive emails from IMAP server
We connect to IMAP server, then log in and acquire IMail object using MailBuilder class.
C# code:

using(Imap imap = new Imap())
{
    imap.Connect("imap.server.com");       // or ConnectSSL for SSL
    imap.UseBestLogin("user", "password");

    imap.SelectInbox();
    List<long> uidList = imap.SearchFlag(Flag.Unseen);
    foreach (long uid in uidList)
    {
        IMail email = new MailBuilder()
            .CreateFromEml(imap.GetMessageByUID(uid));

        Console.WriteLine(email.Subject);
        Console.WriteLine(email.Text);
    }
    imap.Close();
}
VB.NET code:

Using imap As New Imap
    imap.Connect("imap.server.com")       ' or ConnectSSL for SSL
    imap.UseBestLogin("user", "password")

    imap.SelectInbox()
    Dim uidList As List(Of Long) = imap.SearchFlag(Flag.Unseen)

    For Each uid As Long In uidList
        Dim email As IMail = New MailBuilder() _
            .CreateFromEml(imap.GetMessageByUID(uid))

        Console.WriteLine(email.Subject)
        Console.WriteLine(email.Text)
    Next
    imap.Close()
End Using
Receive emails from POP3 server
We connect to POP3 server, then log in and acquire IMail object using MailBuilder class.
C# code:

using(Pop3 pop3 = new Pop3())
{
    pop3.Connect("pop3.server.com");       // or ConnectSSL for SSL
    pop3.UseBestLogin("user", "password");

    foreach (string uid in pop3.GetAll())
    {
        IMail email = new MailBuilder()
            .CreateFromEml(pop3.GetMessageByUID(uid));

        Console.WriteLine(email.Subject);
        Console.WriteLine(email.Text);
    }
    pop3.Close();
}
VB.NET code:

Using pop3 As New Pop3
    pop3.Connect("pop3.server.com")       ' or ConnectSSL for SSL
    pop3.UseBestLogin("user", "password")

    For Each uid As String In pop3.GetAll()
       Dim email As IMail = New MailBuilder() _
            .CreateFromEml(pop3.GetMessageByUID(uid))

        Console.WriteLine(email.Subject)
        Console.WriteLine(email.Text)
    Next
    pop3.Close()
End Using
Send email using SMTP server
You have to create IMail object. Use MailBuilder class or fluent interface, then connect to your SMTP server and send your message.
C# code:

IMail email = Mail
    .Html(@"Html with an image: <img src=""cid:lena"" />")
    .AddVisual(@"c:\lena.jpeg").SetContentId("lena")
    .AddAttachment(@"c:\tmp.doc").SetFileName("document.doc")
    .To("to@mail.com")
    .From("from@mail.com")
    .Subject("Subject")
    .Create();

using(Smtp smtp = new Smtp())
{
    smtp.Connect("smtp.server.com");       // or ConnectSSL for SSL
    smtp.UseBestLogin("user", "password");

    smtp.SendMessage(email);                      

    smtp.Close();    
}                     
VB.NET code:

Dim email As IMail = Mail _
    .Html("Html with an image: <img src=""cid:lena"" />") _
    .AddVisual("C:\lena.jpeg").SetContentId("lena") _
    .AddAttachment("C:\tmp.doc").SetFileName("document.doc") _
    .To("to@mail.com") _
    .From("from@mail.com") _
    .Subject("Subject") _
    .Create()

Using smtp As Smtp = New Smtp
    smtp.Connect("smtp.server.com")       ' or ConnectSSL for SSL
    smtp.UseBestLogin("user", "password")

    smtp.SendMessage(email)

    smtp.Close()
End Using

You can find many more samples here: www.lesnikowski.com/mail/samples