如何使用格式在电子邮件中发送gridview

时间:2013-10-08 13:56:42

标签: c# asp.net email gridview

我正在开发一个在线订购网络应用程序,我必须通过电子邮件向客户发送perches产品详细信息。我已将所有数据保存在数据表中,然后按实际方式生成网格视图,如下所示

 public GridView makeGridview(DataTable Dt)
        {
            GridView GV = new GridView();
            GV.DataSource = Dt;
            GV.DataBind();
            return GV;
        }

然后将此网格视图添加到电子邮件

 body = body.Replace("{Product_Details}", GridViewToHtml(makeGridview(Dt)));

private string GridViewToHtml(GridView gv)
        {
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            gv.RenderControl(hw);
            return sb.ToString();
        }

这工作正常,但问题是我无法以预定义的格式设计它。这封邮件将由我网站的客户收到,因此我需要转换为我们的邮件。 请指导我如何格式化它们。

如果有另一种方法可以做到这一点,那么我也会对此持开放态度。

4 个答案:

答案 0 :(得分:1)

我建议你不要相信GridView作为渲染的html,而是使用你当前的数据源(DataTable):

    public GridView CreateHtmlTable(DataTable dt)
    {
       //Do your HTML work here, like the following:
       string tab = "\t";

       StringBuilder sb = new StringBuilder();

       sb.AppendLine("<html>");
       sb.AppendLine(tab + "<body>");
       sb.AppendLine(tab + tab + "<table>");

       // headers.
       sb.Append(tab + tab + tab + "<tr>");

       foreach (DataColumn dc in dt.Columns)
       {        
       sb.AppendFormat("<td>{0}</td>", dc.ColumnName);        
       }

       sb.AppendLine("</tr>");

       // data rows
       foreach (DataRow dr in dt.Rows)
       {
                  sb.Append(tab + tab + tab + "<tr>");

    foreach (DataColumn dc in dt.Columns)
    {
        string cellValue = dr[dc] != null ? dr[dc].ToString() : "";
        sb.AppendFormat("<td>{0}</td>", cellValue);
    }

    sb.AppendLine("</tr>");
}

       sb.AppendLine(tab + tab + "</table>");
       sb.AppendLine(tab + "</body>");
       sb.AppendLine("</html>");
    }

答案 1 :(得分:0)

请遵循以下代码:

public void SendHTMLMail()
{
MailMessage Msg = new MailMessage();
MailAddress fromMail = new MailAddress("administrator@aspdotnet-suresh.com");
// Sender e-mail address.
Msg.From = fromMail;
// Recipient e-mail address.
Msg.To.Add(new MailAddress("suresh@gmail.com"));
// Subject of e-mail
Msg.Subject = "Send Gridivew in EMail";
Msg.Body += "Please check below data <br/><br/>";
Msg.Body += GetGridviewData(gvUserInfo);
Msg.IsBodyHtml = true;
string sSmtpServer = "";
sSmtpServer = "10.2.160.101";
SmtpClient a = new SmtpClient();
a.Host = sSmtpServer;
a.EnableSsl = true;
a.Send(Msg);
}
// This Method is used to render gridview control
public string GetGridviewData(GridView gv)
{
StringBuilder strBuilder = new StringBuilder();
StringWriter strWriter = new StringWriter(strBuilder);
HtmlTextWriter htw = new HtmlTextWriter(strWriter);
gv.RenderControl(htw);
return strBuilder.ToString();
}

请参阅以下文件:

http://www.aspdotnet-suresh.com/2012/09/how-to-send-gridview-in-email-body-in.html

答案 2 :(得分:0)

老实说,你在这里所做的事情(将Web窗体控件呈现为电子邮件字符串)似乎是一种无意识的伎俩。老实说,使用StringBuilder迭代集合并构建HTML表并不难。

我认为对于电子邮件HTML,惯例通常是使用内联样式来保证与电子邮件客户端的最佳兼容性。所以你想要像这样设计你的元素:

<table>
  <tbody>
    <tr style="background-color: #EE0000">
      <td style="text-transform: uppercase"></td>
      ...
    </tr>
  </tbody>
</table>

过去我通过创建一个HTML文件,包含我想要的所有内容并使用{VariableName}这样的占位符,然后使用它们的值对变量进行字符串替换来完成此操作。这样您就可以将HTML视图与C#代码分开。对于项目列表来说有点棘手,在这种情况下,您至少有两个HTML模板:一个用于整个文档,一个用于项目行。

答案 3 :(得分:0)

我遇到了类似您的问题,我做到了:

  • 将该功能封装在单独的HelperClass中,以易于使用。使用静态变量和方法使其保持简单

  • 编写HTML代码以将GridView的内容格式化为一个漂亮的表(带有斑马条纹和填充物)

     public static StringBuilder gridViewToHTML(GridView gv)
    {
        StringBuilder sb = new StringBuilder();
    
        sb.AppendLine("<html>");
        sb.AppendLine("<body>");
        sb.AppendLine(@"<table style='padding: 10px; font-family: Verdana; font-size:11px;
                            border-style:solid;border-width:1px;border-color:grey;'> ");
    
        sb.AppendLine("<tr>");
    
        /* *** Build header of the HTML table *** */
        for (int i = 0; i < gv.Columns.Count; i++)
        {
            sb.AppendLine("<td style='font-weight:bold;background-color:black;color:white;'>" + gv.Columns[i].HeaderText + "</td>");
        }
    
        sb.AppendLine("</tr>");
    
    
    
        /* *** Build body of the HTML table *** */
        for (int i = 0; i < gv.Rows.Count; i++)
        {
            sb.AppendLine("<tr>");
            foreach (DataControlFieldCell gvcell in gv.Rows[i].Cells)
            {
                sb.AppendLine("<td style='text-align;left;'>" + gvcell.ToString() + "</td>");
            }
            sb.AppendLine("</tr");
        }
    
    
    
        sb.AppendLine("</table>");
        sb.AppendLine("</body>");
        sb.AppendLine("</html>");
    
        return sb;
    }
    
  • 使用StringBuilder对象将电子邮件发送为内容的html格式,并使用临时参数(发送给Whom和Subject)

     public static void sendEmailMessage(StringBuilder stringBuilder, string email_ToAddress, string email_Subject)
    {
        DateTime now = DateTime.Now;
        DateTime yesterday = DateTime.Now.AddDays(-1);
    
        MailMessage mail = new MailMessage();
        //SomeEmailAccount@yourOrganization.com    must be set by your Sysadmin before using it.
        mail.From = new MailAddress("SomeEmailAccount@yourOrganization.com");
        mail.To.Add(email_ToAddress);
    
        mail.Subject = $"{email_Subject} . Date #{now.ToShortDateString()}";
        mail.Body = stringBuilder.ToString();
        mail.IsBodyHtml = true;
    
        NetworkCredential autentificare = new NetworkCredential();
        autentificare.UserName = "SomeEmailAccount@yourOrganization.com"";
        autentificare.Password = "yourPassw0rd";
    
        SmtpClient smtp = new SmtpClient();
    
        smtp.Host = "mail.yourOrganization.com";
        smtp.UseDefaultCredentials = true;
        smtp.Credentials = autentificare;
        smtp.Port = 25;
        smtp.EnableSsl = false;
        smtp.Send(mail);
    
    }