简体   繁体   English

Email ASP.NET 网页与图表控件

[英]Email ASP.NET webpage with Chart Controls

I have an asp.net 4.0 webpage which has 10 chart controls on it.我有一个 asp.net 4.0 网页,上面有 10 个图表控件。 I have been to email the chart controls to the current logged in user when they open the page.我去过email,当他们打开页面时,图表控制当前登录的用户。 The chart controls will be different for each user.每个用户的图表控件都不同。 I have been testing this by trying to send 1 chart control but the body of the email doesnt show the chart only the image outline.我一直在尝试发送 1 个图表控件来对此进行测试,但 email 的主体并未仅显示图表的图像轮廓。 I have tried several things but cant get it to work.我已经尝试了几件事,但无法让它发挥作用。 The code i have just now is -我刚才的代码是 -

web.config web.config

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

webpage网页

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Protected Sub 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

As you can see i have tried some examples on forums like "SB.Append" and "chart10.rendercontrol(htmlTW) but both do not work for me.如您所见,我在“SB.Append”和“chart10.rendercontrol(htmlTW)”等论坛上尝试了一些示例,但两者都不适合我。

Any helpo would be greatful.任何帮助都会很棒。

You are not attaching the image to the e-mail.您没有将图像附加到电子邮件中。

''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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM