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
November 5, 2009 by Nauman · Leave a Comment
This annoying error comes up when using some smtp server that uses SSL.
A simple implementation to configure smtp settings in an ASP.NET web application is to add these setting in the specific "MailSettings" tag of Web.config file. I had also declared all the settings in web.config file but still I get this every time i was to send a mail through the application. After much search I could not find a way to set SSL property in web.config MailSettings tag.
In the end the idea that came up in my mind was to code all the setting in my VB file and send the mail through SMPT Client object in ASP.NET. Following was the code I wrote to hook ASP PasswordRecovery control to send mail:
Protected Sub PasswordRecovery1_SendingMail(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MailMessageEventArgs) Handles PasswordRecovery1.SendingMail
‘!!! UPDATE THIS VALUE TO YOUR EMAIL ADDRESS
‘(1) Create the MailMessage instance
Dim mm As New System.Net.Mail.MailMessage(e.Message.From, e.Message.To(0))
Dim basicinfo As New System.Net.NetworkCredential("username", "password") ‘ google mail username and password to be set here.
‘(2) Assign the MailMessage’s properties
mm.Subject = e.Message.Subject
mm.Body = e.Message.Body
‘(3) Create the SmtpClient object
Dim smtp As New System.Net.Mail.SmtpClient
smtp.Credentials = basicinfo
‘(4) Send the MailMessage (will use the Web.config settings)Try
smtp.EnableSsl = True
smtp.Send(mm)
Catch ex As Exception
MsgBox(ex.Message)
End Try
‘don’t send the System.Net.Mail.MailMessage
e.Cancel = True
End Sub
In this SMPT client object has a property to enable SSL, and that is how I managed to send the mail.