I'm using the following code to send an email:
Dim SendMessage As New Net.Mail.SmtpClient
Dim userToken As New Object
Dim EmailMessage As New MailMessage
With EmailMessage
.From = New MailAddress("username@gmail.com")
.To.Add("username@xxx.com")
.Subject = "Test"
.Body = "test"
End With
AddHandler SendMessage.SendCompleted, AddressOf sendMail_SendCompleted
With SendMessage
.Host = "smtp.gmail.com"
.Port = 25
.UseDefaultCredentials = False
.Credentials = New Net.NetworkCredential("username", "password")
.EnableSsl = False
End With
'SendMessage.SendAsync(cbxFrom.Text, cbxTo.Text, tbSubject.Text, tbBody.Text, userToken)
SendMessage.SendAsync(EmailMessage, userToken)
End While
Private Sub sendMail_SendCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
Dim userToken As Object = e.UserState 'This is how you know which send has completed, you pass some identifier in usertoken
If e.Cancelled Then 'Operation was cancelled
Debug.WriteLine("Email cancelled.")
End If
If e.Error IsNot Nothing Then 'An error occured
Debug.WriteLine("Error: {0}", e.Error.ToString)
Else 'No error occured
Debug.WriteLine("Email sent!")
End If
End Sub
When I run this code I receive the following error:
"A first chance exception of type 'System.Net.Mail.SmtpException' occurred in System.dll
System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. 5sm359599ybq.8
at System.Net.Mail.SendMailAsyncResult.End(IAsyncResult result)
at System.Net.Mail.SmtpClient.SendMailCallback(IAsyncResult result): Error: {0}
The thread 0x68c has exited with code 0 (0x0).
The thread 0x25a0 has exited with code 0 (0x0)."
What am I doing wrong ??? I already tested with defaultcredentials = true but does not work !