无法从asp.net表单发送电子邮件

时间:2012-07-01 20:21:32

标签: asp.net .net vb.net

我创建了一个表单,用于将电子邮件发送到电子邮件帐户并使用以下代码。

       Dim client As New SmtpClient()
        Dim emailmessage As New MailMessage()

        Dim emailfrom As New MailAddress(txtEmail.Text)

        emailmessage.Subject = "Web Submission"
        emailmessage.Body = "Web submission received from " & txtName.Text & ". Phone no: "     & txtPhone.Text & "."
        client.Host = "localhost"
        client.Port = 25
        emailmessage.From = emailfrom
        emailmessage.To.Add("info@test.com")

        client.Send(emailmessage)
        lblMessage.Text = "Thank you, we will contact you soon"

我正在使用IIS express来运行我的项目。我知道IIS表示不支持smtp,如果我是正确的。但如果代码是正确的,它应该正常运行。无论如何,我通过.net发布工具将我的网站发布到我的godaddy帐户。当我尝试从表单发送电子邮件时,我收到以下错误消息。

邮箱不可用。服务器响应为:5.7.1无法转发“info@test.com”

Dim msg As New MailMessage()
        msg.To = "test@gmail.com"
        msg.From = "test@gmail.com"
        msg.Subject = "test"
        'msg.BodyFormat = MailFormat.Html
        msg.BodyFormat = MailFormat.Text
        msg.Body = "hi"

        SmtpMail.SmtpServer = "localhost"

        SmtpMail.Send(msg)
        lblMessage.Text = "An Email has been send to " & "test@gmail.com"

我尝试了上面的代码但仍无效。我只是无法弄清楚我在做什么。我记得我曾经使用过这个工作。但我不记得如何。

1 个答案:

答案 0 :(得分:2)

您缺少的SmtpServer凭据

        client.Credentials = New Net.NetworkCredential("username@gmail.com", "password")

如果没有,请配置您的smtp虚拟服务器,如示例http://codebetter.com/petervanooijen/2006/04/05/using-localhost-as-mailserver-5-7-1-unable-to-relay-for-xxx/

所示