繁体   English   中英

尝试使用 MailKit 发送时出错 Email “连接尝试失败,因为连接方在一段时间后没有正确响应”

[英]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 发送电子邮件时出现错误。 使用端口 587 和主机是梭子鱼服务器。 Email 仍然设法发送,但在 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:在 System.IO.Stream.Read(跨度 1 缓冲区)在 System.Net.SecurityAdapter] 的 MailKit.Net.NetworkStream.Read(字节 [] 缓冲区,Int32 偏移量,Int32 计数)适配器,Memory 1 缓冲区)在 System.Net.Security.SslStream.Read(字节 [] 缓冲区,Int32 偏移量,Int32 计数)在 MailKit.Net.Smtp.SmtpStream.ReadAheadAsync(布尔型取消令牌.Net同步,取消) Smtp.SmtpStream.ReadResponseAsync(Boolean doAsync, CancellationToken cancellationToken) at MailKit.Net.Smtp.SmtpStream.ReadResponse(CancellationToken cancellationToken) at MailKit.Net.Smtp.SmtpClient.DataAsync(FormatOptions options, MimeMessage message, Int64 size, Boolean doAsync, CancellationToken cancelToken, ITransferProgress 程序 ress) at MailKit.Net.Smtp.SmtpClient.SendAsync(FormatOptions options, MimeMessage message, MailboxAddress sender, IList`1 recipients, Boolean doAsync, CancellationToken cancellationToken, ITransferProgress progress) at MailKit.Net.Smtp.SmtpClient.Send(FormatOptions options, MailKit.MailTransport.Send 处的 MimeMessage 消息、CancellationToken 取消令牌、ITransferProgress 进度)(MimeMessage 消息、CancellationToken 取消令牌、ITransferProgress 进度)

问题很可能是这一行:

client.Timeout = 1000;

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM