Send email with background thread?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



ASP 3.5 VB.NET website.

I call the following function on numerous webpages to send plain-text emails where necessary.

All the pages however 'freeze' so to speak for the web user until the SMTP is connected and the email sent.

How could I create a 'thread' in the following code that sends the email in the background so my users don't have to wait (sometimes up to 30 seconds) for the email to be sent before being taken to the success page? My webhost doesn't allow me to use a pickup directory.


Public Function fSendMailPlainTextOnly( _
ByVal strTo As String, _
ByVal strFrom As String, _
ByVal strSubject As String, _
ByVal strBody As String, _
Optional ByVal strCC As String = "", _
Optional ByVal strBCC As String = "" _
) As String

fSendMailPlainTextOnly = ""
Dim cGeneral As New sfGeneral

'Send mail
Dim mMailMessage As New MailMessage()

mMailMessage.From = New MailAddress(strFrom)
mMailMessage.To.Add(New MailAddress(strTo))

If strBCC <> "" Then
mMailMessage.Bcc.Add(New MailAddress(strBCC))
End If

If strCC <> "" Then
mMailMessage.CC.Add(New MailAddress(strCC))
End If

mMailMessage.Subject = strSubject
mMailMessage.Body = strBody

Try
Dim mSmtpClient As New SmtpClient()
If Left(cGeneral.fGetConnectionString, Len("Data Source=ME-VOSTRO\SQLEXPRESS")) = "Data Source=ME-VOSTRO\SQLEXPRESS" Then
mSmtpClient.Host = "smtp.myhost.com"
Dim SMTPUserInfo As New NetworkCredential("myusername", "mypassword")
mSmtpClient.Credentials = SMTPUserInfo
mSmtpClient.Port = 587
Else
mSmtpClient.Host = "relay-hosting.godaddy-server.net"
End If
mSmtpClient.Send(mMailMessage)
fSendMailPlainTextOnly = "True"
Catch exc As Exception
fSendMailPlainTextOnly = "Email send failure: " + exc.ToString()
End Try

End Function

.



Relevant Pages

  • Search pattern
    ... Dim strfile As String ... Dim bAddressFound As Boolean ... Dim strCurrentChar As String ...
    (comp.databases.ms-access)
  • Auto Write Name and Merge across
    ... Dim Sheetname01 As String ... Dim WeekName01 As String ...
    (microsoft.public.excel.misc)
  • Re: multiplatform (pocketPC & desktopPC) (Daniel !!)
    ... Friend Versione As String ... Public Sub GetMyConnectionPalmare() ... Dim errorMessages As String ... Private Function GetDS_Desktop(ByVal SQL As String) As DataSet ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: multiplatform (pocketPC & desktopPC) (Daniel !!)
    ... Friend Versione As String ... Public Sub GetMyConnectionPalmare() ... Dim errorMessages As String ... Private Function GetDS_Desktop(ByVal SQL As String) As DataSet ...
    (microsoft.public.dotnet.framework.compactframework)
  • Help answer these 70-310 questions
    ... One argument is the string ... Dim output As New StringBuilder ... EmployeeLocations. ... You create a strongly named serviced component. ...
    (microsoft.public.cert.exam.mcsd)