Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Send email with background thread?

Reply
Thread Tools

Send email with background thread?

 
 
Mark B
Guest
Posts: n/a
 
      06-09-2009
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

 
Reply With Quote
 
 
 
 
Chris Zopers
Guest
Posts: n/a
 
      06-09-2009
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

*** Sent via Developersdex http://www.developersdex.com ***
 
Reply With Quote
 
 
 
 
Mark B
Guest
Posts: n/a
 
      06-09-2009
Thanks. Got VB.NET for me?

"Chris Zopers" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
> *** Sent via Developersdex http://www.developersdex.com ***


 
Reply With Quote
 
Chris Zopers
Guest
Posts: n/a
 
      06-09-2009
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.

*** Sent via Developersdex http://www.developersdex.com ***
 
Reply With Quote
 
Chris Zopers
Guest
Posts: n/a
 
      06-09-2009
Oops, I now see it should be

Dim ts As New System.Threading.ThreadStart()

and so on.....

*** Sent via Developersdex http://www.developersdex.com ***
 
Reply With Quote
 
Sreenivas
Guest
Posts: n/a
 
      06-09-2009
For all i know, I prefer to delegate to ThreadPool.QueueUserWorkItem
method...


On Jun 9, 9:07*am, "Mark B" <(E-Mail Removed)> wrote:
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I want to send a Slideshow of Pictures With Background Music in Email? Heidi Manway Computer Support 19 03-17-2007 04:20 AM
which email module - MIME::LITE, Email::Send, other? brewsterbear@googlemail.com Perl Misc 2 01-05-2007 09:45 AM
Cannot compose ,reply or send email on Sasktel email system pcbutts1 Computer Support 5 07-29-2005 06:13 AM
why cant i send email messages from this email Billie Jo Ames Computer Support 8 06-30-2005 12:24 PM
HowTo:? open email client to send email Peter ASP .Net 0 07-01-2003 04:58 PM



Advertisments