DDDD
DDDD

Reputation: 3940

Why is my VB.NET website not sending an email?

My code is adding a user to my database. I am trying to have my site send an email with a passcode to verify the email account is legit.

First I am trying to get it to send a basic test email. Then I plan on adding the passcode in a link to my site validating it.

My problem is my code doesn't send the basic test email.

Imports System.Data.SqlClient
Imports System.Net.Mail

Partial Class Account_Register
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles     Me.Load
    RegisterUser.ContinueDestinationPageUrl = Request.QueryString("ReturnUrl")
End Sub



Protected Sub RegisterUser_CreatedUser(ByVal sender As Object, ByVal e As EventArgs) Handles RegisterUser.CreatedUser
    FormsAuthentication.SetAuthCookie(RegisterUser.UserName, False)

    Dim MyMailMessage As New MailMessage()
    ' MyMailMessage.IsBodyHtml = True
    MyMailMessage.From = New MailAddress("[email protected]")
    MyMailMessage.To.Add("[email protected]")
    MyMailMessage.Subject = "Email Confirmation"
    MyMailMessage.Body = "TESTING"
    'MyMailMessage.Body = "<html>" & RegisterUser.UserName & "Link: $" & "<br/> " & "</html>"

    'Create the SMTPClient object and specify the SMTP GMail server
    Dim SMTPServer As New SmtpClient("smtp.gmail.com")
    SMTPServer.Port = 588
    SMTPServer.Credentials = New System.Net.NetworkCredential("[email protected]", "Password")
    SMTPServer.EnableSsl = True

    Try
        SMTPServer.Send(MyMailMessage)
        'MessageBox.Show("Email Sent")
    Catch ex As SmtpException
        'MessageBox.Show(ex.Message)
    End Try


    Dim continueUrl As String = RegisterUser.ContinueDestinationPageUrl
    If String.IsNullOrEmpty(continueUrl) Then
        continueUrl = "~/"
    End If

    Response.Redirect(continueUrl)
End Sub
End Class

Any help?

Upvotes: 0

Views: 551

Answers (2)

DDDD
DDDD

Reputation: 3940

So I ended up having to use Port = 587

Thank you for the help!

Upvotes: 0

Sudz
Sudz

Reputation: 4308

Change the port number Gmail SMTP port is : 465

Use this

 SMTPServer.Port = 465

Upvotes: 1

Related Questions