Windows Phone Developers

Tuesday, May 6, 2008

Send emails asynchronously using .NET

Send Asynchronous emails using VB.NET

Imports System.Net.Mail

Imports System.Net.Mime

Imports System.ComponentModel

Sub Send_Email_Asynchronously()

Try

Dim oMailMsg As MailMessage

Dim oClient As SmtpClient

oMailMsg = New MailMessage

oMailMsg.From = New MailAddress("admin@vbadud.com")

oMailMsg.To.Add(New MailAddress("sample@yahoo.co.au"))

oClient = New SmtpClient("smtp.vbadud.com") ' configure client

AddHandler oClient.SendCompleted, AddressOf Client_SendCompleted

' Send message

oClient.EnableSsl = True

' Send Message Asynchronously

oClient.SendAsync(oMailMsg, Nothing)

Catch ex2 As SmtpFailedRecipientException

'Represents the exception that is thrown when the SmtpClient is not able to complete a Send or SendAsync operation to a particular recipient.

' Occurs when the message is delivered within the given domain

MessageBox.Show(ex2.Message)

Catch ex3 As SmtpException

' Occurs when the message is delivered within any domain

MessageBox.Show(ex3.Message)

End Try

End Sub

oClient.SendAsync sends the specified e-mail message to an SMTP server for delivery. This method does not block the calling thread and allows the caller to pass an object to the method that is invoked when the operation completes.

The following event will be raised when an asynchronous e-mail send operation completes. You can cancel the message in between too.

Private Sub Client_SendCompleted(ByVal sender As Object, ByVal e As asynccompletedeventargs)

If e.Cancelled Then '

MessageBox.Show("Message sending cancelled")

Else

If e.Error Is Nothing Then

MsgBox("Message Sent Successfully")

Else

MsgBox("error has occurred :" & e.Error.Message)

End If

End If

End Sub

Sending Asynchronous mails VB.NET, Sending Secure Mails using VB.NET, Send emails asynchronously using .NET

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

1 comment:

  1. If I have 500 messages to send out, is this a good way to do it? Note that I'm talking about 500 messages, not 500 recipients of the same message - 500 different messages to 500 different recipients.

    I'm concerned that it would create too many calls in the background at once.

    ReplyDelete