A
Adam Honek
Okay.
This is a little strange but here goes.
I wanted to send emails and hence wrote the first sub below. It worked
however it would NOT send the email UNLESS I closed the application (hence
terminated the main thread).
Wanting to fix this I thought it would work if I do it the asyncrhonous way
so the calling thread isn't blocked. This is when I added the 2nd sub below
All the code still works but again, it does not send the email unless I exit
the application. It's almost as if its buffering it for some reason.
Shouldn't this just get fired ASAP as soon as I do .sendasync() or .send()
?????
Very confusing.
Thanks for all feedback,
Adam
************SUB 1****************
Public Sub SendEmail(ByVal sFromEmailAddress As String, ByVal
sFromSenderName As String, ByVal sToEmailAddress As String, ByVal
sEmailSubject As String, ByVal sEmailBody As String, ByVal EmailPriority As
System.Net.Mail.MailPriority)
Try
Dim MySMTPClient As New System.Net.Mail.SmtpClient("smtp.realname.net") '
Dim MyEmail As New System.Net.Mail.MailMessage
Dim MyAddressFrom As New System.Net.Mail.MailAddress(sFromEmailAddress,
sFromSenderName)
Dim MyAddressTo As New System.Net.Mail.MailAddress(sToEmailAddress)
'Build the email
MyEmail.From = MyAddressFrom
MyEmail.To.Add(sToEmailAddress)
MyEmail.Subject = sEmailSubject
MyEmail.Body = sEmailBody
MyEmail.Priority = EmailPriority
MyEmail.IsBodyHtml = True
Dim MyMailObject As Object = MyEmail
AddHandler MySMTPClient.SendCompleted, AddressOf SmtpClient_OnCompleted
MySMTPClient.SendAsync(MyEmail, MyMailObject)
MyEmail = Nothing
'If there are any errors catch them
Catch MyException As Exception
MsgBox("The following error occured: " + MyException.Message,
MsgBoxStyle.Critical, "Sending email")
End Try
End Sub
************SUB 2****************
Public Sub SmtpClient_OnCompleted(ByVal sender As Object, ByVal e As
System.ComponentModel.AsyncCompletedEventArgs)
'Get the Original MailMessage object
Dim mail As System.Net.Mail.MailMessage = CType(e.UserState,
System.Net.Mail.MailMessage)
'write out the subject
Dim subject As String = mail.Subject
If e.Cancelled Then
'They cancelled it last minute
End If
If Not (e.Error Is Nothing) Then
'Explain to the user
Else
MsgBox("The email has been sent", MsgBoxStyle.OkCancel,
Application.ProductName)
End If
End Sub
This is a little strange but here goes.
I wanted to send emails and hence wrote the first sub below. It worked
however it would NOT send the email UNLESS I closed the application (hence
terminated the main thread).
Wanting to fix this I thought it would work if I do it the asyncrhonous way
so the calling thread isn't blocked. This is when I added the 2nd sub below
All the code still works but again, it does not send the email unless I exit
the application. It's almost as if its buffering it for some reason.
Shouldn't this just get fired ASAP as soon as I do .sendasync() or .send()
?????
Very confusing.
Thanks for all feedback,
Adam
************SUB 1****************
Public Sub SendEmail(ByVal sFromEmailAddress As String, ByVal
sFromSenderName As String, ByVal sToEmailAddress As String, ByVal
sEmailSubject As String, ByVal sEmailBody As String, ByVal EmailPriority As
System.Net.Mail.MailPriority)
Try
Dim MySMTPClient As New System.Net.Mail.SmtpClient("smtp.realname.net") '
Dim MyEmail As New System.Net.Mail.MailMessage
Dim MyAddressFrom As New System.Net.Mail.MailAddress(sFromEmailAddress,
sFromSenderName)
Dim MyAddressTo As New System.Net.Mail.MailAddress(sToEmailAddress)
'Build the email
MyEmail.From = MyAddressFrom
MyEmail.To.Add(sToEmailAddress)
MyEmail.Subject = sEmailSubject
MyEmail.Body = sEmailBody
MyEmail.Priority = EmailPriority
MyEmail.IsBodyHtml = True
Dim MyMailObject As Object = MyEmail
AddHandler MySMTPClient.SendCompleted, AddressOf SmtpClient_OnCompleted
MySMTPClient.SendAsync(MyEmail, MyMailObject)
MyEmail = Nothing
'If there are any errors catch them
Catch MyException As Exception
MsgBox("The following error occured: " + MyException.Message,
MsgBoxStyle.Critical, "Sending email")
End Try
End Sub
************SUB 2****************
Public Sub SmtpClient_OnCompleted(ByVal sender As Object, ByVal e As
System.ComponentModel.AsyncCompletedEventArgs)
'Get the Original MailMessage object
Dim mail As System.Net.Mail.MailMessage = CType(e.UserState,
System.Net.Mail.MailMessage)
'write out the subject
Dim subject As String = mail.Subject
If e.Cancelled Then
'They cancelled it last minute
End If
If Not (e.Error Is Nothing) Then
'Explain to the user
Else
MsgBox("The email has been sent", MsgBoxStyle.OkCancel,
Application.ProductName)
End If
End Sub