我正尝试从我的asp.net网站发送电子邮件。
protected void btnSend_Click(object sender, EventArgs e)
{
try
{
//Create Mail Message Object with content that you want to send with mail.
System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage(txtFrom.Text, txtTo.Text, txtSubject.Text, txtMessage.InnerText);
mailMessage.IsBodyHtml = false;
//Proper Authentication Details need to be passed when sending email from gmail
System.Net.NetworkCredential mailAuthentication = new
System.Net.NetworkCredential("my_Email@gmail.com", "my_password");
//Smtp Mail server of Gmail is "smpt.gmail.com" and it uses port no. 587
//For different server like yahoo this details changes and you can
//get it from respective server.
System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
//Enable SSL
mailClient.EnableSsl = true;
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = mailAuthentication;
mailClient.Send(mailMessage);
lblMsg.Text = "success";
}
catch (Exception ex)
{
lblMsg.Text = "Error occured while sending your message." + ex.Message;
}
}
当到达mailClient.Send(mailMessage)时,它不会陷入低谷,而我得到这个错误:“ SMTP服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.5.1需要身份验证。”
我已经将“允许不太安全的应用程序”切换为“开启”,但对我来说仍然无效。