Send email with background thread?

Discussion in 'ASP .Net' started by Mark B, Jun 9, 2009.

  1. Mark B

    Mark B Guest

    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
    Mark B, Jun 9, 2009
    #1
    1. Advertising

  2. Mark B

    Chris Zopers Guest

    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 ***
    Chris Zopers, Jun 9, 2009
    #2
    1. Advertising

  3. Mark B

    Mark B Guest

    Thanks. Got VB.NET for me?

    "Chris Zopers" <> wrote in message
    news:...
    > 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 ***
    Mark B, Jun 9, 2009
    #3
  4. Mark B

    Chris Zopers Guest

    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 ***
    Chris Zopers, Jun 9, 2009
    #4
  5. Mark B

    Chris Zopers Guest

    Oops, I now see it should be

    Dim ts As New System.Threading.ThreadStart()

    and so on.....

    *** Sent via Developersdex http://www.developersdex.com ***
    Chris Zopers, Jun 9, 2009
    #5
  6. Mark B

    Sreenivas Guest

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


    On Jun 9, 9:07 am, "Mark B" <> 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
    Sreenivas, Jun 9, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter
    Replies:
    0
    Views:
    3,355
    Peter
    Jul 1, 2003
  2. =?Utf-8?B?Sm9oYW5uYQ==?=
    Replies:
    3
    Views:
    1,954
    =?Utf-8?B?Sm9oYW5uYQ==?=
    Oct 15, 2004
  3. Replies:
    2
    Views:
    395
    Tintin
    Jan 5, 2007
  4. Replies:
    13
    Views:
    463
    Randy Webb
    May 14, 2006
  5. pbd22
    Replies:
    1
    Views:
    146
Loading...

Share This Page