通过电子邮件发送图表

时间:2015-08-04 04:54:57

标签: c# asp.net email linechart

我想使用ASP.Net和C#在电子邮件正文中发送折线图。我编写了一个代码来显示div标签中的图表。我想用邮件发送折线图。怎么做?

%5B2014%2C%202015%5D

2 个答案:

答案 0 :(得分:5)

假设您使用System.Web.UI.DataVisualization作为图表,chart类的方法SaveImage可用于将该图表另存为图形(例如png )进入内存流。使用该流来吐出data-uri,然后您可以在HTML格式的电子邮件正文中使用它。

例如

Charting.Chart myChart = new Charting.Chart();
System.IO.MemoryStream imgStream = new System.IO.MemoryStream();
myChart.SaveImage(imgStream, Charting.ChartImageFormat.Png);
return "<img class='chartImage' src='data:image/png;base64, " + System.Convert.ToBase64String(imgStream.ToArray()) + "' />";

此代码将返回img标记data-uri作为其src,然后您可以在HTML电子邮件正文中使用该标记。

答案 1 :(得分:2)

流程代码:

头标记:

&#13;
&#13;
<head runat="server">
    <title></title>
     <script type="text/javascript"
          src="https://www.google.com/jsapi?autoload={
            'modules':[{
              'name':'visualization',
              'version':'1',
              'packages':['corechart']
            }]
          }"></script>
    
<script type="text/javascript">
        google.setOnLoadCallback(drawChart);
        function drawChart() {
            var data = google.visualization.arrayToDataTable([
              ['Year', 'Sales', 'Expenses'],
              ['2004', 1000, 400],
              ['2005', 1170, 460],
              ['2006', 660, 1120],
              ['2007', 1030, 540]
            ]);

            var options = {
                title: 'Company Performance',
                curveType: 'function',
                legend: { position: 'bottom' }
            };

            var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));
            var txtBase64 = document.getElementById('txtBase64Image');
            google.visualization.events.addListener(chart, 'ready', function () {
                txtBase64.value = chart.getImageURI();
            });
            chart.draw(data, options);
        }
    </script>
</head>
&#13;
&#13;
&#13;

身体标签:

&#13;
&#13;
<body>
    <form id="form1" runat="server">
        <div id="curve_chart" style="width: 900px; height: 500px"></div>
        <%--If you want, you can textbox visible false.--%>
        <asp:TextBox ID="txtBase64Image" runat="server" Width="600" TextMode="MultiLine"></asp:TextBox><br/>
        <asp:Button ID="Button1" runat="server" Text="Send Mail" OnClick="Button1_Click" />
    </form>
</body>
&#13;
&#13;
&#13;

在c#代码之后:

public Image Base64ToImage(string base64String)
{
    var imageBytes = Convert.FromBase64String(base64String);
    using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
    {
        var image = Image.FromStream(ms, true);
        return image;
    }
}

按钮点击:发送到邮件图表。看看EmailSender https://github.com/serkomut/Serkomut.MailSender

protected void Button1_Click(object sender, EventArgs e)
{
    var split = txtBase64Image.Text.Split(',')[1];
    var image = Base64ToImage(split);
    var stream = new MemoryStream();
    image.Save(stream, ImageFormat.Jpeg);
    stream.Position = 0;

    var result = new EmailSender()
                    .FromHost("smtp.gmail.com")
                    .Credential("sendermail@gmail.com", "mailPassword")
                    .FromTo(new Message
                    {
                        From = "sendermail@gmail.com",
                        To = "tomail@gmail.com"
                    })
                    .Subject("Subject")
                    .Body("Content text...")
                    .Attachment(new Attachment(stream, "chart_image.jpg", "image/jpg"))
                    .Send();
}