使用smtp客户端和ASP.NET不发送邮件

时间:2012-11-22 17:11:47

标签: asp.net smtpclient

我一直在浏览Stack Overflow上的一些看起来与我类似的帖子,我调整了我的代码,但它仍然无效。我的代码主要供您阅读:

Dim ss As SmtpClient = New SmtpClient("smtp.gmail.com", 587)
    'Dim ss As SmtpClient = New SmtpClient("smtp.gmail.com",465 )
    'Dim ss As SmtpClient = New SmtpClient("smtp.gmail.com", 25)
    ss.Timeout = 10000
    ss.DeliveryMethod = SmtpDeliveryMethod.Network
    ss.UseDefaultCredentials = False
    ss.EnableSsl = True
    ss.UseDefaultCredentials = False
    ss.Credentials = New NetworkCredential("name@gmail.com", "name@123")


    Dim subject As String = "Approval request for Quotation with Order Number: " + Me.OrderNo

    Dim body As String = "Dear Sir/Madam," + vbCrLf + " This is to bring to your notice the quotation for the Client " + Me.cmbCustomer.SelectedItem.Text + " and Order Number " + Me.OrderNo & _
        " which is awaiting your approval." + vbCrLf + "Click here to view and approve the Quotation: http:192.168.20.7/appname/Sales/ManageInvoices.aspx" + vbCrLf & _
        " Kindly revert after approval. Thank you." + vbCrLf + "Regards."

    Dim To1, To2, To3 As String

    Dim mm As MailMessage = New MailMessage()
    mm.From = New MailAddress("name@gmail.com")

    To1 = "usernameTo1@domain.com"
    To2 = "usernameTo2@domain.com"
    To3 = "usernameTo3@gmail.com"
    'To is a collection of MailAddress types
    mm.To.Add(To1)
    mm.To.Add(To2)
    mm.To.Add(To3)

    mm.Subject = subject
    mm.Body = body

    mm.BodyEncoding = UTF8Encoding.UTF8
    'mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
    Try
        ss.Send(mm)
    Catch ex As SmtpException
        Me.lblerr.Text = "SMTP Err:" & ex.ToString
    End Try

那么可能是什么使邮件无法到达目的地?我根本没有收到任何错误消息。请帮助谢谢。

1 个答案:

答案 0 :(得分:0)

对于gmail,您需要将端口设置为587。

<system.net>
    <mailSettings>
      <smtp from="myn@gmail.com ">
        <network host="smtp.gmail.com" defaultCredentials="false"
          port="587" userName ="me@gmail.com" password="pass@123" />
      </smtp>
    </mailSettings>
   </system.net>