【问题标题】:Mailkit Timeout ExceptionMailkit 超时异常
【发布时间】:2022-01-18 09:38:24
【问题描述】:

我正在开发一个小型应用程序,我必须在触发时使用 MailKit 发送电子邮件。我的开发环境是 Windows,生产环境是 Ubuntu 20.04 LTS。

在我的开发环境中,电子邮件发送成功,但是当在我的 linux 机器上部署相同的代码时,它显示

TimeoutException: 操作已超时。

以下是异常日志;

MailKit.Net.SocketUtils.ConnectAsync(string host, int port, IPEndPoint localEndPoint, int timeout, bool doAsync, CancellationToken cancellationToken)
MailKit.MailService.ConnectSocket(string host, int port, bool doAsync, CancellationToken cancellationToken)
MailKit.Net.Smtp.SmtpClient.ConnectAsync(string host, int port, SecureSocketOptions options, bool doAsync, CancellationToken cancellationToken)
MailKit.Net.Smtp.SmtpClient.Connect(string host, int port, SecureSocketOptions options, CancellationToken cancellationToken)
MailKit.MailService.Connect(string host, int port, bool useSsl, CancellationToken cancellationToken)
XYZProject.Controllers.HomeController.TriggerException(string fileType) in D:\DotNet\DotNetCore\XYZProject\XYZProject\Controllers\HomeController.cs
XYZProject.Controllers.HomeController.Index(UploadFormViewModel model) in D:\DotNet\DotNetCore\XYZProject\XYZProject\Controllers\HomeController.cs
lambda_method35(Closure , object , object[] )
Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor+SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, object controller, object[] arguments)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

这里要注意一点,为什么我在 linux 机器上获取我的 windows 路径。

这是我为发送电子邮件而调用的函数;

private void TriggerException(string fileType)
{
    int port = Convert.ToInt32(_configuration["AlarmTrigger:Emails:Credentials:SmtpPort"]);
    string host = _configuration["AlarmTrigger:Emails:Credentials:SmtpServer"];
    string username = _configuration["AlarmTrigger:Emails:Credentials:User"];
    string password = _configuration["AlarmTrigger:Emails:Credentials:Password"];

    MailboxAddress mailFrom = new MailboxAddress(_configuration["AlarmTrigger:Emails:From:Name"],
        _configuration["AlarmTrigger:Emails:From:Email"]);

    MailboxAddress mailTo = new MailboxAddress(_configuration["AlarmTrigger:Emails:To:Name"], 
        _configuration["AlarmTrigger:Emails:To:Email"]);

    string mailTitle = "Failure Notification";
    string mailMessage = "Dear Team, \n\r" + fileType + " file upload failed.";

    var message = new MimeMessage();
    message.From.Add(mailFrom);
    message.ReplyTo.Add(mailFrom);
    message.To.Add(mailTo);
    message.Subject = mailTitle;
    message.Body = new TextPart("plain") { Text = mailMessage };

    using (var client = new SmtpClient())
    {
        //client.LocalDomain = "localhost";
        client.Connect(host, port, true);
        client.Authenticate(username, password);

        client.Send(message);
        client.Disconnect(true);
    }
}

【问题讨论】:

    标签: c# .net-core mailkit


    【解决方案1】:

    如果/当套接字在配置的时间跨度(默认为 2 分钟)内无法连接到指定端口上的远程主机时,套接字将在 Connect 调用期间获得超时异常。

    为了解决这个问题,您有几个途径可以探索:

    1. 您可以尝试通过设置SmtpClient.Timeout 值来增加超时。
    2. 验证您使用的主机名和端口是否正确。
    3. 验证您的生产环境没有阻止通过防火墙或病毒扫描程序在您的程序尝试使用的端口上与邮件服务器的传出连接。
    4. 通过他们的防火墙验证 ISP 或其他没有阻止您的生产服务器的传出连接。
    5. 验证生产环境的路由表是否正确,并且存在到 SMTP 服务器的有效网络路由。

    【讨论】:

    • 感谢@jstedfast 指导我解决这个问题。实际上,它不是使用端口 465,而是使用 587。将 client.Connect(host, 465, true) 更改为 client.Connect(host, 587, false)
    【解决方案2】:

    你可以用这个。

                        MailMessage mail = new MailMessage();
                        SmtpClient SmtpServer = new SmtpClient("smtp.live.com");
                        mail.From = new MailAddress("username@hotmail.com");
                        mail.To.Add("Mail Adress");
                        mail.Subject = "Your Subject";
                        mail.Body = "This is for testing SMTP mail from GMAIL";
                        SmtpServer.Port = 587;
                        SmtpServer.Credentials = new System.Net.NetworkCredential("username@ghotmail.com", "Your PAssword");
                        SmtpServer.EnableSsl = true;
                        SmtpServer.Send(mail);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-28
      • 2015-11-29
      • 2013-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多