为什么我的VB.NET网站没有发送电子邮件?

时间:2013-05-15 07:07:19

标签: asp.net vb.net

我的代码是将用户添加到我的数据库中。我想让我的网站发送一封带有密码的电子邮件,以验证电子邮件帐户是否合法。

首先,我试图让它发送一个基本的测试电子邮件。然后我计划在我的网站链接中添加密码验证它。

我的问题是我的代码没有发送基本的测试邮件。

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("NAME@gmail.com")
    MyMailMessage.To.Add("Name@yahoo.com")
    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("NAME@gmail.com", "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

任何帮助?

2 个答案:

答案 0 :(得分:1)

更改端口号Gmail SMTP端口为:465

使用此

 SMTPServer.Port = 465

答案 1 :(得分:0)

所以我最终不得不使用Port = 587

感谢您的帮助!