Send email with background thread?


M

Mark B

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
 
Ad

Advertisements

C

Chris Zopers

Hi Mark,

There are several ways to do this, one of it is the following.

Sub CreatingTheThread()

Dim ts As System.Threading.ThreadStart = New
System.Threading.ThreadStart(SendEmail)

Dim t As System.Threading.Thread = New System.Threading.Thread(ts);

t.Start();

End Sub

Sub SendEmail()
Place your e-mail sending code here.
End Sub
 
C

Chris Zopers

This code is supposted to be VB.NET code..;-)..but it's been two years
since I last used VB (I use C# now). But I think this should work this
way. But the important parts are the following:

1) Create an instance of ThreadStart and pass the name of your function
that sends e-mails to the constructor.
2) Create an instance of Thread and pass the ThreadStart instance to the
constructor.
3) Call the Start method of the Thread instance.
 
C

Chris Zopers

Oops, I now see it should be

Dim ts As New System.Threading.ThreadStart()

and so on.....
 
Ad

Advertisements

S

Sreenivas

For all i know, I prefer to delegate to ThreadPool.QueueUserWorkItem
method...
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top