2

I have created a simple form using Visual Studio 2012 but it's not sending email to my gmail account, the page runs fine but when I hit send button I get error:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. j8sm1567623paz.30

Description: An unhanded 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.7.0 Must issue a STARTTLS command first. j8sm1567623paz.30

Source Error: 
Line 14: 
Line 15:         
Line 16:         mailClient.Send(message)
Line 17:         
Line 18:    

Source File: C:\Website SVN II\test\contact.aspx.vb    Line: 16 

Sources:

Imports System.Net.Mail

Partial Class contact
 Inherits System.Web.UI.Page

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    sendMail(txtEmail.Text, txtMessage.Text)
End Sub

Protected Sub sendMail(ByVal From As String, ByVal body As String)
    Dim mailservername As String = "smtp.gmail.com"
    Dim message As MailMessage = New MailMessage(From, "nabeel.f@gmail.com", "feedback", body)
    Dim mailClient As SmtpClient = New SmtpClient

    mailClient.Host = mailservername
    mailClient.Send(message)
    message.Dispose()
End Sub

End Class

HTML.

        first name
        <asp:TextBox ID="txtFName" runat="server"></asp:TextBox>

        <br />
        <br />

        last name 
        <asp:TextBox ID="txtLName" runat="server"></asp:TextBox>

        <br />
        <br />

        email
        <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
       
        <br />
        <br />            

        message: 
        <asp:TextBox ID="txtMessage" runat="server" TextMode="MultiLine"></asp:TextBox>

        <br />

        <asp:Button ID="Button1" runat="server" Text="Send" />
4

1 回答 1

1

两件事情:

  1. 您需要使用 HTTPS。
  2. 您需要为您的帐户提供凭据。

这是来自此处的 C# 示例:

using System.Net;
using System.Net.Mail;

var fromAddress = new MailAddress("from@gmail.com", "From Name");
var toAddress = new MailAddress("to@example.com", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";

var smtp = new SmtpClient
           {
               Host = "smtp.gmail.com",
               Port = 587,
               EnableSsl = true,
               DeliveryMethod = SmtpDeliveryMethod.Network,
               UseDefaultCredentials = false,
               Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
           };
using (var message = new MailMessage(fromAddress, toAddress)
                     {
                         Subject = subject,
                         Body = body
                     })
{
    smtp.Send(message);
}

vb.net 版本:

Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
    Dim SmtpServer As New SmtpClient()
    SmtpServer.Credentials = New Net.NetworkCredential
                ("xyz@gmail.com", "password")
    SmtpServer.Port = 587
    SmtpServer.Host = "smtp.gmail.com"
    SmtpServer.EnableSsl = True

    mail = New MailMessage()
    Dim addr() As String = TextBox1.Text.Split(",")
    Try
        mail.From = New MailAddress("xyz@gmail.com",
            "Web Developers", System.Text.Encoding.UTF8)

        Dim i As Byte
        For i = 0 To addr.Length - 1
            mail.To.Add(addr(i))
        Next
        mail.Subject = TextBox3.Text
        mail.Body = TextBox4.Text
        If ListBox1.Items.Count <> 0 Then
            For i = 0 To ListBox1.Items.Count - 1
                mail.Attachments.Add(New Attachment
                    (ListBox1.Items.Item(i)))
            Next
        End If
        mail.DeliveryNotificationOptions =
                DeliveryNotificationOptions.OnFailure
        mail.ReplyTo = New MailAddress(TextBox1.Text)
        SmtpServer.Send(mail)
    Catch ex As Exception
        MsgBox(ex.ToString())
    End Try
End Sub
于 2012-11-16T21:08:08.227 回答