{"id":1293,"date":"2010-10-25T15:43:05","date_gmt":"2010-10-25T13:43:05","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=1293"},"modified":"2016-11-18T12:12:06","modified_gmt":"2016-11-18T10:12:06","slug":"forward-email","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/forward-email","title":{"rendered":"Forward an email"},"content":{"rendered":"<p>If you want to share an email with other parties, forwarding is what you need to do.<\/p>\n<div class=\"well\">\nThere are two ways of forwarding a message:<\/p>\n<ul>\n<li>Forwarding an email by inserting the original inline in your forward<\/li>\n<li><a href=\"\/blog\/forward-email-as-attachment\">Forwarding an email as a attachment<\/a><\/li>\n<\/ul>\n<\/div>\n<p>In this article we&#8217;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.<\/p>\n<p><em>ForwardBuilder<\/em> class allows you to specify custom HTML and plain-text <strong>templates<\/strong> for the forward. Mail.dll will parse the HTML code, extract body part, and build-in template engine will do the rest.<\/p>\n<p>The next great thing is that <strong>plain-text version of the email is always generated automatically<\/strong>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/2010\/10\/reply.png\" alt=\"\" title=\"reply\" width=\"429\" height=\"388\" class=\"aligncenter size-full\" \/><\/p>\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 by using <em>ForwardBuilder<\/em> class. The sample also shows how to add additional attachments to the forwarded email. Finally we&#8217;ll use Mail.dll SMTP client to send this message.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C#\r\n\r\nIMail original = GetFirstMessage();\r\n\r\nForwardBuilder forwardBuilder = original.Forward();\r\n\r\n\/\/ You can specify your own, custom, body and subject templates:\r\nforwardBuilder.HtmlReplyTemplate = @&quot;&lt;!DOCTYPE html PUBLIC &quot;&quot;-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN&quot;&quot; &quot;&quot;http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd&quot;&quot;&gt;\r\n    &lt;html&gt;\r\n    &lt;head&gt;\r\n        &lt;meta http-equiv=&quot;&quot;Content-Type&quot;&quot; content=&quot;&quot;text\/html; charset=UTF-8&quot;&quot; \/&gt;\r\n        &lt;title&gt;&#x5B;Subject]&lt;\/title&gt;\r\n    &lt;\/head&gt;\r\n    &lt;body&gt;\r\n    &#x5B;Html]\r\n    &lt;br \/&gt;&lt;br \/&gt;\r\n    On &#x5B;Original.Date] &#x5B;Original.Sender.Name] wrote:\r\n    &lt;blockquote style=&quot;&quot;margin-left: 1em; padding-left: 1em; border-left: 1px #ccc solid;&quot;&quot;&gt;\r\n        &#x5B;QuoteHtml]\r\n    &lt;\/blockquote&gt;\r\n    &lt;\/body&gt;\r\n    &lt;\/html&gt;&quot;;\r\nforwardBuilder.SubjectReplyTemplate = &quot;Fw: &#x5B;Original.Subject]&quot;;\r\n\r\nforwardBuilder.Html = &quot;Tom, &lt;br\/&gt;&lt;br\/&gt;please resolve this.&quot;;\r\n\r\nMailBuilder builder = forwardBuilder.Forward(new MailBox(&quot;bob@example.org&quot;, &quot;Bob&quot;));\r\nbuilder.To.Add(new MailBox(&quot;tom@example.org&quot;, &quot;Tom&quot;));\r\n\r\n\/\/ You can add attachments to forwarded email\r\n\/\/builder.AddAttachment(&quot;report.csv&quot;);\r\n\r\nIMail forward = builder.Create();\r\n\r\nusing (Smtp smtp = new Smtp())\r\n{\r\n    smtp.Connect(&quot;smtp.example.org&quot;); \/\/ or ConnectSSL\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 IMail GetFirstMessage()\r\n{\r\n    IMail email;\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        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        var eml = imap.GetMessageByUID(uids&#x5B;0]); \r\n        email = new MailBuilder().CreateFromEml(eml);\r\n        imap.Close();\r\n    }\r\n    return email;\r\n}\r\n\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET\r\n\r\nDim original As IMail = GetFirstMessage()\r\n\r\nDim forwardBuilder As ForwardBuilder = original.Forward()\r\n\r\n' You can specify your own, custom, body and subject templates:\r\nforwardBuilder.HtmlReplyTemplate = _\r\n&quot;&lt;!DOCTYPE html PUBLIC &quot;&quot;-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN&quot;&quot; &quot;&quot;http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd&quot;&quot;&gt;&quot; + _\r\n&quot; &lt;html&gt;&quot; +  _\r\n&quot; &lt;head&gt;&quot; +  _\r\n&quot;    &lt;meta http-equiv=&quot;&quot;Content-Type&quot;&quot; content=&quot;&quot;text\/html; charset=UTF-8&quot;&quot; \/&gt;&quot; +  _\r\n&quot;    &lt;title&gt;&#x5B;Subject]&lt;\/title&gt;&quot; +  _\r\n&quot; &lt;\/head&gt;&quot; +  _\r\n&quot; &lt;body&gt;&quot; +  _\r\n&quot;  &#x5B;Html]&quot; +  _\r\n&quot;  &lt;br \/&gt;&lt;br \/&gt;&quot; +  _\r\n&quot;  On &#x5B;Original.Date] &#x5B;Original.Sender.Name] wrote:&quot; +  _\r\n&quot;  &lt;blockquote style=&quot;&quot;margin-left: 1em; padding-left: 1em; border-left: 1px #ccc solid;&quot;&quot;&gt;&quot; +  _\r\n&quot;    &#x5B;QuoteHtml]&quot; +  _\r\n&quot;  &lt;\/blockquote&gt;&quot; +  _\r\n&quot; &lt;\/body&gt;&quot; +  _\r\n&quot; &lt;\/html&gt;&quot;\r\nforwardBuilder.SubjectReplyTemplate = &quot;Fw: &#x5B;Original.Subject]&quot;\r\n\r\nforwardBuilder.Html = &quot;Tom, &lt;br\/&gt;&lt;br\/&gt;please resolve this.&quot;\r\n\r\nDim builder As MailBuilder = forwardBuilder.Forward( _\r\n    New MailBox(&quot;bob@example.org&quot;, &quot;Bob&quot;))\r\nbuilder.&#x5B;To].Add(New MailBox(&quot;tom@example.org&quot;, &quot;Tom&quot;))\r\n\r\n' You can add attachments to forwarded email\r\n'builder.AddAttachment(&quot;report.csv&quot;)\r\n\r\nDim forward As IMail = builder.Create()\r\n\r\nUsing smtp As New Smtp()\r\n\tsmtp.Connect(&quot;smtp.example.org&quot;)\r\n\tsmtp.UseBestLogin(&quot;user&quot;, &quot;password&quot;) ' or ConnectSSL\r\n\tsmtp.SendMessage(forward)\r\n\tsmtp.Close()\r\nEnd Using\r\n\r\nPrivate Shared Function GetFirstMessage() As IMail\r\n\tDim email As IMail\r\n\tUsing imap As New Imap()\r\n\t\timap.Connect(&quot;imap.example.com&quot;)\r\n\t\t' or ConnectSSL if you want to use SSL\r\n\t\timap.UseBestLogin(&quot;user&quot;, &quot;password&quot;)\r\n\t\tDim uids As List(Of Long) = imap.GetAll()\r\n\t\tIf uids.Count = 0 Then\r\n\t\t\tThrow New Exception(&quot;There are no messages&quot;)\r\n\t\tEnd If\r\n\t\tDim eml = imap.GetMessageByUID(uids(0))\r\n\t\temail = New MailBuilder().CreateFromEml(eml)\r\n\t\timap.Close()\r\n\tEnd Using\r\n\tReturn email\r\nEnd Function\r\n<\/pre>\n<p>In the forward templates (<em>HtmlForwardTemplate<\/em>, <em>TextForwardTemplate<\/em>, <em>SubjectForwardTemplate<\/em>) you have access to following fields:<\/p>\n<ul>\n<li><em>string Subject<\/em> &#8211; subject of the forward (e.g. [Subject])<\/li>\n<li><em>string Text<\/em> &#8211; plain text of the forward (e.g. [Test])<\/li>\n<li><em>string Html<\/em> &#8211; html of the forward (e.g. [Html])<\/li>\n<li><em>string QuoteText<\/em> &#8211; original text quoted using &#8216;>&#8217; chars (e.g. [QuoteText])<\/li>\n<li><em>string QuoteHtml<\/em> &#8211; body contents of original html or original text converted to html (e.g. [QuoteHtml])<\/li>\n<li><em>IMail Original<\/em> &#8211; original email you are forwarding and all its properties (e.g. [Original.Subject], [Original.Sender.Address])<\/li>\n<\/ul>\n<p>Please note that you can <a href=\"\/blog\/email-template-engine\">use any template and data to generate actual forward email<\/a> (<em>ForwardBuilder.Html<\/em> and <em>ForwardBuilder.Text<\/em>).<\/p>\n<p>If you want <strong>as little change as possible<\/strong> made to the original email you can use following code:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C#\r\n\r\nIMail original = GetFirstMessage();\r\n\r\noriginal.To.Clear();\r\noriginal.To.Add(new MailBox(&quot;forward@example.org&quot;));\r\n\r\nusing (Smtp smtp = new Smtp())\r\n{\r\n    smtp.Connect(&quot;smtp.example.org&quot;); \/\/ or ConnectSSL\r\n    smtp.UseBestLogin(&quot;user&quot;, &quot;password&quot;);\r\n    smtp.SendMessage(original);\r\n    smtp.Close();\r\n}\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET version\r\n\r\nDim original As IMail = GetFirstMessage()\r\n\r\noriginal.&#x5B;To].Clear()\r\noriginal.&#x5B;To].Add(New MailBox(&quot;forward@example.org&quot;))\r\n\r\nUsing smtp As New Smtp()\r\n\tsmtp.Connect(&quot;smtp.example.org&quot;)\r\n\tsmtp.UseBestLogin(&quot;user&quot;, &quot;password&quot;) ' or ConnectSSL\r\n\tsmtp.SendMessage(original)\r\n\tsmtp.Close()\r\nEnd Using\r\n<\/pre>\n<p>You can also <a href=\"\/blog\/forward-email-as-attachment\">forward an email as a attachment<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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: Forwarding an email by inserting the original inline in your forward Forwarding an email as a attachment In this article we&#8217;ll describe the first option, in which you insert the [&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":[15,33,28,78],"class_list":["post-1293","post","type-post","status-publish","format-standard","hentry","category-mail-dll","tag-c","tag-email-component","tag-imap","tag-smtp-vb-net"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1293"}],"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=1293"}],"version-history":[{"count":17,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1293\/revisions"}],"predecessor-version":[{"id":5119,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1293\/revisions\/5119"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=1293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=1293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=1293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}