Windows Phone Developers

Showing posts with label VB.NET SendCompleted event. Show all posts
Showing posts with label VB.NET SendCompleted event. Show all posts

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