我遇到了一個奇怪的行爲,試圖使用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
通過驗證程序運行'GlobalHelper.emailBody'返回的HTML。格式錯誤的HTML可能不會在瀏覽器中顯示任何內容。 – 2013-04-11 01:55:37
你的郵件客戶端已更改?身體不能被正確渲染?你有沒有記錄mail.Body屬性,以確保它得到適當的設置? – Jason 2013-04-11 01:55:58
當你說你可以沒有問題地重新發送信息給收到空白郵件的人時,你到底是如何去做的(也就是說,你記錄最初使用的確切信息還是手動生成一條新信息)?你是否驗證'firstname'和'details'(即沒有未轉義的html字符)? 'GlobalHelper.emailBody'在代碼(即屬性vs字段)和典型值方面看起來像什麼?你有沒有收到一封空白郵件的人轉發給你,以便你可以檢查它? – jerry 2013-04-11 02:08:37