gpt4 book ai didi

asp.net - 使用 System.Threading.ThreadPool 时没有内容的电子邮件

转载 作者:行者123 更新时间:2023-12-01 05:22:00 25 4
gpt4 key购买 nike

我在尝试使用 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("myemail@mydomain.com", "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

添加到 ThreadPool 的 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

最佳答案

我是从 MSDN 复制过来的 MailMessage class

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

关于asp.net - 使用 System.Threading.ThreadPool 时没有内容的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15939378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com