แบบฟอร์มติดต่อไม่ส่งอีเมลไปที่บัญชี Gmail ของฉัน

ฉันได้สร้างแบบฟอร์มอย่างง่ายโดยใช้ Visual Studio 2012 แต่ไม่ได้ส่งอีเมลไปยังบัญชี Gmail ของฉัน หน้าทำงานได้ดี แต่เมื่อฉันกดปุ่มส่ง ฉันได้รับข้อผิดพลาด:

เซิร์ฟเวอร์ SMTP ต้องการการเชื่อมต่อที่ปลอดภัย หรือไคลเอ็นต์ไม่ได้รับการรับรองความถูกต้อง การตอบสนองของเซิร์ฟเวอร์คือ: 5.7.0 ต้องออกคำสั่ง STARTTLS ก่อน j8sm1567623paz.30

คำอธิบาย: มีข้อยกเว้นที่ไม่ได้รับการจัดการเกิดขึ้นระหว่างการดำเนินการคำขอเว็บปัจจุบัน โปรดตรวจสอบการติดตามสแต็กเพื่อดูข้อมูลเพิ่มเติมเกี่ยวกับข้อผิดพลาดและที่มาของโค้ด

รายละเอียดข้อยกเว้น: System.Net.Mail.SmtpException: เซิร์ฟเวอร์ SMTP จำเป็นต้องมีการเชื่อมต่อที่ปลอดภัย หรือไคลเอ็นต์ไม่ได้รับการรับรองความถูกต้อง การตอบสนองของเซิร์ฟเวอร์คือ: 5.7.0 ต้องออกคำสั่ง STARTTLS ก่อน 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 

แหล่งที่มา:

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, "[email protected]", "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" />

person user1830770    schedule 16.11.2012    source แหล่งที่มา
comment
ดูเหมือนว่าเซิร์ฟเวอร์อีเมลต้องการการเชื่อมต่อที่ปลอดภัย และคุณไม่ได้ขอการเชื่อมต่อ   -  person itsbruce    schedule 17.11.2012
comment
ที่เกี่ยวข้อง (เกี่ยวกับ gmail/.net) stackoverflow.com/questions/3615674/, stackoverflow.com/questions/10690015/, stackoverflow.com/questions/1905019/   -  person Kratz    schedule 17.11.2012


คำตอบ (1)


สองสิ่ง:

  1. คุณต้องใช้ HTTPS
  2. คุณต้องให้ข้อมูลประจำตัวสำหรับบัญชีของคุณ

นี่คือตัวอย่าง C# จากที่นี่:

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

var fromAddress = new MailAddress("[email protected]", "From Name");
var toAddress = new MailAddress("[email protected]", "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
                ("[email protected]", "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("[email protected]",
            "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
person RAS    schedule 16.11.2012