2016-08-12 79 views
0

我有一個奇怪的問題,但不知道該用它來做什麼,有時 - 套接字錯誤10054從電子郵件服務器,SMTP客戶端

我有準備一些數據的.NET控制檯應用程序和FTP的過來 後操作完成它應該發出一封確認電子郵件 - 只有發生的情況是,有時電子郵件被接收,有時不會

應用程序在SQL工作代理程序中使用sql server 2014運行在該工作臺上2014 該應用程序在同一臺服務器上作爲數據庫,所有者是sa並且作業在SQL Server代理下運行 - SQLSERVERAGENT對文件夾具有所有權利

使用郵件服務器的MDaemon,對服務器日誌檢查它表明:

Fri 2016-08-12 06:28:50.387: 01: ---------- 
Fri 2016-08-12 06:31:24.154: 05: Session 834167; child 0001 
Fri 2016-08-12 06:31:24.154: 05: Accepting SMTP connection from xxx to xxx 
Fri 2016-08-12 06:31:24.164: 03: --> xxx ESMTP MDaemon 16.0.4; Fri, 12 Aug 2016 06:31:24 +1000 
Fri 2016-08-12 06:31:24.164: 03: --> 220-Unauthorized relay prohibited. 
Fri 2016-08-12 06:31:24.164: 03: --> 220 All transactions and IP are logged. 
Fri 2016-08-12 06:31:24.167: 02: <-- EHLO OCSQL2 
Fri 2016-08-12 06:31:24.168: 03: --> xxx Hello OCSQL2 [10.10.45.34], pleased to meet you 
Fri 2016-08-12 06:31:24.168: 03: --> 250-ETRN 
Fri 2016-08-12 06:31:24.168: 03: --> 250-AUTH LOGIN CRAM-MD5 PLAIN 
Fri 2016-08-12 06:31:24.168: 03: --> 250-8BITMIME 
Fri 2016-08-12 06:31:24.168: 03: --> 250-ENHANCEDSTATUSCODES 
Fri 2016-08-12 06:31:24.168: 03: --> 250-STARTTLS 
Fri 2016-08-12 06:31:24.168: 03: --> 250 SIZE 
Fri 2016-08-12 06:31:24.168: 02: <-- MAIL FROM:<xxx> 
Fri 2016-08-12 06:31:24.170: 01: xxx is an alias for xxx 
Fri 2016-08-12 06:31:24.170: 03: --> 250 2.1.0 Sender OK 
Fri 2016-08-12 06:31:24.170: 02: <-- RCPT TO:<xxx> 
Fri 2016-08-12 06:31:24.175: 03: --> 250 2.1.5 Recipient OK 
Fri 2016-08-12 06:31:24.175: 02: <-- RCPT TO:<xxx> 
Fri 2016-08-12 06:31:24.265: 03: --> 250 2.1.5 Recipient OK 
Fri 2016-08-12 06:31:24.266: 02: <-- RCPT TO:<xxx> 
Fri 2016-08-12 06:31:24.270: 03: --> 250 2.1.5 Recipient OK 
Fri 2016-08-12 06:31:24.270: 02: <-- RCPT TO:<xxx> 
Fri 2016-08-12 06:31:24.275: 03: --> 250 2.1.5 Recipient OK 
Fri 2016-08-12 06:31:24.276: 02: <-- RCPT TO:<xxx> 
Fri 2016-08-12 06:31:24.281: 03: --> 250 2.1.5 Recipient OK 
Fri 2016-08-12 06:31:24.281: 02: <-- RCPT TO:<xxx> 
Fri 2016-08-12 06:31:24.286: 03: --> 250 2.1.5 Recipient OK 
Fri 2016-08-12 06:31:24.286: 02: <-- DATA 
Fri 2016-08-12 06:31:24.290: 01: Creating temp file (SMTP): e:\mdaemon\queues\temp\md50000008212.tmp 
Fri 2016-08-12 06:31:24.290: 03: --> 354 Enter mail, end with <CRLF>.<CRLF> 
Fri 2016-08-12 06:31:24.295: 01: Message size: 552 bytes 
Fri 2016-08-12 06:31:24.323: 04: Socket error sending response to DATA 
Fri 2016-08-12 06:31:24.324: 04: * Socket error 10054 - Connection was reset by the other side! 
Fri 2016-08-12 06:31:24.324: 04: SMTP session terminated (Bytes in/out: 841/540) 

