2012-11-28 96 views
2

我們正在嘗試發送了一個HTML字符串輸出到一個特定的測試電子郵件地址發送電子郵件時,可以指定的錯誤,並發現此運行時錯誤:收件人必須從ASP.Net

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("[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 

我們懷疑我們沒有使用正確的語法這一行:

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

這是一個奇怪的錯誤,因爲我已經設定的BCC列表。我想只有「To」被認爲是規範的。 – arviman

回答

3

你缺少To:地址,這是造成有關接收方的錯誤。

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

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

+0

感謝您的快速回復。我試過ObjMailMessage.To =新MailAddress(「[email protected]」,「發生錯誤」,System.Text.Encoding.UTF8),但得到一個錯誤:Property'To'是'ReadOnly' –

+0

對不起, :財產是一個集合。我更新了答案 - 您應該可以將新的MailAddress添加到集合中。 –

+0

謝謝。它運作良好。 :-) –

相關問題