【问题标题】:Error trying to use MailKit to send Email "A connection attempt failed because the connected party did not properly respond after a period of time"尝试使用 MailKit 发送电子邮件时出错“连接尝试失败,因为连接方在一段时间后没有正确响应”
【发布时间】:2021-07-21 03:20:55
【问题描述】:

当我尝试使用 MailKit 发送电子邮件时出现错误。使用端口 587 和主机是梭子鱼服务器。电子邮件仍然可以发送,但在 client.Send(mail) 处中断,并在下面给出此错误。

有谁知道我错过了什么或做错了什么?如果需要,我可以提供更多代码。

发送邮件代码:

                       client.Connect(_SMTPOptions.Host, _SMTPOptions.Port, SecureSocketOptions.StartTls);
                       client.Timeout = 1000;
                       client.Send(mail);
                       client.Disconnect(true);

添加附件代码(如果这很重要):

                       bytes = bytes + document.Bytes.Length;
                       Stream stream = new MemoryStream(document.Bytes);
                       streamlist.Add(stream);
                       var attachment = new MimePart("application","pdf")
                       {
                           Content = new MimeContent(stream, ContentEncoding.Default),
                           ContentDisposition = new MimeKit.ContentDisposition(MimeKit.ContentDisposition.Attachment),
                           ContentTransferEncoding = ContentEncoding.Base64,
                           FileName = Path.GetFileName(order.OrderNumber + "_" + document.DocumentType + "_" + document.IntDocID + ".pdf")
                       };

                       builder.Attachments.Add(attachment);

错误信息:

连接尝试失败,因为连接方在一段时间后没有正确响应,或者连接失败,因为连接的主机没有响应。

堆栈跟踪:

StackTrace:在 MailKit.Net.NetworkStream.Read(Byte[] 缓冲区,Int32 偏移量,Int32 计数) 在 System.IO.Stream.Read(跨度 1 缓冲区) 在 System.Net.Security.SslStream.ReadAsyncInternal[TIOAdapter](TIOAdapter 适配器,内存 1 缓冲区) 在 System.Net.Security.SslStream.Read(字节 [] 缓冲区,Int32 偏移量,Int32 计数) 在 MailKit.Net.Smtp.SmtpStream.ReadAheadAsync(布尔 doAsync,CancellationToken cancelToken) 在 MailKit.Net.Smtp.SmtpStream.ReadResponseAsync(布尔 doAsync,CancellationToken cancelToken) 在 MailKit.Net.Smtp.SmtpStream.ReadResponse(CancellationToken cancelToken) 在 MailKit.Net.Smtp.SmtpClient.DataAsync(FormatOptions 选项,MimeMessage 消息,Int64 大小,布尔 doAsync,CancellationToken cancelToken,ITransferProgress 进度) 在 MailKit.Net.Smtp.SmtpClient.SendAsync(FormatOptions 选项,MimeMessage 消息,MailboxAddress 发件人,IList`1 收件人,布尔 doAsync,CancellationToken cancelToken,ITransferProgress 进度) 在 MailKit.Net.Smtp.SmtpClient.Send(FormatOptions 选项,MimeMessage 消息,CancellationToken cancelToken,ITransferProgress 进度) 在 MailKit.MailTransport.Send(MimeMessage 消息,CancellationToken cancelToken,ITransferProgress 进度)

【问题讨论】:

    标签: c# .net-core mailkit


    【解决方案1】:

    问题很可能出在这一行:

    client.Timeout = 1000;
    

    如果 MailKit 在该时间段内无法读取(或写入)套接字,则您告诉 MailKit 在 1000 毫秒(也称为 1 秒)后抛出异常。

    MailKit 使用的默认超时时间为 2 分钟 (2 * 60 * 1000)。

    【讨论】:

    • 我也注意到了这一点并将其删除。不幸的是,仍然遇到同样的错误。
    • 尝试获取protocol log 并查看DATA 命令的最后几行。最后一行看起来像C: . 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-13
    • 2016-08-30
    • 2020-06-19
    • 2014-01-07
    • 1970-01-01
    • 2011-10-29
    相关资源
    最近更新 更多