{"id":1450,"date":"2010-10-26T11:03:31","date_gmt":"2010-10-26T09:03:31","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=1450"},"modified":"2016-11-18T12:11:33","modified_gmt":"2016-11-18T10:11:33","slug":"forward-email-as-attachment","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/forward-email-as-attachment","title":{"rendered":"Forward an email as an attachment"},"content":{"rendered":"<p>If you want to share an email with other parties, forwarding it works like a charm in Mail.dll.<\/p>\n<div class=\"well\">\nThere are two ways of forwarding a message:<\/p>\n<ul>\n<li><a href=\"\/blog\/forward-email\">Forwarding an email by inserting the original inline in your forward<\/a><\/li>\n<li>Forwarding an email as an attachment<\/li>\n<\/ul>\n<\/div>\n<p>In this article we&#8217;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.<\/p>\n<h2>Why forward as an attachment?<\/h2>\n<p>Forwarding as an attachment is a way to <strong>share the email body in exactly the form you received it<\/strong>. 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 \u2014 useful to help troubleshooting email problems, for example.<\/p>\n<h2>Forwarding an email<\/h2>\n<p>In our example we&#8217;ll use Mail.dll <a href=\"\/mail\">.NET IMAP component<\/a> to download first message from an IMAP server. Then we&#8217;ll create new message and we&#8217;ll add previously received email as an attachment to it. Finally we&#8217;ll use Mail.dll SMTP client to send this message.<\/p>\n<p>Please note that we are using <em>MimeFactory <\/em>class to create MIME object with content type set to mime-rfc822. This content type is specially designed for attaching email messages.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C# version\r\n\r\nbyte&#x5B;] eml = GetFirstMessage();\r\n\r\nMailBuilder builder = new MailBuilder();\r\nbuilder.From.Add(new MailBox(&quot;from@example.com&quot;));\r\nbuilder.To.Add(new MailBox(&quot;forward_address@example.com&quot;));\r\nbuilder.Subject = &quot;Forwarded email is attached&quot;;\r\n\r\n\/\/ attach the message\r\nMimeRfc822 rfc822 = new MimeFactory().CreateMimeRfc822();\r\nrfc822.Data = eml;\r\n\r\nbuilder.AddAttachment(rfc822);\r\n\r\nIMail forward = builder.Create();\r\n\r\nusing (Smtp smtp = new Smtp())\r\n{\r\n    smtp.Connect(&quot;smtp.example.com&quot;); \/\/ or ConnectSSL if you want to use SSL\r\n    smtp.UseBestLogin(&quot;user&quot;, &quot;password&quot;);\r\n    smtp.SendMessage(forward);\r\n    smtp.Close();\r\n}\r\n\r\n\r\nstatic byte&#x5B;] GetFirstMessage()\r\n{\r\n    byte&#x5B;] eml;\r\n    using (Imap imap = new Imap())\r\n    {\r\n        imap.Connect(&quot;imap.example.com&quot;); \/\/ or ConnectSSL if you want to use SSL\r\n        imap.UseBestLogin(&quot;user&quot;, &quot;password&quot;);\r\n\r\n        \/\/ Receive first message\r\n        List&lt;long&gt; uids = imap.GetAll();\r\n        if (uids.Count == 0)\r\n            throw new Exception(&quot;There are no messages&quot;);\r\n        eml = imap.GetMessageByUID(uids&#x5B;0]);\r\n        imap.Close();\r\n    }\r\n    return eml;\r\n}\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET version\r\n\r\nDim eml As Byte()= GetFirstMessage()\r\n\r\nDim builder As New MailBuilder()\r\nbuilder.From.Add(New MailBox(&quot;from@example.com&quot;))\r\nbuilder.&#x5B;To].Add(New MailBox(&quot;forward_address@example.com&quot;))\r\nbuilder.Subject = &quot;Forwarded email is attached&quot;\r\n\r\n' attach the message\r\nDim rfc822 As MimeRfc822 = New MimeFactory().CreateMimeRfc822()\r\nrfc822.Data = eml\r\n\r\nbuilder.AddAttachment(rfc822)\r\n\r\nDim forward As IMail = builder.Create()\r\n\r\nUsing smtp As New Smtp()\r\n    smtp.Connect(&quot;smtp.example.com&quot;) ' or ConnectSSL if you want to use SSL\r\n    smtp.UseBestLogin(&quot;user&quot;, &quot;password&quot;)\r\n    smtp.SendMessage(forward)\r\n    smtp.Close()\r\nEnd Using\r\n\r\n\r\nPrivate Function GetFirstMessage() As Byte()\r\n    Dim eml As Byte()\r\n    Using imap As New Imap()\r\n        imap.Connect(&quot;imap.example.com&quot;) ' or ConnectSSL if you want to use SSL\r\n        imap.UseBestLogin(&quot;user&quot;, &quot;password&quot;)\r\n\r\n        ' Receive first message\r\n        Dim uids As List(Of Long) = imap.GetAll()\r\n        If uids.Count = 0 Then\r\n            Throw New Exception(&quot;There are no messages&quot;)\r\n        End If\r\n        eml = imap.GetMessageByUID(uids(0))\r\n        imap.Close()\r\n    End Using\r\n    Return eml\r\nEnd Function\r\n\r\n<\/pre>\n<h2>Forwarding an email saved to disk<\/h2>\n<p>If your original message is already saved on disk. You can use <em>AddAttachment<\/em> method. It will automatically choose correct content-type using the file&#8217;s extension (.eml).<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C#\r\n\r\nMailBuilder builder = new MailBuilder();\r\nbuilder.From.Add(new MailBox(&quot;bob@example.com&quot;, &quot;Bob&quot;));\r\nbuilder.To.Add(new MailBox(&quot;alice@example.com&quot;, &quot;Alice&quot;));\r\nbuilder.Subject = &quot;Forwarded message&quot;;\r\nbuilder.Text = &quot;Forwarded message is attached.&quot;;\r\n\r\nbuilder.AddAttachment(&quot;original.eml&quot;);\r\n\r\nIMail forward = builder.Create();\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET\r\n\r\nDim builder As New MailBuilder()\r\nbuilder.From.Add(New MailBox(&quot;bob@example.com&quot;, &quot;Bob&quot;))\r\nbuilder.&#x5B;To].Add(New MailBox(&quot;alice@example.com&quot;, &quot;Alice&quot;))\r\nbuilder.Subject = &quot;Forwarded message&quot;\r\nbuilder.Text = &quot;Forwarded message is attached.&quot;\r\n\r\nbuilder.AddAttachment(&quot;original.eml&quot;)\r\n\r\nDim forward As IMail = builder.Create()\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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: Forwarding an email by inserting the original inline in your forward Forwarding an email as an attachment In this article we&#8217;ll describe the second option, in which you add [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[74,15,33,50,57],"class_list":["post-1450","post","type-post","status-publish","format-standard","hentry","category-mail-dll","tag-attachments","tag-c","tag-email-component","tag-smtp","tag-vb-net"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1450"}],"collection":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/comments?post=1450"}],"version-history":[{"count":12,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1450\/revisions"}],"predecessor-version":[{"id":5117,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1450\/revisions\/5117"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=1450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=1450"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=1450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}