【问题标题】:Issue with sending mails using Mailkit (c#)使用 Mailkit (c#) 发送邮件的问题
【发布时间】:2016-06-27 09:43:46
【问题描述】:

我是Mailkit/Mimekit 的新手,正在从 Easymail 迁移。 下面的代码给出了一条错误消息,说

Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

这是针对我使用了 Try Catch 语句的情况,如果我删除 Try Catch,那么它会给我的输出为

2.0.0 Ok: queued as 3rdP7Y5ZrzzJp60

这是预期的输出,因为我的 SMTP 服务器很好。 为什么会发生这种情况?我只是想捕捉服务器没有响应的错误

string ReturnEmail = "returnpath@xxxxxxx";
string ReturnName  = "Return xxxxxxx";
string FromEmail   = "xxxxxxx@xxxxxxx.com";
string FromName    = "xxxxxxx";
string SenderEmail = "xxxxxxx@xxxxxxx.com";
string SenderName  = "xxxxxxx";      
string TextBody    = @"Text Body";
string HtmlBody = string.Format(@"HTML Body");
string Subject = "Retrouvez-nous à la e à l’occasion de la Coupe du Monde de Combiné Nordique";
string ToName = "XXX";
string ToEmail = "XX@XXX.com";
string MailServer = "XX.XXX.XX.XXX";
int Result = 0;
var message = new MimeMessage();

MailboxAddress RecepientAddress = new MailboxAddress(ToName, ToEmail);
message.From.Add(new MailboxAddress(FromName, FromEmail));

var builder = new BodyBuilder();

builder.TextBody = TextBody;
builder.HtmlBody = HtmlBody;

List<MailboxAddress> To = new List<MailboxAddress>();
To.Add(RecepientAddress);

message.Subject = Subject;
message.Body = builder.ToMessageBody();
message.Sender = new MailboxAddress(SenderName, SenderEmail);  // Sender Address
message.To.Add (RecepientAddress);    
var client = new SmtpClient( new ProtocolLogger("smtp.txt") ); // logging SMTP connections
client.Timeout = 20;

// client.Connect(MailServer, 25);
try
{
    client.Connect(MailServer, 25);
}
catch (SmtpCommandException ex)
{
    Console.WriteLine("Error trying to connect: {0}", ex.Message);
    Console.WriteLine("\tStatusCode: {0}", ex.StatusCode);               
}
catch (SmtpProtocolException ex)
{
    Console.WriteLine("Protocol error while trying to connect: {0}", ex.Message);             
}
catch (Exception ex)
{
   Console.WriteLine(ex.Message);             
}

【问题讨论】:

  • 为什么将客户端超时设置为 20 毫秒?
  • 我只是想设置一个默认超时,也许我应该将它增加到 2000?。
  • 已经有合理的默认设置了,不需要设置。
  • 我建议您保留带有“.log”扩展名的日志。

标签: c# mailkit mimekit


【解决方案1】:

删除这一行:

client.Timeout = 20;

【讨论】:

    猜你喜欢
    • 2020-11-05
    • 2020-04-19
    • 2015-09-21
    • 1970-01-01
    • 2023-03-18
    • 2017-02-17
    • 1970-01-01
    • 2018-12-07
    • 2015-06-07
    相关资源
    最近更新 更多