0

我有这个任务,我必须从一张桌子和它们的行中取出三列,然后以一种非常整洁的专业方法将它们发送给我的老板。我需要帮助,因为我在下面使用的方法看起来非常荒谬并且没有对齐电子邮件的形式。你们能帮我把它转换成 HTML 和正确的格式,让它看起来很整洁。

static void Main(string[] args)
{
    SqlConnection myConnection = new SqlConnection("mydatasourceinfo");
    // Connect to the SQL database using a SQL SELECT query to get all 

    string sqlStr = " select * from thedifference ";
    SqlDataAdapter myCommand = new SqlDataAdapter(sqlStr, myConnection);

    // Create and fill a DataSet.
    DataSet ds = new DataSet();
    myCommand.Fill(ds);

    DataView source = new DataView(ds.Tables[0]);
    SmtpClient smtp = new SmtpClient();

    string from = "xxx.com";
    string to = "xxx.com";
    string subject = "data differences";
    string body = "ClientName      |     Salesdiff        |       CallsDiff";

    foreach (DataRow Row in ds.Tables[0].Rows)
    {
        body = body + " " + "\n" + Row["clientname"] + "  " + "                                       " + Row["salesdiff"] + "                                                                                                  " + Row["callsdiff"] + "\n";
    }

    smtp.Host = "xxx.com";

    smtp.Send(from, to, subject, body);         
}
4

2 回答 2

0

请参阅使用 SQL Server 和 XML 构建 HTML 电子邮件链接。这应该可以很好地完成您的工作。

于 2013-11-08T13:24:26.227 回答
0

您必须将 IsBodyHtml 设置为 true。

        var htmlBody = new StringBuilder();
        htmlBody.Append("<table>");
        // TODO: generate the message content
        htmlBody.Append("</table>");

        var msg = new MailMessage();
        msg.IsBodyHtml = true;
        msg.From = "";
        msg.Body = htmlBody.ToString();

        var smtp = new SmtpClient();
        smtp.Send(msg);

要创建一个漂亮的消息,您可以使用通常的 html 标签,如tableor p(但是,html 和 css 在电子邮件中存在限制,因为许多电子邮件客户端不正确支持它们)。

于 2013-11-08T12:31:12.013 回答