簡體   English   中英

使用Mailkit發送郵件的問題(C#)

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

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

刪除此行:

client.Timeout = 20;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM