Reputation: 120
I am trying to send en email with Mailkit and locally(windows) it is working fine but on the server(ubuntu 20.04) it is not.
I opend the port 465 using ufw(firewall) but it is still not working. I get an Timout* when calling
smtp.Connect(Options.HostAddress, Options.HostPort, Options.HostSecureSocketOptions);
The SecureSocket is on Auto.
Here my Code:
private bool Execute(Message message)
{
// create message
var builder = new BodyBuilder { HtmlBody = message.GetMessageAsString() };
message.Attachments.ToList().ForEach(x => builder.Attachments.Add(x.Filename, x.Data, x.ContentType));
var email = new MimeMessage
{
Sender = MailboxAddress.Parse(Options.SenderEmail),
Subject = message.Subject,
Body = builder.ToMessageBody()
};
if (!string.IsNullOrEmpty(Options.SenderName))
{
email.Sender.Name = Options.SenderName;
}
email.From.Add(email.Sender);
email.To.Add(MailboxAddress.Parse(message.EmailAddress));
// send email
using (var smtp = new SmtpClient())
{
smtp.Connect(Options.HostAddress, Options.HostPort, Options.HostSecureSocketOptions);
smtp.Authenticate(Options.HostUsername, Options.HostPassword);
smtp.Send(email);
smtp.Disconnect(true);
}
return true;
}
Do you have any idea what to try.
Ps if I use Netcat I can call the port 465 from my windows machine with Telnet.
Thanks for your time. Severin
*System.TimeoutException: The operation has timed out.
at MailKit.Net.SocketUtils.ConnectAsync(String host, Int32 port, IPEndPoint localEndPoint, Int32 timeout, Boolean doAsync, CancellationToken cancellationToken)
at MailKit.MailService.ConnectSocket(String host, Int32 port, Boolean doAsync, CancellationToken cancellationToken)
at MailKit.Net.Smtp.SmtpClient.ConnectAsync(String host, Int32 port, SecureSocketOptions options, Boolean doAsync, CancellationToken cancellationToken)
at MailKit.Net.Smtp.SmtpClient.Connect(String host, Int32 port, SecureSocketOptions options, CancellationToken cancellationToken)
at
Test.Smtp.Services.TestService.Execute(Message message) in ...
at Test.Smtp.Services.TestService.SendEmail(Message message) in ...
Upvotes: 1
Views: 1089
Reputation: 120
It wasn't a problem with the firewall. It was with IP 6 which wasnt correctly configrued.
My solution for the moment was just disable IP 6 and then it worked.
I used these two commands:
sudo sysctl -w net.ipv6.conf.default.disable_ipv6=1
sudo sysctl -w net.ipv6.conf.all.disable_ipv6=1
Cheers, Severin
Upvotes: 1
Reputation: 38538
Netcat works on your Windows machine for the same reason that MailKit works on your Windows machine.
Your Windows machine has an unblock route to the mail server.
Your Ubuntu server, on the other hand, does not have an unblocked route to the mail server, so it fails to connect there.
You need to figure out your firewall and/or routing tables on your Ubuntu server and fix that to allow connecting to the mail server.
Upvotes: 0