简体   繁体   English

使用Mailkit发送邮件的问题(C#)

[英]Issue with sending mails using Mailkit (c#)

I am new to Mailkit/Mimekit and in the process of migrating from Easymail. 我是Mailkit/Mimekit并且正在从Easymail迁移。 The code below gives an error message saying 下面的代码给出了一条错误消息:

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.

This is for a case where where I have used Try Catch statements, if I remove the Try Catch then it gives me the output as 这是针对我使用过Try Catch语句的情况,如果我删除了Try Catch然后将输出显示为

2.0.0 Ok: queued as 3rdP7Y5ZrzzJp60

Which is the expected output as my SMTP server is just fine. 这是预期的输出,因为我的SMTP服务器就可以了。 Why would this be happening? 为什么会这样呢? I am just trying to catch errors where the server doesn't respond 我只是想在服务器没有响应的地方捕获错误

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);             
}

删除此行:

client.Timeout = 20;

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

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