如何在asp.net的电子邮件正文中发送包含数据库数据的表 [英] How to send a table with data from database in an email body in asp.net

查看:98
本文介绍了如何在asp.net的电子邮件正文中发送包含数据库数据的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一张表从表中返回25条记录。不,我想发送电子邮件,在邮件正文中我希望数据显示在表格中。





请帮忙怎么做。



谢谢和问候。

Hi All,

I have a table returning 25 records from a table. No I want to send an email and in message body I want data to be shown in table.


Please help how to do it.

Thanks and regards.

推荐答案

Hi,

try this method with your database settings assuming that the table is Employee

public void SendMail()
    {
        string body = "";
        String ConnStr = "Data Source=ServerName;Initial Catalog=DBNamae;User ID=UserName;Password='password';";
        String SQL = "SELECT ID, FirstName FROM Employee "
           + "WHERE ID IS NOT NULL";
        SqlDataAdapter TitlesAdpt = new SqlDataAdapter(SQL, ConnStr);
        DataSet Titles = new DataSet();
        // No need to open or close the connection
        //   since the SqlDataAdapter will do this automatically.
        TitlesAdpt.Fill(Titles);
        body = "<table>";
        foreach (DataRow Title in Titles.Tables[0].Rows)
        {
            body += "<tr>";
            body += "<td>" + Title[0] + "</td>";
            body += "<td>" + String.Format("{0:c}", Title[1])
               + "</td>";
            body += "</tr>";
        }
        body += "</table>";

        //now set up the mail settings
        MailMessage message = new MailMessage();
        message.From = new MailAddress("sender@foo.bar.com");
        //can add more recipient
        message.To.Add(new MailAddress("recipient1@foo.bar.com"));
        message.To.Add(new MailAddress("recipient2@foo.bar.com"));
        //add cc
        message.CC.Add(new MailAddress("carboncopy@foo.bar.com"));
        message.Subject = "This is my subject";
        message.Body = body;

        SmtpClient client = new SmtpClient();
        client.Send(message);
    }


MailMessage objMailMessage = new MailMessage();

objMailMessage.IsBodyHtml = false;



将IsBodyHtml = false的标志更改为IsBodyHtml = true,因为您必须以表格格式发送邮件。
MailMessage objMailMessage = new MailMessage();
objMailMessage.IsBodyHtml = false;

change flag of IsBodyHtml=false to IsBodyHtml=true because you have to send mail in tabular format.


这篇关于如何在asp.net的电子邮件正文中发送包含数据库数据的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