C# SMTP authentication failed but credentials are correct

后端 未结 3 1316
清歌不尽
清歌不尽 2021-01-28 03:20

Here is my problem: I wrote whe following program to test if I can send email:

class Program
{
    static void Main(string[] args)
    {
        try {
                   


        
3条回答
  •  再見小時候
    2021-01-28 03:35

    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);
    

提交回复
热议问题