通过电子邮件发送带有图表控件的 ASP.NET 网页

发布于 2024-11-25 16:46:29 字数 2749 浏览 3 评论 0原文

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

web.config

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

网页

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles 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) 但两者都不适合我。

任何帮助都会非常感激。

I have an asp.net 4.0 webpage which has 10 chart controls on it. I have been to email the chart controls to the current logged in user when they open the page. 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. I have tried several things but cant get it to work. The code i have just now is -

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

    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.

Any helpo would be greatful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╭⌒浅淡时光〆 2024-12-02 16:46:29

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

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

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文