Reply to an email
You can use Mail.dll to easy reply to an HTML and plain-text emails.
ReplyBuilder class allows you to specify custom HTML and plain-text reply templates. Mail.dll will parse HTML, extract body part, and build-in template engine will do the rest to create nicely formatted reply with all attachments, To and Cc fields set.
The next great thing is that plain-text version of the email is generated automatically.
You can also take advantage of ReplyAll method that makes it easy to reply to all: senders of the message and all To and Cc recipients
In our example we’ll use Mail.dll .NET IMAP component to download first message from an IMAP server. Then we’ll use ReplyBuilder to create a reply email message. Finally we’ll use Mail.dll SMTP client to send this message.
// C# IMail original = GetFirstMessage(); ReplyBuilder replyBuilder = original.Reply(); // You can specify your own, custom, body and subject templates: replyBuilder.HtmlReplyTemplate = @"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd""> <html> <head> <meta http-equiv=""Content-Type"" content=""text/html; charset=UTF-8"" /> <title>[Subject]</title> </head> <body> [Html] <br /><br /> On [Original.Date] [Original.Sender.Name] wrote: <blockquote style=""margin-left: 1em; padding-left: 1em; border-left: 1px #ccc solid;""> [QuoteHtml] </blockquote> </body> </html>"; replyBuilder.SubjectReplyTemplate = "Re: [Original.Subject]"; replyBuilder.Html = "Alice, <br/><br/>thanks for your email."; MailBuilder builder = replyBuilder.ReplyToAll( new MailBox("bob@example.org", "Bob")); // You can add attachments to your reply //builder.AddAttachment("report.csv"); IMail reply = builder.Create(); using (Smtp smtp = new Smtp()) { smtp.Connect("smtp.example.com"); // or ConnectSSL smtp.UseBestLogin("user", "password"); smtp.SendMessage(reply); smtp.Close(); } static IMail GetFirstMessage() { IMail email; using (Imap imap = new Imap()) { imap.Connect("imap.example.com"); // or ConnectSSL if you want to use SSL imap.UseBestLogin("user", "password"); List<long> uids = imap.GetAll(); if (uids.Count == 0) throw new Exception("There are no messages"); var eml = imap.GetMessageByUID(uids[0]); email = new MailBuilder().CreateFromEml(eml); imap.Close(); } return email; }
' VB.NET Dim original As IMail = GetFirstMessage() Dim replyBuilder As ReplyBuilder = original.Reply() ' You can specify your own, custom, body and subject templates: replyBuilder.HtmlReplyTemplate = _ "<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">" + _ " <html>" + _ " <head>" + _ " <meta http-equiv=""Content-Type"" content=""text/html; charset=UTF-8"" />" + _ " <title>[Subject]</title>" + _ " </head>" + _ " <body>" + _ " [Html]" + _ " <br /><br />" + _ " On [Original.Date] [Original.Sender.Name] wrote:" + _ " <blockquote style=""margin-left: 1em; padding-left: 1em; border-left: 1px #ccc solid;"">" + _ " [QuoteHtml]" + _ " </blockquote>" + _ " </body>" + _ " </html>" replyBuilder.SubjectReplyTemplate = "Re: [Original.Subject]" Dim builder As MailBuilder = replyBuilder.ReplyToAll( _ New MailBox("bob@example.org", "Bob")) ' You can add attachments to your reply 'builder.AddAttachment("report.csv") Dim reply As IMail = builder.Create() Using smtp As New Smtp() smtp.Connect("smtp.example.com") ' or ConnectSSL smtp.UseBestLogin("user", "password") smtp.SendMessage(reply) smtp.Close() End Using Private Shared Function GetFirstMessage() As IMail Dim email As IMail Using imap As New Imap() imap.Connect("imap.example.com") ' or ConnectSSL if you want to use SSL imap.UseBestLogin("user", "password") Dim uids As List(Of Long) = imap.GetAll() If uids.Count = 0 Then Throw New Exception("There are no messages") End If Dim eml = imap.GetMessageByUID(uids(0)) email = New MailBuilder().CreateFromEml(eml) imap.Close() End Using Return email End Function
In the reply templates (HtmlReplyTemplate, TextReplyTemplate, SubjectReplyTemplate) you have access to following fields:
- string Subject – subject of the reply (e.g. [Subject])
- string Text – plain text of the reply (e.g. [Test])
- string Html – html of the reply (e.g. [Html])
- string QuoteText – original text quoted using ‘>’ chars (e.g. [QuoteText])
- string QuoteHtml – body contents of original html or original text converted to html (e.g. [QuoteHtml])
- IMail Original – original email you are relying to and all its properties (e.g. [Original.Subject], [Original.Sender.Address])
Please note that you can use any template and data to generate actual reply email (ReplyBuilder.Html and ReplyBuilder.Text).