Bị lỗi không gửi mail được trong C#

Mình có làm chức năng gửi mail trong trang Contact của Web, Mail mặc định sẽ gửi vào địa chỉ mail của mình, nhưng khi chạy thì bị lỗi:
System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated.

Đây là đoạn code của mình:

public JsonResult SendMail(Email em)
 {
 string mess;
 string to = "[email protected]";
 string from = em.From;
 MailMessage message = new MailMessage(from, to);
message.Subject = em.Subject;
message.Body = em.Body;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;

SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
NetworkCredential basicCredential1 = new NetworkCredential("[email protected]", "pass");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = basicCredential1;

try
{
client.Send(message);
mess = "Success";
}
catch (Exception e) {
mess = e.ToString();
}

return new JsonResult { Data = mess, JsonRequestBehavior = JsonRequestBehavior.AllowGet};
}

Xin giúp mình khắc phục

1 Like

Không rành C# lắm nhưng thử đưa 2 dòng này lên trên dòng NetworkCredential … thử :smile:

1 Like

Việc thay đổi lên nó cũng không khác gì hết. Cái này chỉ là gán thuộc tính cho biến client thôi mà.

1 Like

Chị thử mấy cái này xem :smile:

http://stackoverflow.com/questions/19111325/the-smtp-server-requires-a-secure-connection-or-the-client-was-not-authenticated

Thử đăng nhập vào tài khoản gmail rồi vào đây:
https://www.google.com/settings/security/lesssecureapps
Chọn lại turn on nếu nó đang off :smile:

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?