首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Mailkit超时异常

Mailkit超时异常
EN

Stack Overflow用户
提问于 2021-12-15 07:12:04
回答 2查看 1.4K关注 0票数 1

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

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

TimeoutException:操作超时了。

以下是异常日志;

代码语言:javascript
运行
复制
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路径。

这是我要求发送电子邮件的功能;

代码语言:javascript
运行
复制
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);
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-12-15 14:59:32

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

为了解决这个问题,您需要探索几个途径:

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

Stack Overflow用户

发布于 2021-12-15 08:18:47

你可以用这个。

代码语言:javascript
运行
复制
                    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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70359666

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档