SMTP邮件错误,发送邮件失败

时间:2014-04-08 02:05:53

标签: asp.net-mvc vb.net email smtp

我想在发送电子邮件时提交表单时遇到问题。问题是SMTP邮件错误。运行时,我收到消息“发送邮件失败”。并为Innerexecption“无法连接到远程服务器”。下面是我的代码:

< Protected Sub btnExit_Click(sender as Object,e As EventArgs)处理btnExit.Click

    Dim conn As New MySql.Data.MySqlClient.MySqlConnection

    Dim strConnectionString As String = ConfigurationManager.ConnectionStrings("testConnectionString").ConnectionString

    Try
        conn.ConnectionString = strConnectionString
        conn.ConnectionString = "server=localhost;user id=root;persistsecurityinfo=False;database=test;password=1234"
        conn.Open()





    Catch ex As MySql.Data.MySqlClient.MySqlException
        MessageBox.Show(ex.Message)
    End Try


    Dim sqlConn As New MySqlConnection(strConnectionString)
    sqlConn.Open()
    Dim sqlComm As New MySqlCommand()

    sqlComm.Connection = sqlConn


    Dim msg As MailMessage
    Dim email As String = String.Empty
    Dim SmtpServer As New SmtpClient("smtp.gmail.com", 587)

    msg = New MailMessage()

    email = TextBox3.Text.Trim()
    'sender email address
    msg.From = New MailAddress("****s@gmail.com")
    'Receiver email address
    msg.[To].Add(email)
    msg.Subject = "Change Request have Been Submitted By:"
    msg.Body = "Hi " & TextBox1.Text.Trim() & "!" & vbLf & "Thanks for Submitted the Change Request Online " & vbLf & "Thanks!"
    msg.IsBodyHtml = True
    SmtpServer.Credentials = New NetworkCredential("****s@gmail.com", "******")
    SmtpServer.Port = 587
    SmtpServer.Host = "smtp.gmail.com"
    SmtpServer.EnableSsl = True
    SmtpServer.Send(msg)'THIS LINE IT SHOW THE ERROR'

    ' UserControl()
    Try
        ScriptManager.RegisterStartupScript(Me, Me.[GetType](), "Message", "alert('Your Change Request have been submitted.');", True)
    Catch ex As Exception
        ScriptManager.RegisterStartupScript(Me, Me.[GetType](), "Message", "alert('Error occured : " & ex.Message.ToString() & "');", True)
        Return
    Finally

        email = String.Empty
        sqlConn.Close()
        sqlComm.Dispose()
    End Try
    Response.Redirect("Default.aspx")
End Sub>

1 个答案:

答案 0 :(得分:0)

这是因为您使用了错误的端口号,google的gmail使用端口465,更改587并且它应该可以正常工作(如果用户名+密码正确)。