Forward an email

If you want to share an email with other parties, forwarding is what you need to do.

There are two ways of forwarding a message:

In this article we’ll describe the first option, in which you insert the original inline in your forward and copy all attachments. You can use Mail.dll to forward both HTML and plain-text emails.

ForwardBuilder class allows you to specify custom HTML and plain-text templates for the forward. Mail.dll will parse the HTML code, extract body part, and build-in template engine will do the rest.

The next great thing is that plain-text version of the email is always generated automatically.

In our example we’ll use Mail.dll .NET IMAP component to download first message from an IMAP server. Then we’ll create new message by using ForwardBuilder class. The sample also shows how to add additional attachments to the forwarded email. Finally we’ll use Mail.dll SMTP client to send this message.

// C#

IMail original = GetFirstMessage();

ForwardBuilder forwardBuilder = original.Forward();

// You can specify your own, custom, body and subject templates:
forwardBuilder.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>";
forwardBuilder.SubjectReplyTemplate = "Fw: [Original.Subject]";

forwardBuilder.Html = "Tom, <br/><br/>please resolve this.";

MailBuilder builder = forwardBuilder.Forward(new MailBox("bob@example.org", "Bob"));
builder.To.Add(new MailBox("tom@example.org", "Tom"));

// You can add attachments to forwarded email
//builder.AddAttachment("report.csv");

IMail forward = builder.Create();

using (Smtp smtp = new Smtp())
{
    smtp.Connect("smtp.example.org"); // or ConnectSSL
    smtp.UseBestLogin("user", "password");
    smtp.SendMessage(forward);
    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 forwardBuilder As ForwardBuilder = original.Forward()

' You can specify your own, custom, body and subject templates:
forwardBuilder.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>"
forwardBuilder.SubjectReplyTemplate = "Fw: [Original.Subject]"

forwardBuilder.Html = "Tom, <br/><br/>please resolve this."

Dim builder As MailBuilder = forwardBuilder.Forward( _
    New MailBox("bob@example.org", "Bob"))
builder.[To].Add(New MailBox("tom@example.org", "Tom"))

' You can add attachments to forwarded email
'builder.AddAttachment("report.csv")

Dim forward As IMail = builder.Create()

Using smtp As New Smtp()
	smtp.Connect("smtp.example.org")
	smtp.UseBestLogin("user", "password") ' or ConnectSSL
	smtp.SendMessage(forward)
	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 forward templates (HtmlForwardTemplate, TextForwardTemplate, SubjectForwardTemplate) you have access to following fields:

  • string Subject – subject of the forward (e.g. [Subject])
  • string Text – plain text of the forward (e.g. [Test])
  • string Html – html of the forward (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 forwarding and all its properties (e.g. [Original.Subject], [Original.Sender.Address])

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

If you want as little change as possible made to the original email you can use following code:

// C#

IMail original = GetFirstMessage();

original.To.Clear();
original.To.Add(new MailBox("forward@example.org"));

using (Smtp smtp = new Smtp())
{
    smtp.Connect("smtp.example.org"); // or ConnectSSL
    smtp.UseBestLogin("user", "password");
    smtp.SendMessage(original);
    smtp.Close();
}
' VB.NET version

Dim original As IMail = GetFirstMessage()

original.[To].Clear()
original.[To].Add(New MailBox("forward@example.org"))

Using smtp As New Smtp()
	smtp.Connect("smtp.example.org")
	smtp.UseBestLogin("user", "password") ' or ConnectSSL
	smtp.SendMessage(original)
	smtp.Close()
End Using

You can also forward an email as a attachment.

Tags: , , ,

3 Responses to “Forward an email”

  1. Waldemar Sdzuj Says:

    Can change the subject when Forward a email?

    Best regards,

    Waldemar Sdzuj

  2. Limilabs support Says:

    @Waldemar,

    Yes you can.

    You can set a different template being used to generate the subject:

    ForwardBuilder forwardBuilder = original.Forward();
    forwardBuilder.SubjectReplyTemplate = "Fwd: [Original.Subject]";
    

    -or- you can directly change the subject of the new email:

    MailBuilder builder = forwardBuilder.Forward(...)
    builder.Subject = "Different subject";
    
  3. Forward an email as an attachment | Blog | Limilabs Says:

    […] Forwarding an email by inserting the original inline in your forward […]

Questions?

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