Mail.dll - How to start |
After installation, this class reference, C# and VB.NET examples can be found in your Start menu.
Go here for licensing and general help, and here for ordering.
Note |
---|
If you need help, please visit our technical Q&A forum. You can find many samples online. |
First you have to add reference to Mail.dll to your project. See MSDN how to. Then add all namespaces you need:
We connect to IMAP server using Imap class, then log in and acquire IMail object using MailBuilder class.
// C# using(Imap imap = new Imap()) { imap.ConnectSSL("imap.server.com"); // or Connect for non SSL/TLS imap.UseBestLogin("user", "password"); imap.SelectInbox(); List<long> uidList = imap.Search(Flag.Unseen); foreach (long uid in uidList) { IMail email = new MailBuilder() .CreateFromEml(imap.GetMessageByUID(uid)); string subject = email.Subject; string text = email.GetBodyAsText(); } imap.Close(); }
We connect to POP3 server using Pop3 class, then log in and acquire IMail object using MailBuilder class.
// C# using(Pop3 pop3 = new Pop3()) { pop3.ConnectSSL("pop3.server.com"); // or Connect for non SSL/TLS pop3.UseBestLogin("user", "password"); foreach (string uid in pop3.GetAll()) { IMail email = new MailBuilder() .CreateFromEml(pop3.GetMessageByUID(uid)); string subject = email.Subject; string text = email.GetBodyAsText(); } pop3.Close(); }
You have to create IMail object. Use MailBuilder class or fluent interface, then connect to your SMTP server using Smtp and send your message.
// C# MailBuilder builder = new MailBuilder(); builder.From.Add(new MailBox("from@example.com")); builder.To.Add(new MailBox("to@example.com")); builder.Html = @"Html with an image: <img src=""cid:lena"" />"; builder.Subject = "Subject"; MimeData visual = builder.AddVisual(@"c:\lena.jpeg"); visual.ContentId = "lena"; MimeData attachment = builder.AddAttachment(@"c:\tmp.doc"); attachment.SetFileName("document.doc", guessContentType: true); IMail email = builder.Create(); using(Smtp smtp = new Smtp()) { smtp.Connect("smtp.server.com"); // or ConnectSSL for SSL/TLS smtp.UseBestLogin("user", "password"); smtp.SendMessage(email); smtp.Close(); }
Note |
---|
If you need help, please visit our technical Q&A forum. You can find many samples online. |