{"id":4189,"date":"2013-08-18T15:20:29","date_gmt":"2013-08-18T13:20:29","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=4189"},"modified":"2014-04-09T15:28:20","modified_gmt":"2014-04-09T13:28:20","slug":"read-system-net-mailsettings-smtp-settings-web-config","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/read-system-net-mailsettings-smtp-settings-web-config","title":{"rendered":"Read system.net\/mailSettings\/smtp settings from web.config"},"content":{"rendered":"<p>There is a standard way of specifying SMTP settings in .NET applications. .NET uses config files (app.config or web.config in case of ASP.NET) and <mailSettings> element to specify the appropriate SMTP parameters to send e-mail.<\/p>\n<p>Sample configuration (in this case Gmail SMTP settings) looks as follows:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;configuration&gt;\r\n\r\n&lt;system.net&gt;\r\n  &lt;mailSettings&gt;\r\n    &lt;smtp deliveryMethod=&quot;network&quot; from=&quot;pam@gmail.com&quot;&gt;\r\n      &lt;network\r\n        host=&quot;smtp.gmail.com&quot;\r\n        port=&quot;465&quot;\r\n        enableSsl=&quot;true&quot;\r\n        userName=&quot;pam@gmail.com&quot;\r\n        password=&quot;password&quot;\r\n    \/&gt;\r\n    &lt;\/smtp&gt;\r\n  &lt;\/mailSettings&gt;\r\n&lt;\/system.net&gt;\r\n\r\n&lt;\/configuration&gt;\r\n<\/pre>\n<p>If port attribute is omitted default value (25) is used. SMTP protocol typically uses ports 587 and 25 for non SSL connections, and port 465 for SSL ones.<\/p>\n<p>Although Mail.dll SMTP component does not support reading from web.config directly, it is quite easy to read those settings programmatically and use them with Mail.dll classes.<\/p>\n<p>Here&#8217;s the simple sample that reads from mailSettings section:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nSmtpSection section = (SmtpSection)ConfigurationManager.GetSection(&quot;system.net\/mailSettings\/smtp&quot;);\r\n\r\nstring from = section.From;\r\nstring host = section.Network.Host;\r\nint port = section.Network.Port;\r\nbool enableSsl = section.Network.EnableSsl;\r\nstring user = section.Network.UserName;\r\nstring password = section.Network.Password;\r\n<\/pre>\n<h2>Use web.config&#8217;s mailSettings with Mail.dll<\/h2>\n<p>In most cases you want to send email via SMTP server (DeliveryMethod set to SmtpDeliveryMethod.Network). Here&#8217;s the sample that uses web.config settings and Mail.dll&#8217;s STMP component to send an email message:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nSmtpSection section = (SmtpSection)ConfigurationManager.GetSection(&quot;system.net\/mailSettings\/smtp&quot;);\r\n\r\nIMail email = Fluent.Mail\r\n            .Text(&quot;Hi, how are you?&quot;)\r\n            .Subject(&quot;Hello&quot;)\r\n            .To(&quot;to@example.com&quot;)\r\n            .From(section.From)\r\n            .Create();\r\n\r\nusing (Smtp client = new Smtp())\r\n{\r\n    client.Connect(section.Network.Host, section.Network.Port, \r\n        section.Network.EnableSsl);\r\n    client.UseBestLogin(section.Network.UserName, section.Network.Password);\r\n    client.SendMessage(email);\r\n    client.Close();\r\n}\r\n<\/pre>\n<h2>IIS pickup folder<\/h2>\n<p>If you plan to use local IIS SMTP service to send emails you created using Mail.dll, you&#8217;ll need to save those emails to IIS pickup folder.<\/p>\n<p>You can specify folder location explicitly using SpecifiedPickupDirectory.PickupDirectoryLocation (default location is &#8220;c:\\Inetpub\\mailroot\\Pickup&#8221;). You can also  use SmtpDeliveryMethod.PickupDirectoryFromIis constant &#8211; in this case we&#8217;ll get pickup folder location directly from IIS metabase.<\/p>\n<p>Following is the code that recognizes different DeliveryMethods and acts accordingly:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nSmtpSection section = (SmtpSection)ConfigurationManager.GetSection(&quot;system.net\/mailSettings\/smtp&quot;);\r\nIMail email = Fluent.Mail\r\n            .Text(&quot;Hi, how are you?&quot;)\r\n            .Subject(&quot;Hello&quot;)\r\n            .To(&quot;lesnikowski@limilabs.com&quot;)\r\n            .From(section.From)\r\n            .Create();\r\n\r\nif (section.DeliveryMethod == SmtpDeliveryMethod.Network)\r\n{\r\n    using (Smtp client = new Smtp())\r\n    {\r\n        client.Connect(section.Network.Host, section.Network.Port, \r\n            section.Network.EnableSsl);\r\n        client.UseBestLogin(section.Network.UserName, section.Network.Password);\r\n        client.SendMessage(email);\r\n        client.Close();\r\n    }\r\n}\r\nelse if (section.DeliveryMethod == SmtpDeliveryMethod.SpecifiedPickupDirectory)\r\n{\r\n    string pickupFolder = section.SpecifiedPickupDirectory.PickupDirectoryLocation;\r\n    email.Save(Path.Combine(pickupFolder, &quot;email.eml&quot;));\r\n}\r\nelse if (section.DeliveryMethod == SmtpDeliveryMethod.PickupDirectoryFromIis)\r\n{\r\n    Assembly system = AppDomain.CurrentDomain.GetAssemblies()\r\n        .First(x =&gt; x.GetName().Name == &quot;System&quot;);\r\n    \r\n    Type iisPickupType = Type.GetType(\r\n        &quot;System.Net.Mail.IisPickupDirectory, &quot; \r\n        + system.GetName().FullName, \r\n        true);\r\n    \/\/ -or- use fully qualified system assembly name directly:\r\n    \/\/Type iisPickupType = Type.GetType(\r\n    \/\/    &quot;System.Net.Mail.IisPickupDirectory, &quot; \r\n    \/\/    + &quot;System, Version=4.0.0.0, Culture=neutral, &quot;\r\n    \/\/    + &quot;PublicKeyToken=b77a5c561934e089&quot;,\r\n    \/\/    true);\r\n\r\n    string pickupFolder = (string)iisPickupType.InvokeMember(\r\n        &quot;GetPickupDirectory&quot;, \r\n        BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.NonPublic, \r\n        null, null, null);\r\n\r\n    email.Save(Path.Combine(pickupFolder, &quot;email.eml&quot;));\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>There is a standard way of specifying SMTP settings in .NET applications. .NET uses config files (app.config or web.config in case of ASP.NET) and element to specify the appropriate SMTP parameters to send e-mail. Sample configuration (in this case Gmail SMTP settings) looks as follows: &lt;configuration&gt; &lt;system.net&gt; &lt;mailSettings&gt; &lt;smtp deliveryMethod=&quot;network&quot; from=&quot;pam@gmail.com&quot;&gt; &lt;network host=&quot;smtp.gmail.com&quot; port=&quot;465&quot; enableSsl=&quot;true&quot; [&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,6],"tags":[10,33,50],"class_list":["post-4189","post","type-post","status-publish","format-standard","hentry","category-mail-dll","category-programming","tag-asp-net","tag-email-component","tag-smtp"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/4189"}],"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=4189"}],"version-history":[{"count":21,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/4189\/revisions"}],"predecessor-version":[{"id":4446,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/4189\/revisions\/4446"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=4189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=4189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=4189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}