ASP.net MVC Sending Mail

一曲冷凌霜 提交于 2019-12-11 07:56:29

问题


This is my code to send mail (test code):

//sending mail
var message = new System.Net.Mail.MailMessage();
message.From = new MailAddress("J2v@gmail.com");
message.To.Add(model.Mailag);
message.Subject = "Valdation d'inscription";
message.Body = "Votre inscription a été valide voici vos cordonne de conexion ID user : "+model.Idag+" Password : "+user.password;
var client = new  System.Net.Mail.SmtpClient
{
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential("", "")
};
client.Send(message);

When I try it I got this error:

SMTP server require secured connexion or you are not connected. Server response was :5.5.1 Authentication Required. Learn more at

on this line: client.Send(message);


回答1:


Use the following settings when relying to GMAIL:

System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com");
smtp.UseDefaultCredentials = false;
var credentials = System.Net.NetworkCredential(”yourid@gmail.com”, “yourpwd”);
smtp.Credentials = credentials;
smtp.EnableSsl = true;
smtp.Port = 587;



回答2:


I expect you will need to pass in your username, password and a port number when using smtp.gmail.com

Try using

var client = new SmtpClient("smtp.gmail.com");
client.Port = 587;
client.Credentials = new NetworkCredential("accountID", "accountPassword");
client.EnableSsl = true;

client.Send(message);



回答3:


when "SMTP server require secured connectionor you are not connected. Server response was :5.5.1 Authentication Required. Learn more at on this line client.Send(message)" occurs while sending mail means,

you have 2-step verification in your gmail account... remove it and run your code.. it works



来源:https://stackoverflow.com/questions/10044887/asp-net-mvc-sending-mail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!