使用图表控件向ASP.NET网页发送电子邮件

时间:2011-07-22 16:51:07

标签: asp.net vb.net mschart stringbuilder

我有一个asp.net 4.0网页,上面有10个图表控件。我打算在打开页面时将图表控件通过电子邮件发送给当前登录的用户。每个用户的图表控件都不同。我一直在尝试发送1个图表控件来测试这个,但是电子邮件的正文并没有显示图表只有图像轮廓。我尝试了几件事,但无法让它发挥作用。我刚才的代码是 -

的web.config

<add key="ChartImageHandler" value="storage=memory;deleteAfterServicing=true;"/>

网页

受保护的子Button1_Click(ByVal sender As Object,ByVal e As System.EventArgs)处理Button1.Click

    SendMail()

End Sub

Private Sub SendMail()

    Dim SB As New StringBuilder()
    Dim SW As New StringWriter(SB)
    Dim htmlTW As New HtmlTextWriter(SW)
    'SB.Append("<td><img src=""cid:chart17""></td>")
    Chart10.RenderControl(htmlTW)
    Dim MyHTML As String = SB.ToString()

    Dim from As String = "EMAIL ADDRESS"
    Dim recip As String = "EMAIL ADDRESS"
    'Dim recip As String = Membership.GetUser.Email.ToString
    Dim subject As String = "Test Email"




    'Create message object and populate w/ data from form
    Dim message As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
    message.From = New System.Net.Mail.MailAddress(from.Trim())
    message.To.Add(recip.Trim())
    message.Subject = subject.Trim()
    message.IsBodyHtml = True
    message.Body = MyHTML
    'Setup SmtpClient to send email. Uses web.config settings.
    Dim smtpClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient()

    'Error handling for sending message
    Try
        smtpClient.Send(message)
        'Exception contains information on each failed receipient
    Catch recExc As System.Net.Mail.SmtpFailedRecipientsException
        For recipient = 0 To recExc.InnerExceptions.Length - 1
            Dim statusCode As System.Net.Mail.SmtpStatusCode
            'Each InnerException is an System.Net.Mail.SmtpFailed RecipientException
            statusCode = recExc.InnerExceptions(recipient).StatusCode

            If (statusCode = Net.Mail.SmtpStatusCode.MailboxBusy) Or (statusCode = Net.Mail.SmtpStatusCode.MailboxUnavailable) Then
                'Log this to event log: recExc.InnerExceptions(recipient).FailedRecipient
                System.Threading.Thread.Sleep(5000)
                smtpClient.Send(message)
            Else
                'Log error to event log.
                'recExc.InnerExceptions(recipient).StatusCode or use statusCode
            End If

        Next
        'General SMTP execptions
    Catch smtpExc As System.Net.Mail.SmtpException
        'Log error to event log using StatusCode information in
        'smtpExc.StatusCode
    Catch ex As Exception
        'Log error to event log.
    End Try

End Sub

正如您所看到的,我在“SB.Append”和“chart10.rendercontrol(htmlTW)等论坛上尝试了一些示例,但两者都不适合我。

任何帮助都会很棒。

1 个答案:

答案 0 :(得分:1)

您没有将图像附加到电子邮件中。

''I am not sure how to handle memory streams in vb but it should be something like so.
Dim ms as MemoryStream = new MemoryStream()
Chart10.SaveImage(ms, ChartImageFormat.Png)
Dim A As System.Net.Mail.Attachment = New System.Net.Mail.Attachment(ms, "MyChart.png")
A.ContentId = "chart17"
A.ContentDisposition.Inline = True
message.Attachments.Add(A)