Cannot send mail using SmtpClient

I want to send mail using the SmtpClient class, but it does not work. Code:

SmtpClient smtpClient = new SmtpClient();

smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential(obMailSetting.UserName, obMailSetting.Password);

smtpClient.Host = obMailSetting.HostMail;
smtpClient.Port = obMailSetting.Port;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = obMailSetting.Connect_Security;//true
//smtpClient.UseDefaultCredentials = true;//It would work if i uncomment this line
smtpClient.Send(email);

It throws an exception:

The SMTP server requires a secure connection, or the client failed authentication. Server response: 5.7.1 Client was not authenticated

I am sure that the username and password are correct. Are there any problems in my code? Thanks.

+4
source share
2 answers

You can try the following:

 MailMessage mail = new MailMessage();
 mail.Subject = "Your Subject";
 mail.From = new MailAddress("senderMailAddress");
 mail.To.Add("ReceiverMailAddress");
 mail.Body = "Hello! your mail content goes here...";
 mail.IsBodyHtml = true;

 SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
 smtp.EnableSsl = true;
 NetworkCredential netCre = 
                  new NetworkCredential("SenderMailAddress","SenderPassword" );
 smtp.Credentials = netCre;

 try
  {
     smtp.Send(mail);                
  }
  catch (Exception ex)
  {   
    // Handle exception here 
  }

:
Exchange " " node "-" . " " Recive Connectors ( , 2), TFS ( , ). , "" " " , ( ).



this SmtpClient :

SmtpClient smtp = new SmtpClient("127.0.0.1");
+1

5.7.1. , UseDefaultCredentials true. , NetworkCredential .

, , ? :

new NetworkCredential("MyUserName", "MyPassword", "MyDomain");

, , , , SMTP-, .

+1

All Articles