How to enable SSL for SmtpClient in Web.config

68,680

Solution 1

For .NET 3 and earlier: You can't. You have to manage it by hand.

For more information you can see https://blogs.msdn.microsoft.com/vikas/2008/04/29/bug-asp-net-2-0-passwordrecovery-web-control-cannot-send-emails-to-ssl-enabled-smtp-servers/.

For .NET 4: You can.

See http://theoldsewingfactory.com/2011/01/06/enable-ssl-in-web-config-for-smtpclient/

<configuration>
    <system.net>
        <mailSettings>
            <smtp deliveryMethod=”network”>
                <network host="localhost"
                         port="25"
                         enableSsl="true"
                         defaultCredentials="true" />
            </smtp>
        </mailSettings>
    </system.net>
</configuration>

Solution 2

I have a dirty workaround (until .NET 4.0 comes out). Instead of changin my code it relies on the used port to determine if SSL is required or not.

var client = new SmtpClient();
client.EnableSsl = client.Port == 587 || client.Port == 465;
// This could also work
//client.EnableSsl = client.Port != 25;

I said it was a dirty hack, but it working fine for the different configurations that we encounter.

Solution 3

this works for me in .net 4

E.G. in web.config

network host="somesmtpserver" userName="[email protected]" 
password="whatever" port="25" enableSsl="true"         

Solution 4

Giles Roberts Jan 18 '12 at 18:01 said

this works for me in .net 4

E.G. in web.config

network host="somesmtpserver" userName="[email protected]" 
password="whatever" port="25" enableSsl="true" 

Port 25 is not a SSL port. Port 25 is the default SMTP port. Furthermore the web.config code is partly filled out. The code should be

    <system.net>
         <mailSettings>
              <smtp deliveryMethod="Network" from="[email protected]">
                     <network host="smtp.gmail.com"
                     userName="[email protected]"
                     password="********"
                     port="587"
                     defaultCredentials="true"
                     enableSsl="true" />
             </smtp>
        </mailSettings>
   </system.net>

This settings above is more accurate then the original web.config code. I don't know witch method is better. Using web.config or using the code-behind page to send the e-mail. No matter witch method you use the code-behind file has to be modified. I say this because you have to wire up From, Subject, and Body text boxes. I'm taking it for granted that the end results that you want to send a message through an aspx web page

Solution 5

This is the code I use in my web.config file.enter image description here

Share:
68,680
holiveira
Author by

holiveira

Updated on January 15, 2020

Comments

  • holiveira
    holiveira over 4 years

    Is there a way to set the EnableSSL from the web.config?

    I could set this property in code, but that wouldn't work for the Simple Mail Web Event and other classes that uses the default Smtp Server. Any ideas?