Forward an email as an attachment

If you want to share an email with other parties, forwarding it works like a charm in Mail.dll.

There are two ways of forwarding a message:

In this article we’ll describe the second option, in which you add forwarded an email as an attachment appended to your forward. This lets you forward multiple emails in one go too.

Why forward as an attachment?

Forwarding as an attachment is a way to share the email body in exactly the form you received it. This makes it easier for the recipient of your forward to reply to the original sender and it preserves some message details that can otherwise be lost — useful to help troubleshooting email problems, for example.

Forwarding an email

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 and we’ll add previously received email as an attachment to it. Finally we’ll use Mail.dll SMTP client to send this message.

Please note that we are using MimeFactory class to create MIME object with content type set to mime-rfc822. This content type is specially designed for attaching email messages.

// C# version

byte[] eml = GetFirstMessage();

MailBuilder builder = new MailBuilder();
builder.From.Add(new MailBox("from@example.com"));
builder.To.Add(new MailBox("forward_address@example.com"));
builder.Subject = "Forwarded email is attached";

// attach the message
MimeRfc822 rfc822 = new MimeFactory().CreateMimeRfc822();
rfc822.Data = eml;

builder.AddAttachment(rfc822);

IMail forward = builder.Create();

using (Smtp smtp = new Smtp())
{
    smtp.Connect("smtp.example.com"); // or ConnectSSL if you want to use SSL
    smtp.UseBestLogin("user", "password");
    smtp.SendMessage(forward);
    smtp.Close();
}


static byte[] GetFirstMessage()
{
    byte[] eml;
    using (Imap imap = new Imap())
    {
        imap.Connect("imap.example.com"); // or ConnectSSL if you want to use SSL
        imap.UseBestLogin("user", "password");

        // Receive first message
        List<long> uids = imap.GetAll();
        if (uids.Count == 0)
            throw new Exception("There are no messages");
        eml = imap.GetMessageByUID(uids[0]);
        imap.Close();
    }
    return eml;
}
' VB.NET version

Dim eml As Byte()= GetFirstMessage()

Dim builder As New MailBuilder()
builder.From.Add(New MailBox("from@example.com"))
builder.[To].Add(New MailBox("forward_address@example.com"))
builder.Subject = "Forwarded email is attached"

' attach the message
Dim rfc822 As MimeRfc822 = New MimeFactory().CreateMimeRfc822()
rfc822.Data = eml

builder.AddAttachment(rfc822)

Dim forward As IMail = builder.Create()

Using smtp As New Smtp()
    smtp.Connect("smtp.example.com") ' or ConnectSSL if you want to use SSL
    smtp.UseBestLogin("user", "password")
    smtp.SendMessage(forward)
    smtp.Close()
End Using


Private Function GetFirstMessage() As Byte()
    Dim eml As Byte()
    Using imap As New Imap()
        imap.Connect("imap.example.com") ' or ConnectSSL if you want to use SSL
        imap.UseBestLogin("user", "password")

        ' Receive first message
        Dim uids As List(Of Long) = imap.GetAll()
        If uids.Count = 0 Then
            Throw New Exception("There are no messages")
        End If
        eml = imap.GetMessageByUID(uids(0))
        imap.Close()
    End Using
    Return eml
End Function

Forwarding an email saved to disk

If your original message is already saved on disk. You can use AddAttachment method. It will automatically choose correct content-type using the file’s extension (.eml).

// C#

MailBuilder builder = new MailBuilder();
builder.From.Add(new MailBox("bob@example.com", "Bob"));
builder.To.Add(new MailBox("alice@example.com", "Alice"));
builder.Subject = "Forwarded message";
builder.Text = "Forwarded message is attached.";

builder.AddAttachment("original.eml");

IMail forward = builder.Create();
' VB.NET

Dim builder As New MailBuilder()
builder.From.Add(New MailBox("bob@example.com", "Bob"))
builder.[To].Add(New MailBox("alice@example.com", "Alice"))
builder.Subject = "Forwarded message"
builder.Text = "Forwarded message is attached."

builder.AddAttachment("original.eml")

Dim forward As IMail = builder.Create()

Tags:     

Questions?

Consider using our Q&A forum for asking questions.

One Response to “Forward an email as an attachment”

  1. Forward an email Says:

    […] You can also forward an email as a attachment. […]