question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SMTP send throws AuthenticationException only from within Docker container

See original GitHub issue

Describe 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:

  1. Create a free account on mailjet.com
  2. Clone this test repo that I created showing the problem: https://github.com/kspearrin/dockersmtptest
  3. 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
  1. 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
  1. 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:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kspearrincommented, Jan 7, 2019

@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.

0reactions
kspearrincommented, Jan 7, 2019
Read more comments on GitHub >

github_iconTop Results From Across the Web

Sending mail from inside docker container - smtp
One of the things I want my Mirth to do for me is to send an email (SMTP sender) from inside the container...
Read more >
Common Errors When Sending Email With Mailkit
Incorrect SMTP Host ... This one is an easy fix. It means that you have used an incorrect SMTP hostname (Usually just a...
Read more >
email - Cannot send mail from inside docker container
I have a spring mvc application which I have dockerized. See this link the way I dockerized. Before containerization the ordinary war deployed ......
Read more >
Can't connect to self-hosted SMTP - Password Manager
AuthenticationException : The remote certificate is invalid according to the validation procedure. Connecting to it over the internet without SSL ...
Read more >
How to Fix and Debug Docker Containers Like a Superhero
Use the docker run --rm -it --name MYCONTAINER [IMAGE] bash command to open an interactive terminal within your container. Take the container's default...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found