5

I am writing a .net core 1.1 console application. The goal is to host this as a scheduled job running inside a docker container.

The console app will use an smtp server and will send emails. Simple as that.

However, I am not successful while running the application inside a docker container. My assumption was that if we expose the smtp port out of the docker container, it should be all that is needed for the smtp server to work.

docker run -it -p 25:25 --rm courierreports:demo

The code is throwing exception while trying to connect to the smtp server. Do i need any additional tools to make my application work?

My machine is running windows 7 and i am using oracle virtual box for docker. The smtp server (org.smtp.com) is provided by my company I am using port 25

The error i get:

fail: StartupLogs[0]
      An error occurred.
fail: StartupLogs[0]
      Unable to read data from the transport connection: Connection timed out.
fail: StartupLogs[0]
         at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
         at MailKit.Net.Smtp.SmtpStream.ReadAhead(CancellationToken cancellationToken)
         at MailKit.Net.Smtp.SmtpStream.ReadResponse(CancellationToken cancellationToken)
         at MailKit.Net.Smtp.SmtpClient.Connect(String host, Int32 port, SecureSocketOptions options, CancellationToken cancellationToken)
         at MailKit.MailService.Connect(String host, Int32 port, Boolean useSsl, CancellationToken cancellationToken)
         at Org.Dept.MailkitAdapter.MailkitSendMail.SendMail(MailMessage mailMsg) in C:\Workspace\POC\DotnetCore\Azure\Practice\EmailService\Org.Dept
.MailkitAdapter\MailkitSendMail.cs:line 31
         at Org.Legal.CourierReports.Logic.EmailProcessor.SendEmail(List`1 model) in C:\Workspace\POC\DotnetCore\Azure\Practice\EmailService\Org.Lega
l.CourierReports.Logic\EmailProcessor.cs:line 121
         at Org.Legal.CourierReports.Logic.EmailProcessor.TestSend() in C:\Workspace\POC\DotnetCore\Azure\Practice\EmailService\Org.Legal.CourierRepo
rts.Logic\EmailProcessor.cs:line 92
         at Org.Legal.CourierReports.Console.Program.Main(String[] args) in C:\Workspace\POC\DotnetCore\Azure\Practice\EmailService\Org.Legal.Courier
Reports.Console\Program.cs:line 42 

Here is my dockerfile

FROM microsoft/dotnet:1.1-runtime
ARG source
WORKDIR /app
COPY ${source:-obj/Docker/publish} .
ENTRYPOINT ["dotnet", "Org.Legal.CourierReports.Console.dll"] 

Here are the commands I used to build & run the docker container

docker build --no-cache -t courierreports:demo .
docker run -it -p 25:25 --rm courierreports:demo

Here is the code. it breaks at the line indicated with =>

using (var client = new SmtpClient())
{
    client.ServerCertificateValidationCallback = (s, c, h, e) => true;

=>  client.Connect(mailMsg.MailOptions.SmtpHost, mailMsg.MailOptions.SmtpPortNoAuth, false);

    client.AuthenticationMechanisms.Remove("XOAUTH2");

    client.Send(mimeMsg);

    client.Disconnect(true);
}
blogs4t
  • 2,329
  • 5
  • 20
  • 33

0 Answers0