如何在将电子邮件作为一条消息发送之前先绑定内容?

时间:2017-12-06 11:06:50

标签: c# email datatable

关于将电子邮件作为一条消息发送,我遇到了问题。代码读取数据库中的所有内容,但不会将消息作为电子邮件中的消息发送。它重复发送数据库中的行/数据。我如何首先绑定内容并将其作为一个发送到电子邮件?

  int Quantity = 0;
    string JobName;
    string OrderType;
    DateTime DueDate;

    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connect"].ConnectionString);

    string cmdText = "SELECT * FROM Shopping_Cart WHERE UID=@UIDD";

    SqlCommand cmd = new SqlCommand(cmdText, con);


    cmd.Parameters.AddWithValue("@UIDD", hfUserID.Value);


    if (con.State == ConnectionState.Closed)
    {
        con.Open();
    }
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[4] {
                        new DataColumn("QUANTITY", typeof(int)),
                        new DataColumn("JOB NAME", typeof(string)),
                        new DataColumn("ORDER TYPE", typeof(string)),
                        new DataColumn("DUE DATE", typeof(DateTime))});

    //===== Execute Query.
    SqlDataReader dr = cmd.ExecuteReader();
    if (Session["UID"] != null)
    {
        while (dr.Read())
        {
            Quantity = dr.GetInt32(4);
            JobName = dr.GetString(10);
            OrderType = dr.GetString(17);
            DueDate = dr.GetDateTime(9);

            dt.Rows.Add(Quantity, JobName, OrderType, DueDate);
        }
        StringBuilder YourTable = new StringBuilder();
        YourTable.Append("Thank you for choosing Junand's Labels Inc. Your order will be evaluated by the manager with the maximum of (3 days). Please wait for the confirmation of the manager in your email whether your order will be acceptable or not. The price will be announced once the order has been accepted. ");
        YourTable.Append("<br>");
        YourTable.Append("<br>");
        YourTable.Append("Order Review");
        YourTable.Append("<table border = '1'>");
        YourTable.Append("<tr>");
        foreach (DataColumn column in dt.Columns)
        {
            YourTable.Append("<th style = 'background-color: #0bd2d1;color:#ffffff'>");
            YourTable.Append(column.ColumnName);
            YourTable.Append("</th>");
        }
        YourTable.Append("</tr>");


        foreach (DataRow row in dt.Rows)
        {
            YourTable.Append("<tr>");
            foreach (DataColumn column in dt.Columns)
            {
                YourTable.Append("<td>");
                YourTable.Append(row[column]);
                YourTable.Append("</td>");

            }
            YourTable.Append("</tr>");
            MailMessage message = new MailMessage();
            message.Body += YourTable;
            message.To.Add(Session["Email"].ToString());
            message.From = new MailAddress("junandscontactus@gmail.com");
            message.Subject = "View your Orders";

            message.IsBodyHtml = true;



            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
            client.EnableSsl = true;
            client.Credentials = new System.Net.NetworkCredential("u@gmail.com", "password");

            client.Send(message);

        }

    }

Here is the sample output in the email

1 个答案:

答案 0 :(得分:0)

移动发送电子邮件

  client.Send(message);

以外的

foreach (DataRow row in dt.Rows)  

环。