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).

Tags: ,

7 Responses to “Reply to an email”

  1. Prosolit Says:

    Hi !
    I have bought your library and when I test your example of reply builder I have an TemplateException –> ‘Html’ was not found

    at this line…

    Dim builder As MailBuilder = replyBuilder.ReplyToAll(New MailBox(“bob@example.org”, “Bob”))

    Do you have an explication for this exception ?
    Thanks in advance for your help !

  2. Limilabs support Says:

    @Prosolit

    Could you show your code and exception stack trace?
    Have you changed any of the reply templates?
    Can you send us the raw version of the message you are replying to?

  3. Prosolit Says:

    Thanks for your quick response !
    For info, I use the 3.0.1217.1817 mail.dll. I’am on Visual Studio 2008 with framework 3.5 (all in french version)

    This is the code :
    ———————
    _event.MailObject.Save(Application.StartupPath + “\test.eml”); //mail content –> see at the end of the post
    ReplyBuilder _replyBuilder = _event.MailObject.Reply();
    _replyBuilder.Html = @”Cher test,test…Signature”;

    // Exception : ‘Html’ was not found (TemplateException)—->
    MailBuilder builder = _replyBuilder.ReplyToAll(new MailBox(EmailFrom, “Aroma-zen.com Service Client”));
    //<----- Exception

  4. Limilabs support Says:

    @Prosolit

    Ah! It seems there is a difference in how reflection in .NET 4.0 and .NET 3.5 treats anonymous types.
    I’ll release corrected version today.
    Thanks!

    [Update]
    Resolved, the version is 3.0.1222.1931.
    You can download it at http://www.limilabs.com/mail/download

  5. Prosolit Says:

    Perfect ! It works !
    Thank you very much for the quick response.

  6. Carlos Rodrigues Says:

    Hi!

    How can i get in the HtmlReplayTemplate the original recipient name?

    Thanks in advance
    Carlos

  7. Limilabs support Says:

    @Carlos

    In the reply templates (HtmlReplyTemplate, TextReplyTemplate, SubjectReplyTemplate) you have access to following fields:
    Subject – subject of the reply; Text – plain text of the reply; Html – html of the reply; QuoteText – original text; QuoteHtml – original html; IMail Original – (and all its properties).

    Please note that you can use any template and data to generate actual reply email (ReplyBuilder.Html and (ReplyBuilder.Text).

Questions?

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