【发布时间】:2022-01-05 20:53:05
【问题描述】:
我正在尝试使用 Mailkit 发送电子邮件,并且在本地(Windows)它工作正常,但在服务器上(ubuntu 20.04)它不是。
我使用 ufw(防火墙)打开了 465 端口,但它仍然无法正常工作。打电话时我得到一个超时*
smtp.Connect(Options.HostAddress, Options.HostPort, Options.HostSecureSocketOptions);
SecureSocket 处于自动状态。
这是我的代码:
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;
}
你知道要尝试什么吗?
如果我使用 Netcat,我可以使用 Telnet 从我的 windows 机器调用端口 465。
感谢您的宝贵时间。 严重
*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 ...
【问题讨论】:
-
如果超时,那么要么是路由问题,要么是防火墙问题。您需要在服务器端检查它是否正在接收初始SYN数据包。同时运行wireshark,看看它是否与你认为你发送的一致。