C# SMTP authentication failed but credentials are correct

坚强是说给别人听的谎言 提交于 2019-12-02 12:04:46
silviagreen

This link saved my life: link

here the problem I experienced is well described: even though the user name is passed with the AUTH LOGIN the server responds with the AUTH_LOGIN_Username_Challenge again.

This problem happens only using System.Net.Mail. The link suggests tre possible solutions:

1)Use CDOSYS (Does not send the User Name with the AUTH Login)
2)Use System.Web.Mail (Does not send the User Name with the AUTH Login)
3)Contact the SMTP Server owner and have them fix the server.

Unfortunately I couldn't the SMTP server owner, so I had to use System.Web.Mail. I know it is deprecated but unfortunately in cases like this there is no other choice IMO.

That's my working code:

System.Web.Mail.MailMessage msg = new System.Web.Mail.MailMessage();
        msg.Body = message.Body;

        string smtpServer = "mail.business.it";
        string userName = "username";
        string password = "password";
        int cdoBasic = 1;
        int cdoSendUsingPort = 2;
        if (userName.Length > 0)
        {
            msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", smtpServer);
            msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 25);
            msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", cdoSendUsingPort);
            msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", cdoBasic);
            msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", userName);
            msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password);
        }
        msg.To = message.Destination;
        msg.From = "me@domain.it";
        msg.Subject = message.Subject;
        msg.BodyFormat = MailFormat.Html;//System.Text.Encoding.UTF8;
        SmtpMail.SmtpServer = smtpServer;
        SmtpMail.Send(msg);

This answer helped me in using System.Web.Mail.

While I had encountered the same issue, I found a solution to the issue here

In short, you must use

smtp.UseDefaultCredentials = false;

AFTER

smtp.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["email.username"].ToString(), 
                                         ConfigurationManager.AppSettings["email.password"].ToString());

So quick summary, something that would look more like this should work:

        SmtpClient smtp = new SmtpClient();
        smtp.Host = ConfigurationManager.AppSettings["email.smtp"].ToString();
        smtp.Port = Convert.ToInt32(ConfigurationManager.AppSettings["email.port"]);
        smtp.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["email.username"].ToString(), 
                                                 ConfigurationManager.AppSettings["email.password"].ToString());
        smtp.UseDefaultCredentials = false;
        smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["email.enablessl"]);
        Console.WriteLine("Sending email..." + (i+1));
        smtp.Send(mail);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!