【问题标题】:A recipient must be specified error when sending email from ASP.Net从 ASP.Net 发送电子邮件时必须指定收件人错误
【发布时间】:2012-11-16 10:47:06
【问题描述】:

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

A recipient must be specified.

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

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("myEmailAddress@gmail.com", "myPassword")
    SmtpServer.Port = 587
    SmtpServer.Host = "smtp.gmail.com"
    SmtpServer.EnableSsl = True

    ObjMailMessage = New MailMessage()

    Try
        ObjMailMessage.From = New MailAddress("myEmailAddress@gmail.com", "Some text is here.", System.Text.Encoding.UTF8)

        ObjMailMessage.Subject = "Test message from Emad"
        ObjMailMessage.ReplyToList.Add("john.doe@example.com")
        ObjMailMessage.Body = dataGridHTML

        ObjMailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure

        SmtpServer.Send(ObjMailMessage)

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

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

ObjMailMessage.From = ObjMailMessage.ReplyToList.Add("john.doe@example.com")

【问题讨论】:

  • 这是一个奇怪的错误,因为我设置了密件抄送列表。我想只有“To”被认为是规范的。

标签: asp.net vb.net email


【解决方案1】:

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

ObjMailMessage.To.Add(New MailAddress("mail@somemail.com", "An error happened", System.Text.Encoding.UTF8))

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

【讨论】:

  • 感谢您的快速回复。我试过 ObjMailMessage.To = New MailAddress("mail@somemail.com", "An error occurred", System.Text.Encoding.UTF8) 但得到一个错误:Property 'To' is 'ReadOnly'
  • 抱歉,To: 属性是一个集合。我更新了答案-您应该能够将新的 MailAddress 添加到集合中。
猜你喜欢
  • 2017-01-28
  • 2016-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多