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

Yahoo と Hotmail は gmail とは動作が異なりますか? 私はこのSMTPのことに本当に慣れていません。今日から学習を始めたばかりですので、ご容赦ください。どんなアドバイスや意見も受け入れます。前もって感謝します。

4

2 に答える 2

0

それらは異なるポートで動作します:

Hotmail 送信メール サーバー (SMTP) - smtp.live.com (TLS 有効、ポート 587)

Yahoo 送信メール サーバー (SMTP) - smtp.mail.yahoo.com (SSL 有効、ポート 995)

使用していることを確認してください:

SMTPServer.EnableSsl = True

そして、各サービスの SMTP ポートを変更します。(この場合、Hotmail の場合は 587)。

1 つは SSL で、もう 1 つは TLS です。TLS (トランスポート レベル セキュリティ) は、SSL に取って代わるやや広義の用語であるため、心配する必要はありません。EnableSsl

于 2013-10-22T09:32:47.330 に答える
0

EASendMail.NET 用のライブラリを使用すると、ポートが自動的に検出されます。

http://www.emailarchitect.net/easendmail/

私がした例:

   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 に答える