SMTP send throws AuthenticationException only from within Docker container
See original GitHub issueDescribe the bug
I understand that SMTP mail sending is not really a ASP.NET thing, however, this issue seems to surface due the application running in a docker container built from the ASP.NET Core image, so I figured it was worthy of being opened as an issue in this repo. Please let me know if I should open elsewhere.
Certain SMTP mail servers (in this case, mailjet.com) will throw an AuthenticationException
when trying to send mail from an ASP.NET Core application that is running in an ASP.NET Core Docker container.
For example:
var client = new SmtpClient(_configuration.GetValue<string>("smtp:host"),
_configuration.GetValue<int>("smtp:port"))
{
EnableSsl = _configuration.GetValue<bool>("smtp:ssl"),
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(_configuration.GetValue<string>("smtp:username"),
_configuration.GetValue<string>("smtp:password"))
};
var message = new MailMessage
{
From = new MailAddress(_configuration.GetValue<string>("smtp:from")),
Subject = "Testing SMTP Email",
SubjectEncoding = Encoding.UTF8,
BodyEncoding = Encoding.UTF8,
BodyTransferEncoding = System.Net.Mime.TransferEncoding.QuotedPrintable,
Body = "<p>Hi, This is a test email.</p>",
IsBodyHtml = true
};
message.To.Add(new MailAddress(_configuration.GetValue<string>("smtp:to")));
client.SendCompleted += (sender, e) =>
{
if(e.Error != null)
{
_logger.LogError(e.Error, "SendCompleted Error");
}
if(e.Cancelled)
{
_logger.LogError("SendCompleted Cancelled");
}
message.Dispose();
client.Dispose();
};
client.SendAsync(message, null);
Using my SMTP settings provided by mailjet.com, I can send email just fine from this application running locally (Windows 10) or when the application deployed to Azure via Visual Studio “Publish”.
{
"Smtp": {
"Host": "in-v3.mailjet.com",
"Port": 587,
"Ssl": true,
"Username": "myusername",
"Password": "mypassword",
"From": "myapp@example.com",
"To": "me@example.com"
}
}
However, if I build this application into a Docker image (smtp appsettings provided via --env-file
), it fails to send mail with an AuthenticationException
.
System.Net.Mail.SmtpException: Failure sending mail. —> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.
I can change my mail settings to another server, such as SendGrid, and it works fine both on my local machine, Azure, and in the Docker container.
It seems like this is a problem with a missing CA for mailjet’s SSL cert, but I can see the DigiCert CA inside the Docker container if I bash in and look.
I don’t understand why it works in other environments, but not this Docker container.
To Reproduce
Steps to reproduce the behavior:
- Create a free account on mailjet.com
- Clone this test repo that I created showing the problem: https://github.com/kspearrin/dockersmtptest
- Create an env-file for your appsettings. Example:
smtp__host=in-v3.mailjet.com
smtp__port=587
smtp__ssl=true
smtp__username=myusername
smtp__password=mypassword
smtp__to=myapp@example.com
smtp__from=me@example.com
logDirectory=/app/logs
- Build the docker image and run it:
docker build --no-cache -t dockersmtptest ./
docker run -d --env-file=/path/to/appsettings.env -p 5012:80 -v /path/to/logs:/app/logs dockersmtptest
- Visit http://localhost:5012/api/mail to try and send the email
If you mapped the logs directory volume, you should find a log file with the exceptions since Serilog is configured to write to a rolling file.
Expected behavior
Should be able to send mail using mailjet.com from application running inside ASP.NET Core docker container just like I can when running it locally.
Screenshots
n/a
Additional context
n/a
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (3 by maintainers)
@Eilon I just tried this with dotnet core on Linux (Ubuntu, no docker) and the same issue occurs. So it would appear that is isn’t really an issue with the Docker images. I will re-open the issue on corefx. Thanks.
For reference: https://github.com/dotnet/corefx/issues/34407