{"id":1289,"date":"2013-07-02T09:40:26","date_gmt":"2013-07-02T07:40:26","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=1289"},"modified":"2015-01-13T01:52:44","modified_gmt":"2015-01-12T23:52:44","slug":"send-email-with-custom-header","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/send-email-with-custom-header","title":{"rendered":"Send email with custom header"},"content":{"rendered":"<p>In this article we&#8217;ll show how to create and send email message with custom header added.<\/p>\n<p>As a prerequisite you need to add reference to Mail.dll <a href=\"\/mail\">.NET email component<\/a> to your project.<\/p>\n<p>In contrast to <em>System.Net.Mail<\/em>, Mail.dll allows almost any manipulation to email message&#8217;s MIME tree. This includes adding custom headers on the root level. The easiest way to achieve this, is to use <em>MailBuilder <\/em>class and <em>AddCustomHeader <\/em>method:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nMailBuilder builder = new MailBuilder();\r\nbuilder.AddCustomHeader(&quot;x-spam-value&quot;, &quot;90%&quot;);\r\n<\/pre>\n<p>As you can see this method operates on higher level of abstraction than MIME document, but when the email is created, you can observe that the custom header was actually added to the MIME document root:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nIMail email = builder.Create();\r\nstring header = email.Document.Root.Headers&#x5B;&quot;x-spam-value&quot;];\r\n<\/pre>\n<p>Custom headers (those that are not defined by <a href=\"\/mail\/rfcs\">email standards<\/a> like Date or Subject) should be prefixed with &#8220;X-&#8221; string (header names case is not important).<\/p>\n<p>Following is the entire sample, that creates new email message, adds custom header. Than it connects to specified SMTP server and sends the message. Please note that some error handling is missing for simplicity and you should examine <em>ISendMessageResult<\/em> result object returned by <em>SendMessage <\/em>to be sure that email sending was successful.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C# version\r\n\r\nusing System;\r\nusing Limilabs.Mail;\r\nusing Limilabs.Mail.Headers;\r\nusing Limilabs.Client.SMTP;\r\n\r\nclass Program\r\n{\r\n    static void Main(string&#x5B;] args)\r\n    {\r\n        \/\/ Use builder object to create new email message\r\n        MailBuilder builder = new MailBuilder();\r\n        builder.Subject = &quot;Test&quot;;\r\n        builder.Text = &quot;This is plain text message.&quot;;\r\n        builder.From.Add(new MailBox(&quot;alice@mail.com&quot;, &quot;Alice&quot;));\r\n        builder.To.Add(new MailBox(&quot;bob@mail.com&quot;, &quot;Bob&quot;));\r\n\r\n        builder.AddCustomHeader(&quot;x-spam-value&quot;, &quot;90%&quot;);\r\n\r\n        IMail email = builder.Create();\r\n\r\n        \/\/ Send the message\r\n        using (Smtp smtp = new Smtp())\r\n        {\r\n            smtp.Connect(&quot;server.example.com&quot;);    \/\/ or ConnectSSL\r\n            smtp.UseBestLogin(&quot;user&quot;, &quot;password&quot;);\r\n\r\n            smtp.SendMessage(email);\r\n\r\n            smtp.Close();\r\n        }\r\n    }\r\n};\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET version\r\n\r\nImports System;\r\nImports Limilabs.Mail\r\nImports Limilabs.Mail.Headers\r\nImports Limilabs.Client.SMTP\r\n\r\nPublic Module Module1\r\n    Public Sub Main(ByVal args As String())\r\n\r\n        ' Use builder object to create new email message\r\n        Dim builder As New MailBuilder()\r\n        builder.Subject = &quot;Test&quot;\r\n        builder.Text = &quot;This is plain text message.&quot;\r\n        builder.From.Add(New MailBox(&quot;alice@mail.com&quot;, &quot;Alice&quot;))\r\n        builder.&#x5B;To].Add(New MailBox(&quot;bob@mail.com&quot;, &quot;Bob&quot;))\r\n\r\n        builder.AddCustomHeader(&quot;x-spam-value&quot;, &quot;90%&quot;)\r\n\r\n        Dim email As IMail = builder.Create()\r\n\r\n        ' Send the message\r\n        Using smtp As New Smtp()\r\n            smtp.Connect(&quot;server.example.com&quot;)    ' or ConnectSSL\r\n            smtp.UseBestLogin(&quot;user&quot;, &quot;password&quot;)\r\n\r\n            smtp.SendMessage(email)\r\n\r\n            smtp.Close()\r\n        End Using\r\n\r\n    End Sub\r\nEnd Module\r\n<\/pre>\n<p>Fluent interface version:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C# version\r\n\r\nIMail email = Mail.Text(&quot;This is plain text message.&quot;)\r\n    .Subject(&quot;Test&quot;)\r\n    .From(New MailBox(&quot;alice@mail.com&quot;, &quot;Alice&quot;))\r\n    .To(&quot;to@mail.com&quot;)\r\n    .AddCustomHeader(&quot;X-Header&quot;, &quot;x header value&quot;)\r\n    .Create();\r\n\r\n\/\/ Send the message\r\nusing (Smtp smtp = new Smtp())\r\n{\r\n    smtp.Connect(&quot;server.example.com&quot;);\r\n    smtp.UseBestLogin(&quot;user&quot;, &quot;password&quot;);\r\n    smtp.SendMessage(email);\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 email As IMail = Mail.Text(&quot;This is plain text message.&quot;) _\r\n  .Subject(&quot;Test&quot;) _\r\n  .From(New MailBox(&quot;alice@mail.com&quot;, &quot;Alice&quot;)) _\r\n  .&#x5B;To](New MailBox(&quot;bob@mail.com&quot;, &quot;Bob&quot;)) _\r\n  .AddCustomHeader(&quot;X-Header&quot;, &quot;x header value&quot;) _\r\n  .Create()\r\n\r\n' Send the message\r\nUsing smtp As New Smtp()\r\n    smtp.Connect(&quot;server.example.com&quot;)\r\n    smtp.UseBestLogin(&quot;user&quot;, &quot;password&quot;)\r\n    smtp.SendMessage(email)\r\n    smtp.Close()\r\nEnd Using\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In this article we&#8217;ll show how to create and send email message with custom header added. As a prerequisite you need to add reference to Mail.dll .NET email component to your project. In contrast to System.Net.Mail, Mail.dll allows almost any manipulation to email message&#8217;s MIME tree. This includes adding custom headers on the root level. [&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,50,57],"class_list":["post-1289","post","type-post","status-publish","format-standard","hentry","category-mail-dll","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\/1289"}],"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=1289"}],"version-history":[{"count":5,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1289\/revisions"}],"predecessor-version":[{"id":4890,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1289\/revisions\/4890"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=1289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=1289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=1289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}