我正在使用简单的CMD“服务”扩展我的Web应用程序,该应用程序应向新注册的用户发送验证电子邮件。我的问题是通过对Gmail帐户进行身份验证,其中引发了以下异常:
“ SMTP服务器需要安全连接,或者客户端未通过身份验证。服务器响应为:5.5.1需要身份验证。”
我已经尝试在自己的IMAP服务器上进行身份验证,但是也没有成功。后来,由于完全依赖于本地配置,我尝试使用XAMP Mercury电子邮件服务器,但这不是最佳解决方案,所以我放弃了这个想法。将来,我只想为该应用创建一个新的Google帐户,因此无需维护。
String body = "<head>" +
"Here comes some logo" +
"</head>" +
"<body>" +
"<h1>Account confirmation reqest.</h1>" + Environment.NewLine +
"<a>Dear User, </a>" + Environment.NewLine +
"<a>In order to be able to use musicshop app properly, we require You to confirm Your email address.</a>" + Environment.NewLine +
"<a>This is the last step towards using our app.</a>" + Environment.NewLine +
"<a>Pleas follow this hyperlink to confirm your address.</a>" + Environment.NewLine +
"<a>[Callback url]</a>" +
"</body>";
try
{
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential()
{
UserName = "[email protected]",
Password = "mypassword"
};
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
smtpClient.Send("[email protected]", "[email protected]", "Account verification", body);
}
catch (Exception ex)
{
}
我只希望能够通过Gmail服务器发送电子邮件,没有任何例外。我是否需要任何NuGet软件包,使用不同的方法?
如果您的Gmail帐户启用了两步验证,则必须创建一个应用专用密码来进行身份验证。
另请注意,SmtpClient是IDisposable
-您应将其放在一个using (var smtpClient = new SmtpClient("smtp.gmail.com", 587)) { ... }
块中,以便SMTP连接RSET,QUIT和正确关闭。
==编辑==
另外,您似乎还打开了from
和recipients
参数smtpClient.Send
。
string body = "<head>" +
"Here comes some logo" +
"</head>" +
"<body>" +
"<h1>Account confirmation reqest.</h1>" + Environment.NewLine +
"<a>Dear User, </a>" + Environment.NewLine +
"<a>In order to be able to use musicshop app properly, we require You to confirm Your email address.</a>" + Environment.NewLine +
"<a>This is the last step towards using our app.</a>" + Environment.NewLine +
"<a>Pleas follow this hyperlink to confirm your address.</a>" + Environment.NewLine +
"<a>[Callback url]</a>" +
"</body>";
try
{
using (var smtpClient = new SmtpClient("smtp.gmail.com", 587))
{
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential()
{
UserName = Config.Username,
Password = Config.Password,
};
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
//Oops: from/recipients switched around here...
//smtpClient.Send("[email protected]", "[email protected]", "Account verification", body);
smtpClient.Send("[email protected]", "[email protected]", "Account verification", body);
}
}
catch (Exception e)
{
Console.Error.WriteLine("{0}: {1}", e.ToString(), e.Message);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句