如何在电子邮件正文中的c#中将行附加到表中?

时间:2016-11-30 07:35:16

标签: c# asp.net

您好我在我的mvc5应用程序中向用户发送电子邮件。我以表格的形式发送一些信息。我能够正确地发送所有信息但我在这里面临的问题就像邮件正文表单独出现一样。 这是我的代码。

 public static int sendemailonCommit(List<emailClass> item)
        {
            StringBuilder builder = new StringBuilder();
            string AdminEmail = ConfigurationManager.AppSettings["AdminEmail"].ToString();
            MailMessage mail = new MailMessage();
            mail.To.Add(item[0].emailID);
            mail.Bcc.Add(AdminEmail);
            mail.From = new MailAddress(MailID);
            builder.Append("<p style='color: #000000; background-color: #ffffff'>Hi " + item[0].uploadedBy + ",</p>");

            foreach (var data in item)
            {
            if (data.docStatus == "Approved")
            { 
            mail.Subject = "Status of Document in Our System";
            }
            else
            {
            mail.Subject = "Status of Document in Our System";
            }

            if (data.docStatus == "Approved")
            {
                builder.Append("<p style='color: #000000; background-color: #ffffff'>Your below documents has been Approved by the C3Card KYC System. <br> </ p>");
                builder.Append("<table border=1 ><tr>");
                builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Client Id" + "</th>");
                builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Employee ID" + "</th>");
                builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Employee Citizenid" + "</th>");
                builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Employee Name" + "</th>");
                builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Document type" + "</th>");
                builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Uploaded By" + "</th>");
                builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Updated On" + "</th>");
                builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Status" + "</th>");
                builder.Append("</tr>");
                builder.Append("<tr>");
                builder.Append("<td>" + data.clinetId + "</td>");
                builder.Append("<td>" + data.employeeID + "</td>");
                builder.Append("<td>" + data.employeecitiID + "</td>");
                builder.Append("<td>" + data.empName + "</td>");
                builder.Append("<td>" + data.docType + "</td>");
                builder.Append("<td>" + data.uploadedBy + "</td>");
                builder.Append("<td>" + data.uploadedOn + "</td>");
                builder.Append("<td>" + data.docStatus + "</td>");
                builder.Append("</tr></table>");
            }
            else
            {
                builder.Append("<p style='color: #000000; background-color: #ffffff'>Your below document has been Rejected by the C3Card KYC System. <br> </ p>");
                builder.Append("<table border=1 ><tr>");
                builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Client Id" + "</th>");
                builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Employee ID" + "</th>");
                builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Employee Citizenid" + "</th>");
                builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Employee Name" + "</th>");
                builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Document type" + "</th>");
                builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Uploaded By" + "</th>");
                builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Updated On" + "</th>");
                builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Status" + "</th>");
                builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Reject Comment" + "</th>");
                builder.Append("</tr>");
                builder.Append("<tr>");
                builder.Append("<td>" + data.clinetId + "</td>");
                builder.Append("<td>" + data.employeeID + "</td>");
                builder.Append("<td>" + data.employeecitiID + "</td>");
                builder.Append("<td>" + data.empName + "</td>");
                builder.Append("<td>" + data.docType + "</td>");
                builder.Append("<td>" + data.uploadedBy + "</td>");
                builder.Append("<td>" + data.uploadedOn + "</td>");
                builder.Append("<td>" + "Rejected" + "</td>");
                builder.Append("<td>" + data.rejectComment + "</td>");
                builder.Append("</tr></table>");
            }
            }
            builder.Append("<p style='color: #000000; background-color: #ffffff'></br>Thanks,</p>");
            builder.Append("<p style='color: #000000; background-color: #ffffff'></br>Admin</p>");
            mail.Body = builder.ToString();
            mail.IsBodyHtml = true;

            SmtpClient smtp = new SmtpClient(hostserver);
            if (IsInternalBuild)
            {
                smtp.Host = hostserver;
                smtp.Port = Convert.ToInt32(portno);
                smtp.UseDefaultCredentials = true;
                smtp.EnableSsl = true;
            }
            smtp.Credentials = new System.Net.NetworkCredential
            (MailID, Password);
            smtp.Send(mail);
            return 1;
        }

我收到邮件,但如果我的列表中有两个项目,那么foreach将执行两次。在一封电子邮件中,我将分别获得两个表,但我想将第一个表追加到第二个而不是生成新表。我可以对此有所了解吗?谢谢。

2 个答案:

答案 0 :(得分:2)

      builder.Append("<p style='color: #000000; background-color: #ffffff'>Your below documents has been Approved by the C3Card KYC System. <br> </ p>");
      builder.Append("<table border=1 ><tr>");

      mail.Subject = "Status of Document in Our System";

       if (data.docStatus == "Approved")
       {

            builder.Append("<table border=1 ><tr>");
            builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Client Id" + "</th>");
            builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Employee ID" + "</th>");
            builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Employee Citizenid" + "</th>");
            builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Employee Name" + "</th>");
            builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Document type" + "</th>");
            builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Uploaded By" + "</th>");
            builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Updated On" + "</th>");
            builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Status" + "</th>");
            builder.Append("</tr>");

        foreach (var data in item)
        {
            builder.Append("<tr>");
            builder.Append("<td>" + data.clinetId + "</td>");
            builder.Append("<td>" + data.employeeID + "</td>");
            builder.Append("<td>" + data.employeecitiID + "</td>");
            builder.Append("<td>" + data.empName + "</td>");
            builder.Append("<td>" + data.docType + "</td>");
            builder.Append("<td>" + data.uploadedBy + "</td>");
            builder.Append("<td>" + data.uploadedOn + "</td>");
            builder.Append("<td>" + data.docStatus + "</td>");
            builder.Append("</tr>");
         }

         builder.Append("</table>");

       }
       else
       {

        foreach (var data in item)
        {

            builder.Append("<table border=1 ><tr>");
            builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Client Id" + "</th>");
            builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Employee ID" + "</th>");
            builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Employee Citizenid" + "</th>");
            builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Employee Name" + "</th>");
            builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Document type" + "</th>");
            builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Uploaded By" + "</th>");
            builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Updated On" + "</th>");
            builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Status" + "</th>");
            builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Reject Comment" + "</th>");
            builder.Append("</tr>");
       }

           builder.Append("</table>");

       }

答案 1 :(得分:0)

只需将此部分放在foreach循环之前,然后从if-else块中删除

builder.Append("<p style='color: #000000; background-color: #ffffff'>Your below documents has been Approved by the C3Card KYC System. <br> </ p>");
builder.Append("<table border=1 ><tr>");
builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Client Id" + "</th>");
builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Employee ID" + "</th>");
builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Employee Citizenid" + "</th>");
builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Employee Name" + "</th>");
builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Document type" + "</th>");
builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Uploaded By" + "</th>");
builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Updated On" + "</th>");
builder.Append("<th style='font-family: Arial; font-size: 10pt;'>" + "Status" + "</th>");
builder.Append("</tr>");

完成foreach之后

builder.Append("</tr></table>");