{"id":381,"date":"2009-12-21T19:00:55","date_gmt":"2009-12-21T17:00:55","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=381"},"modified":"2017-07-21T13:28:28","modified_gmt":"2017-07-21T11:28:28","slug":"sending-email-with-embedded-image","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/sending-email-with-embedded-image","title":{"rendered":"Sending email with embedded image"},"content":{"rendered":"<p>There are 3 ways of embedding images inside email message:<\/p>\n<ul>\n<li>Embedding image using multipart\/related MIME object<\/li>\n<li>Referencing remote web server (requires image maintenance for a long time)<\/li>\n<li>Using BASE64 encoded inline images (not supported by many email clients)<\/li>\n<\/ul>\n<p>In this article we&#8217;ll show how to use the first method. This method makes message bigger, but images <em>don&#8217;t need to be stored on your web server<\/em>. Furthermore this method is widely supported by email clients.<\/p>\n<h2>How this works<\/h2>\n<p>When MIME tree is build we specify that images added are <strong>in relation to HTML content<\/strong> of the message. We also specify content disposition as <strong>inline<\/strong>. This  means that those images are not actual attachments, but rather should be displayed to user when he\/she opens the message.<\/p>\n<p>In Mail.dll it is quite simple as we just need to add the image to <em>MailBuilder.Visuals<\/em>collection using <em>AddVisual<\/em> method. Remember that you need to set the image&#8217;s Content-ID, as it is required in the next step.<\/p>\n<p>HTML body of the message references those images by using special <code>\"cid:\"<\/code> protocol. It specifies Content-ID of the image that should be used:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nThis is our &lt;strong&gt;brand new&lt;\/strong&gt; logo: &lt;br \/&gt;\r\n&lt;img src=&quot;cid:logo@example.com&quot; \/&gt;\r\n<\/pre>\n<h2>Create HTML message using MailBuilder<\/h2>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ Use builder class to create an email\r\nMailBuilder builder = new MailBuilder();\r\n\r\n\/\/ Set From, To\r\nbuilder.From.Add(new MailBox(&quot;alice@mail.com&quot;, &quot;Alice&quot;));\r\nbuilder.To.Add(new MailBox(&quot;bob@mail.com&quot;, &quot;Bob&quot;));\r\nbuilder.Subject = &quot;Test&quot;;\r\n\r\n\/\/ Set HTML content (Notice the src=&quot;cid:...&quot; attribute)\r\nbuilder.Html = @&quot;&lt;html&gt;&lt;body&gt;&lt;img src=&quot;&quot;cid:logo@example.com&quot;&quot; \/&gt;&lt;\/body&gt;&lt;\/html&gt;&quot;;\r\n\r\n\/\/ Html automatically extracts plaint text from html so,\r\n\/\/ unless you want to specify plain text explicitly, you don't need to use this line:\r\nbuilder.Text = &quot;This is text version of the message.&quot;;\r\n\r\n\/\/ Add image and set its Content-Id\r\nMimeData visual = builder.AddVisual(@&quot;c:\\image.jpg&quot;);\r\nvisual.ContentId = &quot;logo@example.com&quot;;\r\n\r\nIMail email = builder.Create();\r\n\r\n<\/pre>\n<p>And of course the VB.NET version of the same code:<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' Use builder class to create an email\r\nDim builder As New MailBuilder()\r\n\r\n' Set From, To\r\nbuilder.From.Add(New MailBox(&quot;alice@mail.com&quot;, &quot;Alice&quot;))\r\nbuilder.To.Add(New MailBox(&quot;bob@mail.com&quot;, &quot;Bob&quot;))\r\nbuilder.Subject = &quot;Test&quot;\r\n\r\n' Set HTML content (Notice the src=&quot;cid:...&quot; attribute)\r\nbuilder.Html = &quot;&lt;html&gt;&lt;body&gt;&lt;img src=&quot;&quot;cid:logo@example.com&quot;&quot; \/&gt;&lt;\/body&gt;&lt;\/html&gt;&quot;\r\n\r\n' Html automatically extracts plaint text from html so,\r\n' unless you want to specify plain text explicitly, you don't need to use this line:\r\nbuilder.Text = &quot;This is text version of the message.&quot;\r\n\r\n' Add image and set its Content-Id\r\nDim visual As MimeData = builder.AddVisual(&quot;c:\\image.jpg&quot;)\r\nvisual.ContentId = &quot;logo@example.com&quot;\r\n\r\nDim email As IMail = builder.Create()\r\n<\/pre>\n<h2>HTML message using fluent interface<\/h2>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C#\r\n\r\nusing Fluent = Limilabs.Mail.Fluent;\r\n\r\nIMail email = Fluent.Mail.Html(@&quot;&lt;html&gt;&lt;body&gt;&lt;img src=&quot;&quot;cid:logo@example.com&quot;&quot; \/&gt;&lt;\/body&gt;&lt;\/html&gt;&quot;)\r\n        .From(new MailBox(&quot;alice@mail.com&quot;, &quot;Alice&quot;))\r\n        .To(new MailBox(&quot;bob@mail.com&quot;, &quot;Bob&quot;))\r\n        .Subject(&quot;Test&quot;)\r\n        .AddVisual(@&quot;c:\\image.jpg&quot;)\r\n        .SetContentId(&quot;logo@example.com&quot;)\r\n        .Create();\r\n<\/pre>\n<p>And of course the VB.NET version using fluent interface:<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET\r\n\r\nIMail email = Mail.Html(&quot;&lt;html&gt;&lt;body&gt;&lt;img src=&quot;&quot;cid:logo@example.com&quot;&quot; \/&gt;&lt;\/body&gt;&lt;\/html&gt;&quot;) _\r\n\t.From(New MailBox(&quot;alice@mail.com&quot;, &quot;Alice&quot;)) _\r\n\t.To(New MailBox(&quot;bob@mail.com&quot;, &quot;Bob&quot;)) _\r\n\t.Subject(&quot;Test&quot;).AddVisual(&quot;c:\\image.jpg&quot;) _\r\n\t.SetContentId(&quot;logo@example.com&quot;) _\r\n\t.Create()\r\n<\/pre>\n<h2>Sending email using SMTP<\/h2>\n<p>Now we can connect to SMTP server and send the HTML email with embedded image we recently created:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing (Smtp client = new Smtp())\r\n{\r\n    client.Connect(&quot;smtp.example.com&quot;); \/\/ or ConnectSSL\r\n    client.UseBestLogin(&quot;user&quot;, &quot;password&quot;);\r\n    client.SendMessage(email);\r\n    client.Close();\r\n}\r\n<\/pre>\n<p>You can download <a href=\"\/mail\">Mail.dll email client here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are 3 ways of embedding images inside email message: Embedding image using multipart\/related MIME object Referencing remote web server (requires image maintenance for a long time) Using BASE64 encoded inline images (not supported by many email clients) In this article we&#8217;ll show how to use the first method. This method makes message bigger, but [&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-381","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\/381"}],"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=381"}],"version-history":[{"count":8,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/381\/revisions"}],"predecessor-version":[{"id":5362,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/381\/revisions\/5362"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}