Niranjan Godbole
Niranjan Godbole

Reputation: 2175

How to append rows to table in c# in email body?

Hi I am sending email to users in my mvc5 application. I am sending some information in the form of tables. I am able to send all the information properly but i am facing problems here like in mail body tables are coming separately. This is my code.

 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;
        }

I am getting mail but if i have two items in my list so foreach will execute twice. In one email I will get two tables separately but i want to append first table to second instead of generating new table. May i have some idea on this? Thank you.

Upvotes: 1

Views: 4338

Answers (2)

go..
go..

Reputation: 948

      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>");

       }

Upvotes: 3

Jaydip Jadhav
Jaydip Jadhav

Reputation: 12309

Just put this part before foreach loop and remove from if-else block

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>");

And after completing foreach put

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

Upvotes: 0

Related Questions