سؤال

I'm experiencing a strange behavior trying to send email using Threading.ThreadPool.

This has worked for over a year now but recently it has stated to intermittently send emails with no content. The addressees and subject are all fine, however, the rest of the email is blank. Nothing has changed code wise (apart from Windows updates to the server it runs on).

Here's the code I'm using - does anyone have a suggestion of how I might narrow down where the problem is occurring? Upon re-sending the email to someone who has claimed of receiving a blank email they get it fine - it uses the exact same code as the first one that was sent.

Sub to generate 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'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

Sub that adds 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
هل كانت مفيدة؟

المحلول 2

Well, after fixing the incorrect doc type declaration I've not had a single complaint of blank emails in almost a month. I'm going to assume this fixed the problem all though I'm not certain why.

Thanks for all of your input/help.

نصائح أخرى

I copied from 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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top