【问题标题】:ASP.Net - send email with page contentsASP.Net - 发送带有页面内容的电子邮件
【发布时间】:2009-07-06 07:04:54
【问题描述】:

我有一个 Asp.Net 页面,其中包含一个 GridView 和几个图像(谷歌图表 - pngs)。 我需要通过电子邮件发送我页面的内容。我怎样才能做到这一点?网格可以是电子邮件正文中的 html 表格,或者整个内容可以是图像;没关系。
感谢您的帮助!

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    您可以使用将 URL 转换为单个 MHT 文件(嵌入图像、样式表等)的库并将该文件附加到您的电子邮件中。

    Here's a .NET library 完成这项工作(它是由 Jeff Atwood 编写的 ^^)

    【讨论】:

    • 感谢奥利维尔!我会尽快调查您的建议。
    【解决方案2】:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.
    WebParts;
    using System.Web.UI.HtmlControls;
    using System.Net.Mail;
    
    public partial class Default : System.Web.UI.Page
    {
      protected void Page_Load(object sender, EventArgs e)
      {
      }
      protected void Button1_Click(object sender, EventArgs e)
      {
    //Calling the function SendMail
        Response.Write( SendMail("
     dinesh.rabara@gmail.com ","
     info@server.com","
     dinesh_rabara@yahoo.com ","Test Mail","Test Mail Body"));
      }
    
      public string SendMail(string toList, string from, string ccList, string subject, string body)
      {
          MailMessage message = new MailMessage();
          SmtpClient smtpClient = new SmtpClient();
          string msg = string.Empty;
          try
          {
              MailAddress fromAddress = new MailAddress(from);
              message.From = fromAddress;
              message.To.Add(toList);
              if (ccList != null && ccList != string.Empty)
                  message.CC.Add(ccList);
              message.Subject = subject;
              message.IsBodyHtml = true;
              message.Body = body;
              smtpClient.Host = "mail.server.com";
              smtpClient.Port = 25;
              smtpClient.UseDefaultCredentials = true;
              smtpClient.Credentials = new System.Net.NetworkCredential("info@server.com ","password");         
    
              smtpClient.Send(message);
              msg = "Successful";
          }
          catch (Exception ex)
          {
              msg = ex.Message;
          }
          return msg;
      }
    
    }
    

    【讨论】:

      【解决方案3】:

      过去我使用GridView.RenderControl 将gridview 渲染到ASP.NET 之外的文件中(在控制台应用程序中)。这可能值得研究。

      编辑-我已经设法找到一个使用这种技术的人的博客文章-this may help you

      这是否适用于图像取决于您生成它们的操作。大概它们是动态生成的,可能使用 HttpHandler,或者它们是从控件生成的?

      【讨论】:

      • 您好,理查德,感谢您的回答。幸运的是,我有图像的 URL,因为它们是谷歌图表图像。我认为最难的部分是网格。我会调查你的建议。再次感谢!
      • 虽然 Olivier 的回答也是正确的,但 Richard 的回答要简单得多,而且做得更干净。谢谢!
      猜你喜欢
      • 2012-05-25
      • 2010-12-06
      • 1970-01-01
      • 2014-07-13
      • 2012-11-17
      • 2010-11-22
      • 2011-09-22
      • 2012-05-03
      • 1970-01-01
      相关资源
      最近更新 更多