{"id":1291,"date":"2010-10-25T15:41:38","date_gmt":"2010-10-25T13:41:38","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=1291"},"modified":"2010-10-25T15:41:38","modified_gmt":"2010-10-25T13:41:38","slug":"send-email-using-fluent-interface","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/send-email-using-fluent-interface","title":{"rendered":"Send email using fluent interface"},"content":{"rendered":"<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/ C# version\n\nusing Fluent = Limilabs.Mail.Fluent;\n\nFluent.Mail.Html(@&quot;Html with an image: &lt;img src=&quot;&quot;cid:lena&quot;&quot; \/&gt;&quot;)\n  .AddVisual(@&quot;c:lena.jpeg&quot;).SetContentId(&quot;lena&quot;)\n  .AddAttachment(@&quot;c:tmp.doc&quot;).SetFileName(&quot;document.doc&quot;)\n  .To(&quot;to@mail.com&quot;)\n  .From(&quot;from@mail.com&quot;)\n  .Subject(&quot;Subject&quot;)\n  .UsingNewSmtp()\n  .Server(&quot;smtp.example.org&quot;)\n  .WithSSL()\n  .WithCredentials(&quot;user&quot;, &quot;password&quot;)\n  .Send();\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\n' VB.NET version\n\nImports Fluent = Limilabs.Mail.Fluent;\n\nFluent.Mail.Html(&quot;Html with an image: &lt;img src=&quot;&quot;cid:lena&quot;&quot; \/&gt;&quot;) _\n  .AddVisual(&quot;c:lena.jpeg&quot;).SetContentId(&quot;lena&quot;) _\n  .AddAttachment(&quot;c:tmp.doc&quot;).SetFileName(&quot;document.doc&quot;) _\n  .&#x5B;To](&quot;to@mail.com&quot;) _\n  .From(&quot;from@mail.com&quot;) _\n  .Subject(&quot;Subject&quot;) _\n  .UsingNewSmtp() _\n  .Server(&quot;smtp.example.org&quot;) _\n  .WithSSL() _\n  .WithCredentials(&quot;user&quot;, &quot;password&quot;) _\n  .Send()\n<\/pre>\n<p>Fluent interface for creating and sending emails was added to Mail.dll as an additional feature.<\/p>\n<p>You can still use MailBuilder class to create emails.<br \/>\nNote that error handling code was removed for simplicity, you should use try&#8230;catch block when sending.<\/p>\n<p>If your server <strong>does not require authentication or SSL<\/strong> simply skip WithSSL and WithCredentials methods:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/ C# version\n\nMail.Text(&quot;Hi there&quot;)\n    .Subject(&quot;Hi&quot;)\n    .To(&quot;to@mail.com&quot;)\n    .From(&quot;from@example.org&quot;)\n    .UsingNewSmtp()\n    .Server(&quot;smtp.example.org&quot;)\n    .Send();\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\n' VB.NET version\n\nMail.Text(&quot;Hi there&quot;) _\n  .Subject(&quot;Hi&quot;) _\n  .&#x5B;To](&quot;to@mail.com&quot;) _\n  .From(&quot;from@example.org&quot;) _\n  .UsingNewSmtp() _\n  .Server(&quot;smtp.example.org&quot;) _\n  .Send()\n<\/pre>\n<p>Same code using MailBuilder:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/ C# version\n\nMailBuilder builder = new MailBuilder();\nbuilder.Text = &quot;Hi there&quot;;\nbuilder.Subject = &quot;Hi&quot;;\nbuilder.To.Add(new MailBox(&quot;to@mail.com&quot;));\nbuilder.From.Add(new MailBox(&quot;from@example.org&quot;));\nIMail email= builder.Create();\n\nusing(Smtp smtp = new Smtp())\n{\n    smtp.Connect(&quot;smtp.example.org&quot;); \/\/ or ConnectSSL\n    \/\/ smtp.UseBestLogin(&quot;user&quot;, &quot;password&quot;);\n    smtp.SendMessage(email);\n    smtp.Close();\n}\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\n' VB.NET version\n\nDim builder As New MailBuilder()\nbuilder.Text = &quot;Hi there&quot;\nbuilder.Subject = &quot;Hi&quot;\nbuilder.&#x5B;To].Add(New MailBox(&quot;to@mail.com&quot;))\nbuilder.From.Add(New MailBox(&quot;from@example.org&quot;))\n\nDim emailAs IMail = builder.Create()\n\nUsing smtp As New Smtp()\n    smtp.Connect(&quot;smtp.example.org&quot;) ' or ConnectSSL\n    ' smtp.UseBestLogin(&quot;user&quot;, &quot;password&quot;)\n    smtp.SendMessage(email)\n    smtp.Close()\nEnd Using\n<\/pre>\n<p>You can also <strong>reuse existing Smtp connection<\/strong> to send multiple emails using fluent interface:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/ C# version\n\nusing(Smtp smtp = new Smtp())\n{\n    smtp.Connect(&quot;smtp.example.org&quot;); \/\/ or ConnectSSL\n    smtp.UseBestLogin(&quot;user&quot;, &quot;password&quot;);\n\n    Mail.Text(&quot;Hi there&quot;)\n        .Subject(&quot;Hi&quot;)\n        .To(&quot;to@mail.com&quot;)\n        .From(&quot;from@example.org&quot;)\n        .Send(smtp);\n\n    \/\/ Send more emails here...\n\n    smtp.Close();\n}\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\n' VB.NET version\n\nUsing smtp As New Smtp()\n\tsmtp.Connect(&quot;smtp.example.org&quot;) ' or ConnectSSL\n\tsmtp.UseBestLogin(&quot;user&quot;, &quot;password&quot;)\n\n\tMail.Text(&quot;Hi there&quot;) _\n          .Subject(&quot;Hi&quot;) _\n          .&#x5B;To](&quot;to@mail.com&quot;) _\n          .From(&quot;from@example.org&quot;) _\n          .Send(smtp)\n\n\t' Send more emails here...\n\n\tsmtp.Close()\nEnd Using\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\/\/ C# version using Fluent = Limilabs.Mail.Fluent; Fluent.Mail.Html(@&quot;Html with an image: &lt;img src=&quot;&quot;cid:lena&quot;&quot; \/&gt;&quot;) .AddVisual(@&quot;c:lena.jpeg&quot;).SetContentId(&quot;lena&quot;) .AddAttachment(@&quot;c:tmp.doc&quot;).SetFileName(&quot;document.doc&quot;) .To(&quot;to@mail.com&quot;) .From(&quot;from@mail.com&quot;) .Subject(&quot;Subject&quot;) .UsingNewSmtp() .Server(&quot;smtp.example.org&quot;) .WithSSL() .WithCredentials(&quot;user&quot;, &quot;password&quot;) .Send(); &#8216; VB.NET version Imports Fluent = Limilabs.Mail.Fluent; Fluent.Mail.Html(&quot;Html with an image: &lt;img src=&quot;&quot;cid:lena&quot;&quot; \/&gt;&quot;) _ .AddVisual(&quot;c:lena.jpeg&quot;).SetContentId(&quot;lena&quot;) _ .AddAttachment(&quot;c:tmp.doc&quot;).SetFileName(&quot;document.doc&quot;) _ .&#x5B;To](&quot;to@mail.com&quot;) _ .From(&quot;from@mail.com&quot;) _ .Subject(&quot;Subject&quot;) _ .UsingNewSmtp() _ .Server(&quot;smtp.example.org&quot;) _ .WithSSL() [&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-1291","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\/1291"}],"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=1291"}],"version-history":[{"count":0,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1291\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=1291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=1291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=1291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}