Sending Email Asyncronously in ASP.NET

Discussion in 'Site Programming, Development and Design' started by 0000677, Dec 6, 2009.

  1. Hi
    Please, anyone can help me? I need to know how send email asyncronously using VB.NET with ASP.NET (Visual Studio 2008).
    Thank You.
     
  2. Ray

    Ray

    Last edited by a moderator: Oct 14, 2015
  3. Hi Ray
    I already saw this article and I use exactly this code in my site. But I need to know how can I use the method SmtpMail.SendAsync() and not the method SmtpMail.Send().
    The method SmtpMail.SendAsync() use 2 parameters and is usefull when the application doesnt need to wait for the email to be sent.
    Thank You
    Eduardo Benoliel
     
  4. Ray

    Ray

    Is there a reason why you can't use the SmtpMail.Send method? For the most part this method is more then enough to sustain the business needs of our customers when sending out email from their web application.
    I'll have to check with our systems administrator and verify if they allow 'SmtpMail.SendAsync' on our servers.
     
  5. Ray
    The use of SendAsync is usefull , for example, when I need to report to myself any problem during the use of application without demage of the response performance.
    Thank You
     
  6. Ray

    Ray

    I just checked with our developer and we should support the 'SmtpMail.SendAsync' method. Whats the error message you are getting?
     
  7. I receive the error message: failure sending mail.

    this is my code:
    Imports System.Web.SessionState
    Imports System.Net.Mail.MailMessage
    Imports System.Net.Mail.SmtpClient
    Imports System.Net
    Imports System.Net.Mail
    Imports System.ComponentModel

    Public Class Global_asax
    Inherits System.Web.HttpApplication
    Public Function EnviaEmal(ByVal cto As String, ByVal cbody As String) As Boolean
    Dim msg As String = "---"
    Dim lRet As Boolean = True
    Dim strFrom = "[email protected]"

    Dim strTo = cto '"[email protected]"


    Dim MailMsg As New System.Net.Mail.MailMessage(New MailAddress(strFrom.Trim()), New MailAddress(strTo))
    MailMsg.BodyEncoding = Encoding.Default
    MailMsg.Subject = "txtrial.co.cc usado"
    MailMsg.Body = cbody
    MailMsg.Priority = MailPriority.High
    MailMsg.IsBodyHtml = True
    Dim SmtpMail As New SmtpClient
    Dim basicAuthenticationInfo As New System.Net.NetworkCredential("[email protected]", "***")

    SmtpMail.Host = "mail.txtrial.co.cc"
    SmtpMail.UseDefaultCredentials = False
    SmtpMail.Credentials = basicAuthenticationInfo
    Dim userState As Object = MailMsg

    'wire up the event for when the Async send is completed
    AddHandler SmtpMail.SendCompleted, AddressOf SmtpClient_OnCompleted
    Stop
    Try
    SmtpMail.SendAsync(MailMsg, userState)

    Catch ex As Exception
    'Stop
    msg = ex.Message
    lRet = False
    End Try

    MailMsg.Dispose()

    Return lRet

    End Function

    Public Sub SmtpClient_OnCompleted(ByVal sender As Object, ByVal e As ComponentModel.AsyncCompletedEventArgs)
    'Get the Original MailMessage object
    Dim mail As MailMessage = CType(e.UserState, MailMessage)

    'write out the subject
    Dim subject As String = mail.Subject

    If e.Cancelled Then
    Console.WriteLine("Send canceled for mail with subject [{0}].", subject)
    End If
    If Not (e.Error Is Nothing) Then
    Console.WriteLine("Error {1} occurred when sending mail [{0}] ", subject, e.Error.ToString())
    Else
    Console.WriteLine("Message [{0}] sent.", subject)
    End If
    End Sub 'SmtpClient_OnCompleted
     
  8. Make sure the following Typo is not present in your actual code:

    "Dim basicAuthenticationInfo As New System.Net.NetworkCredential("[email protected] o.cc", "***")"

    '.c o.cc'
     
  9. Ray

    Ray

    Is this the full error "failure sending mail"? You may also want to make sure you set Async to true.
     
  10. Hi

    I found 2 problems and fixed both:
    1)
    After the method SmtpMail.SendAsync(MailMsg, userState)
    I can't execute the method MailMsg.Dispose()

    2)
    I must set Async="true" in :
    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" uiculture="auto" EnableSessionState = "True" ValidateRequest="true" Buffer="true" Async="true" %>

    Now the SendAsync method is working fine.
    Thank you very much.
     
  11. Ray

    Ray

    Glad to hear it.
     

Share This Page