Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does System.Net.Mail fail where System.Web.Mail works

I can get both System.Net.Mail and System.Web.Mail to work with GMail, but I can't get them both to work with smtp.att.yahoo.com.

I get the SMTP settings from my own Web.config keys. These settings work when I send using System.Web.Mail, but fail with System.Net.Mail.

    <add key="SmtpServer" value="smtp.att.yahoo.com"/>
    <add key="SmtpServerAuthenticateUser" value="[email protected]"/>
    <add key="SmtpServerPort" value="465"/>
    <add key="SmtpUseSSL" value="1"/>
    <add key="SmtpServerAuthenticatePassword" value="MY PASSWORD"/>

Here is the code that grabs my settings, and works with GMail, fails with att.yahoo:

        SmtpClient smtp;

        if (!string.IsNullOrEmpty(Util.get_setting("SmtpServer", "")))
        {
           smtp = new SmtpClient(Util.get_setting("SmtpServer", ""));
        }
        else
        {
           smtp = new SmtpClient();
        }


        if (!string.IsNullOrEmpty(Util.get_setting("SmtpServerAuthenticatePassword", "")))
           smtp.Credentials = new System.Net.NetworkCredential(
               Util.get_setting("SmtpServerAuthenticateUser", ""), 
               Util.get_setting("SmtpServerAuthenticatePassword", ""));

        if (!string.IsNullOrEmpty(Util.get_setting("SmtpServerPort", "")))
           smtp.Port = int.Parse(Util.get_setting("SmtpServerPort", ""));

        if (Util.get_setting("SmtpUseSSL", "0") == "1")
           smtp.EnableSsl = true;

        smtp.Send(message);

Is this my problem?

http://blogs.msdn.com/webdav_101/archive/2008/06/02/system-net-mail-with-ssl-to-authenticate-against-port-465.aspx

like image 311
Corey Trager Avatar asked Oct 05 '08 16:10

Corey Trager


2 Answers

I've learned the answer. The answer is:

Because System.Net.Mail does not support "implicit" SSL, only "explicit" SSL.

like image 138
Corey Trager Avatar answered Sep 18 '22 13:09

Corey Trager


The previous answers concerning implicit and explicit SSL connections via System.Net.Mail is absolutely correct. The way I was able to get through this obstacle, and without having to use the now obsolete System.Web.Mail, was to use the CDO (Collaborative Data Objects).

I detailed and gave an example on another stack overflow post (GMail SMTP via C# .Net errors on all ports) if curious. Otherwise, you can go directly to the KB article at http://support.microsoft.com/kb/310212.

Hope this helps!

like image 23
Bryan Allred Avatar answered Sep 20 '22 13:09

Bryan Allred