0

我的 SMTP 代码在 gmail 上工作,但是当我在 Yahoo 和 Hotmail 上尝试它时,它总是失败。我输入了正确的服务器和端口,但它仍然不允许我发送。我真的很困惑我哪里出错了,因为我能够在 gmail 下完美地发送邮件。

这是我的完整代码:

Function SendEmail(ByVal Recipients As List(Of String), _
                  ByVal FromAddress As String, _
                  ByVal Subject As String, _
                  ByVal Body As String, _
                  ByVal UserName As String, _
                  ByVal Password As String, _
                  Optional ByVal Server As String, _
                  Optional ByVal Port As Integer, _
                  Optional ByVal Attachments As List(Of String) = Nothing) As String


    Dim Email As New MailMessage()
    Try
        Dim SMTPServer As New SmtpClient
        For Each Attachment As String In Attachments
            Email.Attachments.Add(New Attachment(Attachment))
        Next
        Email.From = New MailAddress(FromAddress)
        For Each Recipient As String In Recipients
            Email.To.Add(Recipient)
        Next
        Email.Subject = Subject
        Email.Body = Body
        SMTPServer.Host = Server
        SMTPServer.Port = Port
        SMTPServer.Credentials = New System.Net.NetworkCredential(UserName, Password)
        SMTPServer.EnableSsl = True
        SMTPServer.Send(Email)
        SMTPServer.Timeout = 500
        Email.Dispose()

        Return "Email to " & Recipients(0) & " from " & FromAddress & " was sent."
    Catch ex As SmtpException
        Email.Dispose()
        Return "Sending Email Failed. Smtp Error."
    Catch ex As ArgumentOutOfRangeException
        Email.Dispose()
        Return "Sending Email Failed. Check Port Number."
    Catch Ex As InvalidOperationException
        Email.Dispose()
        Return "Sending Email Failed. Check Port Number."
    End Try
End Function


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim Recipients As New List(Of String)
    Recipients.Add("put recipient email here")
    Dim FromEmailAddress As String = Recipients(0)
    Dim Subject As String = "subject goes here"
    Dim Body As String = "Email body goes here"
    Dim UserName As String = "put username here"
    Dim Password As String = "put password here"
    Dim Port As Integer = port number goes here
    Dim Server As String = "SMTP server goes here"
    Dim Attachments As New List(Of String)
    MsgBox(SendEmail(Recipients, FromEmailAddress, Subject, Body, UserName, Password, Server, Port, Attachments))

End Sub

与 gmail 相比,Yahoo 和 Hotmail 的行为是否有所不同?我对这个 SMTP 东西真的很陌生。我今天刚开始学习,请多多包涵。任何建议或意见都将被接受。提前致谢。

4

2 回答 2

0

您可以使用EASendMail.NET 库,它会自动检测端口。

http://www.emailarchitect.net/easeendmail/

我做的例子:

   Private Function Hotmail_Sender(ByVal Account_User As String, ByVal Account_Password As String, ByVal Mail_To As String, ByVal Mail_Subject As String, ByVal Mail_Body As String, Optional ByVal Mail_Attachments() As String = Nothing) As Boolean

       Dim Hot_Mail As New EASendMail.SmtpMail("TryIt")
       Dim Hot_Server As New EASendMail.SmtpServer("smtp.live.com")
       Dim Hot_Smtp As New EASendMail.SmtpClient()

       Hot_Server.User = Account_User
       Hot_Server.Password = Account_Password
       Hot_Server.ConnectType = EASendMail.SmtpConnectType.ConnectSSLAuto

       Hot_Mail.From = Account_User
       Hot_Mail.To = Mail_To
       Hot_Mail.Subject = Mail_Subject
       Hot_Mail.TextBody = Mail_Body
       If Mail_Attachments IsNot Nothing Then For Each Attachment In Mail_Attachments : Hot_Mail.AddAttachment(Attachment) : Next

       Try : Hot_Smtp.SendMail(Hot_Server, Hot_Mail) : Return True
       Catch ex As Exception : Return False : End Try

   End Function
于 2013-10-22T09:34:11.980 回答
0

他们在不同的端口上工作:

Hotmail 外发邮件服务器 (SMTP) - smtp.live.com(启用 TLS,端口 587)

雅虎外发邮件服务器 (SMTP) - smtp.mail.yahoo.com(启用 SSL,端口 995)

确保您使用的是:

SMTPServer.EnableSsl = True

更改每个服务的 SMTP 端口。(在本例中,Hotmail 为 587)。

一种是 SSL,另一种是 TLS。TLS(传输级安全)是一个稍微宽泛的术语,它已经取代了 SSL,所以不用担心,它可以使用EnableSsl

于 2013-10-22T09:32:47.330 回答