I am developing an ASP.NET Core 2 api and recently I've been trying to create a "password reset function" similar to most websites, by sending an email to the account in order to change the password.
So I've googled and wrote something that partially works. By partially I mean, it works as expected in localhost, but im hosting my application on Amazon Web Services (AWS) and for some reason when it's uploaded the email is never sent. By this i mean that the api returns an internal server error, and I'm not sure how i could debug something that just fails on host
I know there is Amazon SES but unfortunatly I can't use it, since my api is hosted in Brazil and Amazon SES is not avaiable in my region yet.
Does anyonne know what could be going wrong, or even how to debug it?
Here's the code I'm currently using to send emails
Base
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();
}
}
On Controller
public HttpStatusCode SendResetPassword(GetStringModel model)
{
...
var sentEmail = emailService.SendEmail(model.Data, "MySubject", EmailService.PasswordResetBody(guid.ToString()));
return sentEmail;
}
Exception
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