代碼中的應用:

public bool sendUploadNotification() { 
     var arrToEmail = toEmail.Split(';'); 
     var arrCCEmail = ccEmail.Split(';'); 
     MailMessage msg = new MailMessage(); 
     string body; 
     string subject; 
     bool result = false; 

     if (logger.IsDebugEnabled) { logger.Debug("sending upload email"); } 

     try { 
      body = ReadBody(); 
      subject = GetSubject(); 


      if (logger.IsDebugEnabled) { 
       logger.Debug("ToEmail:" + toEmail + "," + "CCEmail:" + ccEmail + ",Subject:" + subject); 
      } 

      msg.IsBodyHtml = true; 
      msg.BodyEncoding = Encoding.UTF8; 
      msg.SubjectEncoding = Encoding.UTF8; 
      msg.From = new MailAddress(fromEmail,fromName); 
      msg.Subject = subject; 
      msg.Body = body;   


      foreach (var item in arrToEmail) { 
       msg.To.Add(new MailAddress(item)); 
      } 
      foreach (var item in arrCCEmail) { 
       msg.CC.Add(new MailAddress(item)); 
      } 

      client.Send(msg); 
      result = true; 

      if (logger.IsInfoEnabled) { 
       logger.Info("Upload confirmation email sent"); 
      } 
     } 
     catch (Exception ex) { 
      if (logger.IsErrorEnabled) { 
       logger.Error("Error sending upload confirmation email", ex); 
      } 
      sendExceptionMail(ex); 
      result = false; 
     } 

     return result; 

    } 

我沒有得到任何異常的應用程序日誌,一切都成功完成。

服務器有一個發送電子郵件不受SQL代理觸發其他.NET模塊這似乎並不存在這個問題,但是其他模塊,它們被稱爲在運行時從網站

這個問題似乎隨機發生,大多數時候它不工作,有時我已經收到電子郵件按預期,沒有任何應用程序或服務器的變化

我不明白爲什麼它失敗,搜索谷歌出現有些問題與Windows 2008服務器有關的TCP/IP堆棧,但我在Windows服務器2012 R2 - 我可以想到的唯一的事情,如果它可能必須做的權限,但它有效

回答

0

昨天我有同樣的情況。首先,您需要確保電子郵件地址格式正確。相信我或不會讓你成爲一個問題。

Using System.ComponentModel; 

foreach (var item in arrToEmail) 
{ 
    if (RegularExpressions.Regex.IsMatch(Strings.Trim(Sender), "^\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$")) 
    { 
     msg.To.Add(new MailAddress(item)); 
    } 
} 
foreach (var item in arrCCEmail) 
{ 
    if (RegularExpressions.Regex.IsMatch(Strings.Trim(Sender), "^\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$")) 
    { 
     msg.CC.Add(new MailAddress(item)); 
    } 
} 

其次,將發件人添加到您的郵件構建中。 MDaemon非常棘手和敏感。

msg.Sender = New MailAddress(credentialUser) 

第三,不要關閉應用程序太快或處置您的「客戶端」變量(假設是SMTPClient對象)。如果連接配置過快,MDaemon認爲第三方取消操作,無論郵件結構是否在MDaemon服務器上。

可選,您可以在應用程序中使用Async Send Mail。這會給你一個確認和一個電子郵件被髮送(或失敗)的正確時間。

例如:

client.send(msg);在你的代碼,你可以補充一點:

// Set the method that is called back when the send operation ends. 

    smtp.SendCompleted += SendCompletedCallback; 

    string userState = "Email Token or any text here to identify your Operation"; 
    //This line will replace "client.send(msg)" code 
    smtp.SendAsync(msg, userState); 
在你的代碼

某處添加此委託:

private void SendCompletedCallback(object sender, AsyncCompletedEventArgs e) { 
    // Get the unique identifier for this asynchronous operation. 
    string token = e.UserState.ToString(); 
    if (e.Cancelled) { 
     Console.WriteLine("[{0}] Send canceled.", token); 
    } 

    if (e.Error != null) { 
     Console.WriteLine("[{0}] {1}", token, e.Error.ToString()); 
    } 
    else { 
     Console.WriteLine("Message sent."); 
    }  
} 

希望它能幫助!

+0

謝謝,我會嘗試它,當我可以安排更改 – Danish

相關問題