{"id":1282,"date":"2010-10-25T15:38:14","date_gmt":"2010-10-25T13:38:14","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=1282"},"modified":"2015-10-08T23:01:15","modified_gmt":"2015-10-08T21:01:15","slug":"send-email-with-attachment","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/send-email-with-attachment","title":{"rendered":"Send email with attachment"},"content":{"rendered":"<p>In this article we\u2019ll create and send email with an attachment.<\/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>We&#8217;ll use <em>MailBuilder<\/em> class to create email message. This will be plain text message sent from Alice to Bob:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nMailBuilder builder = new MailBuilder();\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\nbuilder.Text = &quot;This is plain text message.&quot;;\r\n<\/pre>\n<p>You use <em>AddAttachment<\/em> method to add new attachment to the email, it returns <em>MimeData <\/em> class instance, that can by used to change\/set file name, data, content-id, or any other MIME header. This method has 3 overloads, that allow to create and add attachment from file, byte array or add existing <em>MimeData <\/em>object as email attachment. In our sample we&#8217;ll create attachment from file located on disk. <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nMimeData attachment = builder.AddAttachment(@&quot;..\/image.jpg&quot;);\r\n<\/pre>\n<p>Mail.dll email component automatically recognizes content type using file extension. When you create attachment using byte array or <em>MemoryStream<\/em>, you can use <em>ContentType<\/em> property to set it manually:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nattachment.ContentType = ContentType.ImageJpeg;\r\n<\/pre>\n<p>Finally we&#8217;ll use <em>Smtp <\/em> component to connect to SMTP server and send your message.<\/p>\n<p>Following is the entire sample, that creates new email message, adds attachment from file located on disk. 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 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;Test&quot;;\r\n        builder.Text = &quot;This is plain text message.&quot;;\r\n\r\n        \/\/ Read attachment from disk, add it to Attachments collection\r\n        MimeData attachment = builder.AddAttachment(@&quot;..\/image.jpg&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};\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;Test&quot;\r\n        builder.Text = &quot;This is plain text message.&quot;\r\n\r\n        ' Read attachment from disk, add it to Attachments collection\r\n        Dim attachment As MimeData = builder.AddAttachment(&quot;..\/image.jpg&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    End Sub\r\nEnd Module\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In this article we\u2019ll create and send email with an attachment. As a prerequisite you need to add reference to Mail.dll .NET email component to your project. We&#8217;ll use MailBuilder class to create email message. This will be plain text message sent from Alice to Bob: MailBuilder builder = new MailBuilder(); builder.From.Add(new MailBox(&quot;alice@mail.com&quot;, &quot;Alice&quot;)); builder.To.Add(new [&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-1282","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\/1282"}],"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=1282"}],"version-history":[{"count":8,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1282\/revisions"}],"predecessor-version":[{"id":4972,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1282\/revisions\/4972"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=1282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=1282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=1282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}