发送邮件失败:SMTP异常

时间:2010-08-04 04:44:14

标签: vb.net email

我写信是希望你能帮助我。今天我要用vb.net开发一个邮件应用程序,为此我编写了下面给出的代码。

此代码抛出异常(“SMTP异常”)

Public Function SendAnEmail(ByVal MsgBody As String)
    Try
        Dim MsgFrom As String = "amolkadam.a@gmail.com"
        Dim MsgTo As String = "amolkadam.a@gmail.com"
        Dim MsgSubject As String = "claim Report"

        '  Pass in the message information to a new MailMessage

        Dim msg As New Net.Mail.MailMessage(MsgFrom, MsgTo, MsgSubject, MsgBody)

        '   Create an SmtpClient to send the e-mail
        Dim mailClient As New SmtpClient("219.64.91.90")  '  = local machine IP Address

        '  Use the Windows credentials of the current User
        mailClient.UseDefaultCredentials = True

        ' Pass the message to the mail server
        mailClient.Send(msg)

        '  Optional user reassurance:
        MessageBox.Show(String.Format("Message Subject ' {0} ' successfully sent From {1} To {2}", MsgSubject, MsgFrom, MsgTo), "EMail", Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Information)

        '  Housekeeping
        msg.Dispose()

    Catch ex As FormatException
        MessageBox.Show(ex.Message & " :Format Exception")
    Catch ex As SmtpException
        MessageBox.Show(ex.Message & " :SMTP Exception")
    End Try
End Function

1 个答案:

答案 0 :(得分:0)

您已经编写了使用自己的PC作为邮件服务器的代码,这是正确的吗?这样你就没有误解为发送到SmtpClient的IP应该是服务器的IP。

如果您的本地PC上运行了SMTP服务器,您是否尝试过通过其他客户端从该服务器发送电子邮件?并且服务器将接受包含gmail.com地址from的电子邮件(某些服务器不会)。 您可以按照here的说明使用任何telnet客户端尝试此操作,只需根据需要交换地址。