I am experiencing strange behavior trying to send an email using Threading.ThreadPool .
This has been working for over a year, but recently, he said that he periodically sends emails without content. The recipients and the subject are fine, but the rest of the email is empty. Nothing has changed the code (except for Windows updates to the server on which it runs).
Here's the code I'm using, does anyone have a suggestion on how I can narrow down the issue? After resending the email to someone who claimed to receive an empty letter, they receive everything in order - he uses the same code as the first one sent.
Sub to create email:
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 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
Sub adding to ThreadPool:
Private Sub AddEmailToThreadPool(email As MailMessage) System.Threading.ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback(AddressOf sendEmail), email) End Sub
Sub that sends email:
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
source share