Home > OS >  Adding List data as a table to the Mail Body C#
Adding List data as a table to the Mail Body C#

Time:01-20

I'm sending a mail to the users from my ASP.NET MVC application.

With the mail body details, I want to add the list of data as a table to the body also.

Help is needed to guide me to resolve the issue.

The mail is like requesting documents from the users and the required document list is on this list. This can be included record 1 or more than 1

 List<ReqDocForMail> docList = new List<ReqDocForMail>();

Model is

public class ReqDocForMail
{
    public string FileType { get; set; }
    public string Note { get; set; }
}

string Subject = "Document Request for Task Number ";
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("sendingmailaddress", "MailNameShows");
mailMessage.To.Add(User.Email_Primary);
mailMessage.Subject = "Subject";
string Body; Body = @" 
  <html>
   <body>
    <p>Dear "   CusGen   Customer.Sur_Name   " "   Customer.Name   @",</p>
    <br />Thank you </p>
    <br />
    <br />Your Task is under processing. <br />
    <br />To continue the task you need to upload the following documents. <br />
    <br />Login to the Site and go to the <b>In Process Tasks</b>
    <br />
    <br />Open the request :"   TaskDetails.TaskNumber   @". <br />
    <br />Move to the <b>Upload Documents</b> section and upload the following documents. 
    //Here I want to show the list data [File Type | Note] as a table //
   </body>
  </html>";

CodePudding user response:

You can create a inherited class from List<> and add GetHTML() method to it.

pseudo code:

public class ReqDocForMail
{
    public string FileType { get; set; }
    public string Note { get; set; }
}

public class ReqDocList: List<ReqDocForMail>
{
    public string GetHTML()
    {
        string html = "";
        foreach(ReqDocForMail doc in this)
        {
            html = doc.Note   "";//YOUR HTML FORMATTING HERE
        }
        return html;
    }
}

void Main()
{
    ReqDocList lst = new ReqDocList();
    
    string lstHTML = lst.GetHTML();
}

Approach 2
Create an extension method to get HTML, again some psuedo:

public static class ReqDocListExtension
{
    public static string GetHTML(this List<ReqDocForMail> lst)
    {
        string html = "";
        foreach(ReqDocForMail doc in lst)
        {
            html = doc.Note   "";//YOUR HTML FORMATTING HERE
        }
        return html;
    }
}

void Main()
{
    List<ReqDocForMail> lst = new List<ReqDocForMail>();
    
    string lstHTML = lst.GetHTML();
}
  • Related