Search code examples
asp.netemailgmail

asp.net The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required


hi i know this question is a bit redundant but i am experiencing it differently here is my code on sending email after creation of account on asp.net

Dim newreg As MembershipUser = Membership.GetUser(context.Request("username"))
                Dim newid As Guid
                If newreg.ProviderUserKey IsNot Nothing Then
                    newid = DirectCast(newreg.ProviderUserKey, Guid)
                End If

                Dim body As String = String.Empty
                Dim reader As StreamReader = New StreamReader(context.Server.MapPath("~/Account/email.htm"))
                body = reader.ReadToEnd
                body = body.Replace("{UserName}", context.Request("username").ToString)
                body = body.Replace("{Url}", "http://wwww.123.com/Account/activate.aspx?id=" & context.Request("username").ToString & "&usertype=" & givetype(context.Request("username")) & "&actid=" & newid.ToString)

                Dim mailMessage As MailMessage = New MailMessage
                mailMessage.From = New MailAddress(ConfigurationManager.AppSettings("UserName"))
                mailMessage.Subject = "Account Activation"
                mailMessage.Body = body
                mailMessage.IsBodyHtml = True
                mailMessage.To.Add(New MailAddress(context.Request("email").ToString))
                Dim smtp As SmtpClient = New SmtpClient
                smtp.Host = ConfigurationManager.AppSettings("Host")
                smtp.EnableSsl = True
                Dim NetworkCred As System.Net.NetworkCredential = New System.Net.NetworkCredential
                NetworkCred.UserName = ConfigurationManager.AppSettings("UserName")
                NetworkCred.Password = ConfigurationManager.AppSettings("Password")
                smtp.UseDefaultCredentials = False
                smtp.Credentials = NetworkCred
                smtp.Port = Integer.Parse(ConfigurationManager.AppSettings("Port"))
                smtp.Send(mailMessage)

the odd thing is when i run the website on localhost gmail allows it but when i run the website on a vps it rejects and gives this error

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required

any help will do tnx


Solution

  • got it working the problem was gmail is blocking the request to send the email because of the 2 step verification i created a password for the app and now gmail is not blocking the request to send e-mails hope this helps if somebody will encounter the same issue with gmail