在电子邮件中构建表格 [英] Structuring a table in an email

查看:101
本文介绍了在电子邮件中构建表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在控制台应用程序中列出了一些数据,这些数据是从电子邮件中提取的.之后,我将发送所有列出的所有数据的电子邮件.问题在于数据的结构不是很好,电子邮件也难以理解.我尝试使用body html,但失败了.我想知道是否有人可以帮助我弄清楚如何构造电子邮件.下面是我在C#中的代码

I Have some data listed in a console application which was fetched from an email. After I send an email with all this listed data. The problem is the data is not coming structured in a nice way and the email isn't understandable. I tried using body html but failed. I was wondering if anyone could help me figure out how to structure the email. below is my code in c#

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Mail;
using System.Net;

namespace sql_connection
{
    class Program
    {
        static void Main(string[] args) 
        {
            string conn = null;
            SqlConnection connection;
            conn = ("Data Source=Database\\SQL2012;Initial Catalog=jobs;User ID=user;Password=passs");

            connection = new SqlConnection(conn);
            try
            {            
                connection.Open();
                Console.WriteLine("Connection Open!");
                SqlCommand cmd = new SqlCommand("SELECT jobs.[dbo].[tb_work].whd_Date,jobs.[dbo].[tb_work].whd_FromTime,jobs.[dbo].[tb_work].whd_ToTime, jobs.[dbo].[tb_work].whd_User,jobs.[dbo].[tb_UserLogin].login_Email FROM jobs.[dbo].[tb_work]INNER JOIN jobs.[dbo].[tb_UserLogin] ON jobs.[dbo].[tb_work].whd_User = jobs.[dbo].[tb_UserLogin].login_LoginId WHERE  DATEDIFF(DAY,[whd_FromTime],GETDATE())<=7 AND   (whd_ToTime = '' OR whd_ToTime IS NULL) AND(whd_User=login_LoginId)");
                cmd.Connection = connection;
                SqlDataReader reader = cmd.ExecuteReader();
                var columns = Enumerable.Range(0, reader.FieldCount).Select(reader.GetName).ToList();

                var list = new List<string>();
                var col = new List<string>();

                while(reader.Read())
                {
                    var s = string.Format(" {1}     {0}         {2}       {3} ",
                                reader["whd_ToTime"] == DBNull.Value 
                                    ? "NULL" : reader["whd_ToTime"].ToString(), 
                                reader["whd_FromTime"] == DBNull.Value
                                    ? "NULL" : reader["whd_FromTime"].ToString(),                     
                                reader["whd_User"].ToString(),
                                reader["login_Email"].ToString());

                    Console.WriteLine(string.Join("   ", columns.ToArray()));

                    Console.WriteLine(s);
                    list.Add(s);
                }

                var sb = new StringBuilder();
                foreach (var s in list)
                {
                    sb.AppendLine(s);
                }

                connection.Close();

                MailMessage message;                    
                message=new MailMessage();

                MailAddress to = new MailAddress("xxxx@gmail.com");

                MailAddress from = new MailAddress("xxxx@gmail.com");

                MailMessage mail = new MailMessage(from, to);

                mail.Subject = ("missed punch clock");

                message.IsBodyHtml = true;

                StringBuilder html = new StringBuilder();

                html.AppendFormat("<!DOCTYPE html>");
                html.AppendFormat("<html><body><table>");
                html.AppendFormat("<tr><td>");

                html.Append("<table width=600px border=1 cellspacing=2 cellpadding=2 align=center bgcolor=White dir=ltr rules=all style=border-width: thin; border-style: solid; line-height: normal; vertical-align: baseline; text-align: center; font-family: Calibri; font-size: medium; font-weight: normal; font-style: normal; font-variant: normal; color: #000000; list-style-type: none;>");
                for (int rowind = 0; rowind < 1; rowind++)
                {
                    html.Append("<tr>");
                    html.Append("<td>");
                    html.Append("<table width=600px border=1 cellspacing=2 cellpadding=2 align=center bgcolor=White dir=ltr rules=all style=border-width: thin; border-style: solid; line-height: normal; vertical-align: baseline; text-align: center; font-family: Calibri; font-size: medium; font-weight: normal; font-style: normal; font-variant: normal; color: #000000; list-style-type: none;>");

                    for (int newrowind = 0; newrowind < 1; newrowind++)
                    {
                        html.AppendFormat("<tr>");
                        html.Append("<td colspan=1  style=font-weight:bold>");
                        html.Append("whd_ToTime");
                        html.Append("</td>");
                        html.Append("<td colspan=2 style=font-weight:bold>");
                        html.Append("whd_FromTime");
                        html.Append("</td>");
                        html.Append("<td colspan=3 style=font-weight:bold>");
                        html.Append("whd_User");
                        html.Append("</td>");
                        html.Append("</tr>");

                        foreach (var s in list)
                        {
                            html.AppendFormat("<tr>");
                            html.Append("<td colspan=1  style=font-weight:bold>");
                            html.Append(sb.ToString());
                            html.Append("</td>");
                            html.Append("</tr>");
                        }
                    }

                    html.Append("</tr>");
                    html.Append("</td>");
                    html.Append("</table>");
                }

                html.Append("</table>");
                html.AppendFormat("</td></tr>");
                html.AppendFormat("</table></html></body>");

                mail.Body= html.ToString();
                mail.IsBodyHtml = true;

                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;

                smtp.Credentials = new NetworkCredential("xxxx"gmail.com", xxxxxxxx");
                smtp.EnableSsl = true;
                Console.WriteLine("Sending email..");
                smtp.Send(mail);
            } 
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

推荐答案

首先,为了查明问题,只需查看生成的HTML,这样就可以在生成它的代码中检测到问题.您可以做的是首先使用HTML设计邮件,并在看起来不错时编写代码以实现它.

First, in order to pinpoint your issue, just look at the generated HTML and like that you can detect issues in your code that generated it. What you can do is first design the mail in HTML, and when it looks good write your code to implement it.

读取在代码中加载的HTML模板(作为嵌入式资源)并使用占位符存储自定义数据而不是在代码中构建HTML更容易,因为这样可以将视图(HTML)与逻辑分离.

It's easier to read an HTML template that you load in your code (as an embedded resource) and use placeholders for your custom data instead of building HTML in code - because like this you separate the view (HTML) from the logic.

请注意,格式化邮件是一项艰巨的任务,因为您必须考虑各种设备和邮件客户端.如果您希望它们在所有部件上都能很好地显示,那么最好使用类似 Zurb Ink 的框架您可以自己完成所有操作-这为您提供了最好的机会,以最终在大多数设备和客户端上都可以使用的响应式电子邮件告终.

Note that formatting mails is a difficult task as you have to take into account various devices and mail clients. If you want them to display nicely on all of them, then you better use a framework like Zurb Ink instead of doing everything yourself - this gives you the best chance to end up with responsive emails that work on most devices and clients.

这篇关于在电子邮件中构建表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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