Reply to an email
Wednesday, December 28th, 2011You 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 IMAP client 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 template:
replyBuilder.HtmlReplyTemplate =
"[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>";
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");
string 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 template:
replyBuilder.HtmlReplyTemplate = _
"[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>"
replyBuilder.Html = _
"Alice, <br/><br/>thanks for your email."
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 As String = imap.GetMessageByUID(uids(0))
email = New MailBuilder().CreateFromEml(eml)
imap.Close()
End Using
Return email
End Function
