我正在开发一个ASP.NET Core 2 api,最近我一直在尝试创建类似于大多数网站的“密码重置功能”,通过向该帐户发送电子邮件以更改密码。
所以我用谷歌搜索并写了一些部分有用的东西。部分我的意思是,它在localhost中按预期工作,但我在亚马逊网络服务(AWS)上托管我的应用程序,并且出于某种原因,当它上传时,电子邮件永远不会被发送。 通过这个我的意思是api返回一个内部服务器错误,我不知道如何调试在主机上失败的东西
我知道有亚马逊SES但不幸的是我无法使用它,因为我的api托管在巴西,亚马逊SES在我所在的地区尚不可用。
anyonne是否知道可能出现的问题,甚至是如何调试它?
以下是我目前用来发送电子邮件的代码
基
public interface IEmailService
{
HttpStatusCode SendEmail(string email, string subject, string body);
}
public class EmailService : IEmailService
{
private readonly IConfiguration _config;
public static NetworkCredential credential;
public static SmtpClient client;
public EmailService(IConfigurationRoot c)
{
_config = c;
credential = new NetworkCredential()
{
UserName = _config["EmailSettings:Email"],
Password = _config["EmailSettings:Password"]
};
client = new SmtpClient()
{
Credentials = credential,
Host = _config["EmailSettings:Host"],
Port = int.Parse(_config["EmailSettings:Port"]),
EnableSsl = true
};
}
public HttpStatusCode SendEmail(string email, string subject, string body)
{
using (client)
{
using (var message = new MailMessage())
{
message.To.Add(new MailAddress(email));
message.From = new MailAddress(_config["EmailSettings:Email"]);
message.Subject = subject;
message.Body = body;
message.IsBodyHtml = true;
try
{
client.Send(message);
return HttpStatusCode.OK;
}
catch
{
return HttpStatusCode.InternalServerError;
}
}
}
}
// Body builder, just
public static string PasswordResetBody(string token)
{
StringBuilder body = new StringBuilder();
body.AppendFormat("<h1>My title</h1>");
body.AppendFormat("<br />");
body.AppendFormat("<h3>Passw recovery</h3>");
body.AppendFormat("<br />");
body.AppendFormat("<p>Automated Msg.</p>");
body.AppendFormat("<br />");
body.AppendFormat("<p>Click below</p>");
body.AppendFormat($"<a href=\"https://www.mywebsite.com/account/reset/{token}\">https://www.mywebsite.com/account/reset/{token}</a>");
return body.ToString();
}
}
在控制器上
public HttpStatusCode SendResetPassword(GetStringModel model)
{
...
var sentEmail = emailService.SendEmail(model.Data, "MySubject", EmailService.PasswordResetBody(guid.ToString()));
return sentEmail;
}
异常
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. Learn more at
at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode)
at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at Agrega.MySQL.Services.Database.Services.EmailService.SendEmail(String email, String subject, String body) in C:\Projects\PersonalProjects\Project.Core\Project\Project.MySQL\Services\Database\Services\EmailService.cs:line 55
at Agrega.MySQL.Services.Utilities.Authentication.AuthenticationUtility.SendResetPassword(GetStringModel model) in C:\Projects\PersonalProjects\Project.Core\Project\Project.MySQL\Services\Utilities\Authentication\AuthenticationUtility.cs:line 376
答案 0 :(得分:0)
感谢伐木,我用Google搜索并在另一个上找到答案 stackoverflow question
基本上,似乎因为我使用的是Gmail,Google具有位置安全功能,并且它阻止了电子邮件请求。当我信任该设备并接受其工作的活动时。但是,我不确定这是否是最佳方式。
Tseng必须是正确的,我将在另一个地区调查SES,因为我在AWS免费套餐上,因此机器和IP可以随时更改。谷歌可能会再次阻止他们。