2011-04-05 106 views
0

我正在向我的網絡發送批量電子郵件通知,並且我遇到了使用SMTP的問題。使用C#通過SMTP發送電子郵件時出現問題

問題對於任何電子郵件帳戶,我用它發送約30-40電子郵件,然後我得到以下錯誤:

...郵箱無法使用。服務器的迴應是:這封電子郵件不被接受,請聯繫您的電子郵件主機提供商。

任何想法?

感謝

代號爲Windows服務:

public SMTP() 
    { 
     client = new SmtpClient(host); 

     client.UseDefaultCredentials = false; 
     client.Credentials = new NetworkCredential(_username, _password); 

    } 

    public void Send() 
    { 
     using (EmailEntities ent = new EmailEntities()) 
     { 
      var res = ent.wdj_tbl_EmailQueue.Take(50); 
      if (res != null) 
      { 
       foreach (wdj_tbl_EmailQueue r in res) 
       { 
        try 
        { 
         Int32 id = r.ID; 
         eml = new MailMessage(); 
         try 
         { 
          AddTo((r.emailTo.Replace("\n", "").Trim() ?? "")); 
          AddCc((r.emailCC.Replace("\n", "").Trim() ?? "")); 
          AddBcc((r.emailBCC.Replace("\n", "").Trim() ?? "")); 
         } 
         catch 
         { 
          ent.wdj_tbl_EmailQueue.DeleteObject(r); 
          continue; 
         } 

         eml.From = new MailAddress(r.emailFrom); 
         eml.IsBodyHtml = (r.emailIsHTML ?? true); 
         eml.Subject = r.emailSubject; 

         fixHeaders("To", eml.To); 
         fixHeaders("Cc", eml.CC); 

         if (!eml.IsBodyHtml) 
         { 
          eml.Body = (r.emailTextBody ?? ""); 
         } 
         else 
         { 
          eml.AlternateViews.Add(AlternateView.CreateAlternateViewFromString((r.emailHTMLBody ?? ""), new ContentType("text/html"))); 
          eml.Body = (r.emailTextBody ?? ""); 
          eml.IsBodyHtml = false; 
         } 
         try 
         { 
          client.Send(eml); 
         } 
         catch (Exception ex) 
         { 
          if (!EventLog.SourceExists("WadjaEmailService")) 
           EventLog.CreateEventSource("EmailService", "EmailLog"); 
          EventLog.WriteEntry("EmailService", "The email with id " + id.ToString() + " (" + eml.To + ") was not sent: " + ex.Message, EventLogEntryType.Error); 

          continue; 
         } 
        } 
        catch 
        { 
         ent.wdj_tbl_EmailQueue.DeleteObject(r); 
         continue; 
        } 
        ent.wdj_tbl_EmailQueue.DeleteObject(r); 
       } 
       ent.SaveChanges(); 
      } 
     } 
    } 

    public void AddTo(String email) 
    { 
     //network = GetNetworkName(); 
     string[] emailArray = email.Split(','); 
     if (emailArray.Length > 1) 
     { 
      for (int i = 0; i < emailArray.Length; i++) 
      { 
       eml.To.Add(new MailAddress(emailArray[i])); 
      } 
     } 
     else 
     { 
      eml.To.Add(new MailAddress(email)); 
     } 

    } 

    public void AddTo(String email, String displayName) 
    { 
     eml.To.Add(new MailAddress(email, displayName)); 
    } 

    public void AddCc(String email) 
    { 
     if (String.IsNullOrEmpty(email)) 
      return; 
     string[] emailArray = email.Split(','); 
     if (emailArray.Length > 1) 
     { 
      for (int i = 0; i < emailArray.Length; i++) 
      { 
       eml.CC.Add(new MailAddress(emailArray[i])); 
      } 
     } 
     else 
     { 
      eml.CC.Add(new MailAddress(email)); 
     } 
    } 

    public void AddCc(String email, String displayName) 
    { 
     if (String.IsNullOrEmpty(email)) 
      return; 
     eml.CC.Add(new MailAddress(email, displayName)); 
    } 

    public void AddBcc(String email) 
    { 
     if (String.IsNullOrEmpty(email)) 
      return; 
     string[] emailArray = email.Split(','); 
     if (emailArray.Length > 1) 
     { 
      for (int i = 0; i < emailArray.Length; i++) 
      { 
       eml.Bcc.Add(new MailAddress(emailArray[i])); 
      } 
     } 
     else 
     { 
      eml.Bcc.Add(new MailAddress(email)); 
     } 
    } 

    public void AddBcc(String email, String displayName) 
    { 
     if (String.IsNullOrEmpty(email)) 
      return; 
     eml.Bcc.Add(new MailAddress(email, displayName)); 
    } 

    private void fixHeaders(String hName, MailAddressCollection mac) 
    { 
     String hTmp = ""; 

     foreach (MailAddress ma in mac) 
     { 
      if (!String.IsNullOrEmpty(ma.DisplayName)) 
       hTmp += (", " + ma.DisplayName + "<" + ma.Address + ">"); 
      else 
       hTmp += (", " + ma.Address); 
     } 

     while (hTmp.StartsWith(",")) 
      hTmp = hTmp.Substring(1).Trim(); 

     if (!String.IsNullOrEmpty(hTmp)) 
     { 

      eml.Headers.Add(hName, hTmp); 

     } 
    } 
+3

你使用System.Net.Mail.SmtpClient類嗎?你能發送你的代碼嗎? – 2011-04-05 05:16:32

+0

是的,我已經發布了上面的代碼片段。 – RRG 2011-04-05 14:29:20

回答

1

如果你的代碼能夠發送電子郵件,30-40,然後失敗了,我就先來看看由應用程序產生的任何日誌,以確保它失敗的電子郵件地址在應用程序外部有效且可用。

然後,我會看看服務器日誌(如果可能),試圖隔離它拒絕後來的郵件的原因。

你發送這些消息的速度有多快?是否有可能在服務器上遇到某種限制?

+0

我每天發送約5-8000封電子郵件。我將所有電子郵件存儲在數據庫中,並且我有一個windows服務,可以檢索50個郵件併發送郵件。我正在使用商業SMTP,所以我沒有任何訪問日誌文件e.t.c從我的最後,我正在更新服務器事件查看器,因爲你可以看到上面的代碼,我得到的唯一錯誤是我提到的。 – RRG 2011-04-05 14:38:37

+0

我猜你在哪裏,我改變它每分鐘發送10封電子郵件,現在它工作。 – RRG 2011-04-06 13:27:56

相關問題