2013-04-11 53 views
4

我遇到了一個奇怪的行爲,試圖使用Threading.ThreadPool發送電子郵件。使用System.Threading.ThreadPool時沒有內容的電子郵件

這已經工作了一年多了,但最近它已經聲明要間歇地發送沒有內容的電子郵件。收件人和主題都很好,但是,其餘的電子郵件是空白的。沒有什麼改變了代碼的明智之處(除了它運行的服務器的Windows更新)。

下面是我使用的代碼 - 有沒有人有我如何可以縮小問題發生的位置的建議?在將電子郵件重新發送給聲稱收到空白電子郵件的人時,他們可以很好地使用它 - 它使用與發送的第一個完全相同的代碼。

子生成電子郵件:

Public Sub EmailConfirmation(email As String, 
           firstname As String, 
           details As String) 

     Try 


      Dim embody As String = GlobalHelper.emailBody 
      'static class which loads the email text at application load for use later. 
      'This is a point of concern because it's obviously where the embody text is 
      'is loaded, but the issue is intermittent and if there was a failure here surely 
      'it would be caught by the 'try...catch' and a log of the error created (which has 
      'never happened). I also ran an experiment for a while where if this string was 
      'empty then create a log entry. After receiving a complaint of a blank email 
      'no error log was found. 

      embody = Replace(embody, "[FirstName]", firstname) 
      embody = Replace(embody, "[DATA]", details) 


      'create the mail message 
      Dim mail As New MailMessage() 

      'set the addresses 
      mail.From = New MailAddress("[email protected]", "My Display Name") 
      mail.To.Add(email) 
      mail.IsBodyHtml = True 

      'set the content 
      mail.Subject = "Email Subject!" 
      mail.Body = embody 


      AddEmailToThreadPool(mail) 

     Catch ex As Exception 

     'if there is an error it is logged here. 

     End Try 


    End Sub 

分,增加了線程池:

Private Sub AddEmailToThreadPool(email As MailMessage) 
     System.Threading.ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback(AddressOf sendEmail), email) 
    End Sub 

子發送電子郵件:

Private Sub sendEmail(stateinfo As Object) 

     Dim email As MailMessage = CType(stateinfo, MailMessage) 


     Try 

      'send the message 
      Dim smtp As New SmtpClient("mail.mydomain.com") 
      smtp.Send(email) 
     Catch ex As Exception 
      'error is logged here. 
     End Try 

    End Sub 
+1

通過驗證程序運行'GlobalHelper.emailBody'返回的HTML。格式錯誤的HTML可能不會在瀏覽器中顯示任何內容。 – 2013-04-11 01:55:37

+0

你的郵件客戶端已更改?身體不能被正確渲染?你有沒有記錄mail.Body屬性,以確保它得到適當的設置? – Jason 2013-04-11 01:55:58

+1

當你說你可以沒有問題地重新發送信息給收到空白郵件的人時,你到底是如何去做的(也就是說,你記錄最初使用的確切信息還是手動生成一條新信息)?你是否驗證'firstname'和'details'(即沒有未轉義的html字符)? 'GlobalHelper.emailBody'在代碼(即屬性vs字段)和典型值方面看起來像什麼?你有沒有收到一封空白郵件的人轉發給你,以便你可以檢查它? – jerry 2013-04-11 02:08:37

回答

0

那麼,修復不正確的doc類型聲明後,我幾乎沒有在一個月內對空白電子郵件進行過一次投訴。雖然我不確定爲什麼,但我會假設這可以解決所有問題。

感謝您的所有輸入/幫助。

+1

很高興你似乎解決了你的問題。奇怪的是,用戶能夠看到它一次,但不是另一次,但是,如果它是由無效的HTML引起的。您是否碰巧知道該用戶是否使用不同的電子郵件客戶端,瀏覽器或計算機來查看這兩條消息? – jerry 2013-05-09 16:37:05

+0

可能,我想可能是這種情況,他們沒有在那裏顯示郵件客戶端,但會顯示在他們的手機上。但通過第三方處理這些問題使得提問/獲得答案變得困難。我會得到的唯一回應是「現在正在工作」。公平地說,我並不完全相信這個問題已經解決,我認爲格式不正確的文檔類型會導致這種行爲,會等着瞧。 – GJKH 2013-05-09 16:44:26

1

我從MSDN複製MailMessage class

此類型的任何公共靜態(Visual Basic中的Shared)成員都是線程安全的。任何實例成員不保證是線程安全的。

+0

我認爲你在做某件事; @GJKH,可以通過多線程同時調用'GlobalHelper.emailBody'嗎? – 2013-04-11 16:40:52

+0

只需設置一個測試控制檯應用程序,它以循環方式將一百萬個這樣的項目添加到線程池中,並在該循環內添加了另外10個項目到池中。我的筆記本電腦變熱了,但從未拋棄過一次。我查看了電子郵件正文是否爲空,從未接收過一次。 – GJKH 2013-04-11 17:13:22

+0

如果你暗示'MailMessage'對象不能傳遞給'QueueUserWorkItem',那是不正確的。事件成員不是線程安全的(.NET Framework類庫中的常見警告)意味着您不應該同時從多個線程訪問同一個MailMessage對象。代碼似乎無法做到這一點,因爲每個'EmailConfirmation'調用實例化一個新的MailMessage對象,將它傳遞給AddEmailToThreadPool,然後再次不訪問它(可能的錯誤記錄放在一邊)。 – jerry 2013-04-11 20:09:37