尝试通过我的Web服务发送电子邮件时出现错误。我曾尝试启用对安全性较低的应用的访问权限,禁用两步验证并通过网络浏览器登录帐户。 SO上的解决方案都没有为我工作。我仍然得到:如何使用C#通过Gmail发送电子邮件
Error: System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
我能做些什么来解决这个问题?
namespace EmailService
{
public class Service1 : IService1
{
public string SendEmail(string inputEmail, string subject, string body)
{
string returnString = "";
try
{
MailMessage email = new MailMessage();
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
// set up the Gmail server
smtp.EnableSsl = true;
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "mypassword");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
// draft the email
MailAddress fromAddress = new MailAddress("[email protected]");
email.From = fromAddress;
email.To.Add(inputEmail);
email.Subject = body;
email.Body = body;
smtp.Send(email);
returnString = "Success! Please check your e-mail.";
}
catch(Exception ex)
{
returnString = "Error: " + ex.ToString();
}
return returnString;
}
}
}
看看是否有帮助:http://stackoverflow.com/questions/18503333/the-smtp-server-requires-a-secure-connection-o r-the-client-was-not-authenticated – 2015-04-06 04:03:41
你是否尝试过ping服务器,如果是,那么Eghbal Sohrabi提到的解决方案就OK – Shubhojit 2015-04-06 06:55:50
@Shubhojit ping Google服务器? – Johnny 2015-04-06 22:28:35