Atulya Raj

Atulya Raj

  • NA
  • 23
  • 3.3k

Error in Sending Email through Gmail

Jun 19 2020 3:36 PM
I have been trying to send Email in Asp.Net Core 3.1.But I always keep on getting this error System.Net.Mail.SmtpException: Server does not support secure connections.
 
My Source Code-
  1. var fromAddress = new MailAddress("[email protected]""Atulya Raj");  
  2. var toAddress = new MailAddress("[email protected]""Name");  
  3. const string fromPassword = "password";  
  4. const string subject = "Test";  
  5. string body = "Hello"  
  6. var smtp = new SmtpClient  
  7. {  
  8. Host = "smtp.gmail.com",  
  9. Port = 587,  
  10. EnableSsl = true,  
  11. DeliveryMethod = SmtpDeliveryMethod.Network,  
  12. UseDefaultCredentials = false,  
  13. Credentials = new NetworkCredential(fromAddress.Address, fromPassword)  
  14. };  
  15. using (var message = new MailMessage(fromAddress, toAddress)  
  16. {  
  17. Subject = subject,  
  18. Body = body  
  19. })  
  20. {  
  21. smtp.Send(message);  
  22. }  
Any help would be nice.Thanks.

Answers (1)