vb.net 从 ASP.Net 发送电子邮件时必须指定收件人错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13607533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:20:59  来源:igfitidea点击:

A recipient must be specified error when sending email from ASP.Net

asp.netvb.netemailemail-integration

提问by Emad-ud-deen

We are trying to send the out the output of a html string to a particular test email address and found this error at runtime:

我们试图将 html 字符串的输出发送到特定的测试电子邮件地址,并在运行时发现此错误:

A recipient must be specified.

Here is the coding from the code-behind file.

这是来自代码隐藏文件的编码。

Protected Sub EmailTheList()

    ' Get the rendered HTML.
    '-----------------------
    Dim SB As New StringBuilder()
    Dim SW As New StringWriter(SB)
    Dim htmlTW As New HtmlTextWriter(SW)

    GridViewSummary.RenderControl(htmlTW)

    ' Get the HTML into a string.
    ' This will be used in the body of the email report.
    '---------------------------------------------------
    Dim dataGridHTML As String = SB.ToString()

    Dim SmtpServer As New SmtpClient()
    SmtpServer.Credentials = New Net.NetworkCredential("[email protected]", "myPassword")
    SmtpServer.Port = 587
    SmtpServer.Host = "smtp.gmail.com"
    SmtpServer.EnableSsl = True

    ObjMailMessage = New MailMessage()

    Try
        ObjMailMessage.From = New MailAddress("[email protected]", "Some text is here.", System.Text.Encoding.UTF8)

        ObjMailMessage.Subject = "Test message from Emad"
        ObjMailMessage.ReplyToList.Add("[email protected]")
        ObjMailMessage.Body = dataGridHTML

        ObjMailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure

        SmtpServer.Send(ObjMailMessage)

    Catch ex As Exception
        MsgBox(ex.ToString())
    End Try
End Sub

We suspect we are not using the correct syntax for this line:

我们怀疑我们没有为此行使用正确的语法:

ObjMailMessage.From = ObjMailMessage.ReplyToList.Add("[email protected]")

回答by Dave Swersky

You're missing the To: address, which is causing the error regarding a recipient.

您缺少收件人:地址,这会导致有关收件人的错误。

ObjMailMessage.To.Add(New MailAddress("[email protected]", "An error happened", System.Text.Encoding.UTF8))

Reference: http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.aspx

参考:http: //msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.aspx