我遇到了一个奇怪的行为,试图使用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