{"id":1284,"date":"2010-10-25T15:38:53","date_gmt":"2010-10-25T13:38:53","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=1284"},"modified":"2017-07-18T13:37:48","modified_gmt":"2017-07-18T11:37:48","slug":"send-html-email","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/send-html-email","title":{"rendered":"Send HTML email"},"content":{"rendered":"<p>In this article we&#8217;ll show how to create and send HTML email message with embedded image.<\/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>First we&#8217;ll learn some basics of HTML syntax used in emails. Especially how to reference image attached to the message from the HTML content. We use cid protocol to do that:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;img src = 'cid:image1' \/&gt;\r\n<\/pre>\n<p>What comes after &#8220;cid:&#8221; is the Content-ID of the referenced visual object. This object must be attached to the email.<\/p>\n<p>As always, when we need to create email message, we use <em>MailBuilder<\/em> class. We need to set <em>Html<\/em> property with the HTML data. It is always good to send emails both in HTML and plain text formats. Good thing is, that Mail.dll <strong>automatically extracts plain text data from HTML<\/strong> content when <em>Html<\/em> property is set.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nMailBuilder builder = new MailBuilder();\r\nbuilder.Html =                            \/\/ Set HTML body\r\n            &quot;This is &lt;strong&gt;HTML&lt;\/strong&gt; message, &quot; +\r\n            &quot;with embedded image:&lt;br \/&gt;&quot; +\r\n            &quot;&lt;img src = 'cid:image1' \/&gt;.&quot;;\r\n<\/pre>\n<p>Next thing we need to do is to attach the image. We&#8217;ll use  <em>AddVisual <\/em> method for that &#8211; it adds visual element to the email MIME tree. <em>AddVisual <\/em> has few overloads that allow to add MIME objects from memory stream, byte array or from disk.<\/p>\n<p>Remember to set <em>ContentId <\/em> property for added visual objects (E.g. image.ContentId = &#8220;image1&#8221;).<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nMimeData image = builder.AddVisual(@&quot;c:\\image.jpg&quot;);\r\nimage.ContentId = &quot;image1&quot;;\r\n<\/pre>\n<p>Final step is to connect to SMTP server and send the message. Use <em>Connect(string host)<\/em> method to establish connection &#8211; it uses standard SMTP server port: 587. You can use <em>Connect(string host, int port)<\/em> overload, if you need to specify different port (for example 25). If your server requires SSL use <em>ConnectSSL <\/em>method (Here you can find more info on using SMTP with SSL).<\/p>\n<p>Here&#8217;s the entire sample:<\/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.MIME;\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 class to create new email message\r\n        MailBuilder builder = new MailBuilder();\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        builder.Subject = &quot;This is HTML test&quot;;\r\n\r\n        builder.Html =                            \/\/ Set HTML body\r\n            &quot;This is &lt;strong&gt;HTML&lt;\/strong&gt; message, &quot; +\r\n            &quot;with embeddedimage:&lt;br \/&gt;&quot; +\r\n            &quot;&lt;img src = 'cid:image1' \/&gt;.&quot;;\r\n\r\n        \/\/ Read attachment from disk...and add it to Visuals collection\r\n        MimeData image = builder.AddVisual(@&quot;c:\\image.jpg&quot;);\r\n        image.ContentId = &quot;image1&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 for SSL\r\n            smtp.UseBestLogin(&quot;user&quot;, &quot;password&quot;); \/\/ remove if not needed\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 Limilabs.Client.SMTP\r\nImports Limilabs.Mail\r\nImports Limilabs.Mail.MIME\r\nImports Limilabs.Mail.Headers\r\n\r\nPublic Module Module1\r\n    Public Sub Main(ByVal args As String())\r\n\r\n        ' Use builder class to create new email message\r\n        Dim builder As New MailBuilder()\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        builder.Subject = &quot;This is HTML test&quot;\r\n\r\n        ' Set HTML body\r\n        builder.Html = &quot;This is &lt;strong&gt;HTML&lt;\/strong&gt; message, &quot; _\r\n                            + &quot;with embeddedimage:&lt;br \/&gt;&quot; _\r\n                            + &quot;&lt;img src = 'cid:image1' \/&gt;.&quot;\r\n\r\n        ' Read attachment from disk...and add it to Visuals collection\r\n        Dim image As MimeData = builder.AddVisual(&quot;c:\\image.jpg&quot;)\r\n        image.ContentId = &quot;image1&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 for SSL\r\n            smtp.UseBestLogin(&quot;user&quot;, &quot;password&quot;)   ' remove if not needed\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","protected":false},"excerpt":{"rendered":"<p>In this article we&#8217;ll show how to create and send HTML email message with embedded image. As a prerequisite you need to add reference to Mail.dll .NET email component to your project. First we&#8217;ll learn some basics of HTML syntax used in emails. Especially how to reference image attached to the message from the HTML [&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-1284","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\/1284"}],"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=1284"}],"version-history":[{"count":7,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1284\/revisions"}],"predecessor-version":[{"id":5360,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1284\/revisions\/5360"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=1284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=1284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=1284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}