ASP.net联系表单发送电子邮件错误

时间:2015-05-31 03:58:07

标签: asp.net vb.net contactus

我真的需要帮助。我有一个网站,在该网站上我联系了我们页面,用户可以在该页面上联系管理员了解该页面或任何其他内容。

问题是当我尝试渲染页面时,它给出了一个错误说

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at

Source Error:


Line 21:         smtp.Credentials = NetworkCred
Line 22:         smtp.Port = 587
Line 23:         smtp.Send(mm)
Line 24:         lblMessage.Text = "Email Sent SucessFully."
Line 25:     End Sub


Source File: C:\Users\user\Desktop\ContactUsForm\VB.aspx.vb    Line: 23 

我真的不知道wt要做什么。我的VB代码是:

Imports System.Net
Imports System.Net.Mail
Partial Class VB
    Inherits System.Web.UI.Page
    Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim mm As New MailMessage("sender@gmail.com", "reveiver@gmail.com")
        mm.Subject = txtSubject.Text
        mm.Body = "Name: " & txtName.Text & "<br /><br />Email: " & txtEmail.Text & "<br />" & txtBody.Text
        If FileUpload1.HasFile Then
            Dim FileName As String = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName)
            mm.Attachments.Add(New Attachment(FileUpload1.PostedFile.InputStream, FileName))
        End If
        mm.IsBodyHtml = True
        Dim smtp As New SmtpClient()
        smtp.Host = "smtp.gmail.com"
        smtp.EnableSsl = True
        Dim NetworkCred As New System.Net.NetworkCredential()
        NetworkCred.UserName = "sender@gmail.com"
        NetworkCred.Password = "senderpassword"
        smtp.UseDefaultCredentials = True
        smtp.Credentials = NetworkCred
        smtp.Port = 587
        smtp.Send(mm)
        lblMessage.Text = "Email Sent SucessFully."
    End Sub
End Class

错误在第23行,即smtp.Send(mm)

我真的有需要。请任何人帮助我。

1 个答案:

答案 0 :(得分:0)

更改

smtp.UseDefaultCredentials = False

您不想使用默认凭据,您希望使用您在代码中创建的凭据。

https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.usedefaultcredentials(v=vs.110).aspx