none
How to convert part of text file with space split to table in Email body in C# RRS feed

  • Question

  • Hello All,

    Basically, I want to convert part of text file to a table that is added to en email body

    My text file would look like this:

    -----------------------------

    This is a summary of people involved in this activity

    Sara, Deparment1, 30

    Luca, Department2, 45

    Lo,     Department3, 34

    Thanks

    ------------------------------------

    The code should Take the middle part of the text file, read it and then put it into table in an email body.

    I was able to read the file using stramreader and create the email body but not yet convert the middle part of my text file into a table that can be added grammatically to an email body. Please help

    Thursday, November 7, 2019 1:26 PM

Answers

  • Hi AA_NED, 

    Thank you for posting here.

    For your question, you want to send a table to email.

    You can refer to the following code.

            private void button1_Click(object sender, EventArgs e)
            {
                string path = @"your file path ";
    
                string messageBody = "";
                string htmlTableStart = "<table style=\"border-collapse:collapse; text-align:center;\" >";
                string htmlTableEnd = "</table>";
                string htmlHeaderRowStart = "<tr style =\"background-color:#6FA1D2; color:#ffffff;\">";
                string htmlHeaderRowEnd = "</tr>";
                string htmlTrStart = "<tr style =\"color:#555555;\">";
                string htmlTrEnd = "</tr>";
                string htmlTdStart = "<td style=\" border-color:#5c87b2; border-style:solid; border-width:thin; padding: 5px;\">";
                string htmlTdEnd = "</td>";
    
                messageBody = messageBody + htmlTableEnd;
                messageBody += htmlTableStart;
                messageBody += htmlHeaderRowStart;
                messageBody += htmlTdStart + "Name" + htmlTdEnd;
                messageBody += htmlTdStart + "Department" + htmlTdEnd;
                messageBody += htmlTdStart + "Age" + htmlTdEnd;
                messageBody += htmlHeaderRowEnd;
    
    
                using (var reader = new StreamReader(path))
                {
                    while (!reader.EndOfStream)
                    {
                        var line = reader.ReadLine();
    
                        if (line != null && line.Contains(","))
                        {
                           var parts = Array.ConvertAll(line.Split(','),
                                lineItem => lineItem.Trim());
                            messageBody = messageBody + htmlTrStart;
                            for (int i = 0; i < parts.Length; i++)
                            {                          
                                messageBody = messageBody + htmlTdStart + parts[i] + htmlTdEnd;                    
                            }
                            messageBody = messageBody + htmlTrEnd;
                        }
                    }
                }
                sendEmail(messageBody);
                MessageBox.Show("Success");          
            }
    
            public void sendEmail(string content)
            {
                MailAddress from = new MailAddress("email");
                MailAddress to = new MailAddress("email");
                MailMessage message = new MailMessage(from, to);
                message.Subject = "Test";
                message.IsBodyHtml = true;
                message.Body = content;
                string account = @"email";
                string password = @"password";
                SmtpClient smtp = new SmtpClient("Smtp.live.com", 587);
                smtp.EnableSsl = true;
                NetworkCredential NetworkCred = new NetworkCredential(account, password);
                smtp.Credentials = NetworkCred;
                smtp.Send(message);
            }
    

    Data in my text file.

    The email I received.

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by AA_NED Monday, November 11, 2019 10:19 AM
    Friday, November 8, 2019 6:55 AM
    Moderator

All replies

  • To be clear, you want this in a table?

    Sara, Deparment1, 30
    Luca, Department2, 45
    Lo,     Department3, 34

    Edit, try this if you want each line as per above into a tr/td of a table.

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                var fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, 
                    "TextFile1.txt");
    
                var itemList = new List<LineItems>();
    
                using (var reader = new StreamReader(fileName))
                {
                    while (!reader.EndOfStream)
                    {
                        var line = reader.ReadLine();
    
                        if (line != null && line.Contains(","))
                        {
                            var parts = Array.ConvertAll(line.Split(','), 
                                lineItem => lineItem.Trim());
    
                            itemList.Add(new LineItems()
                            {
                                Item1 =  parts[0],
                                Item2 = parts[1],
                                Item3 = parts[2]
                            });
    
                        }
                    }
                }
    
                var sb = new StringBuilder();
                sb.AppendLine("<table>");
                foreach (var item in itemList)
                {
                    sb.AppendLine(item.ToString());
                }
                sb.AppendLine("</table>");
    
                Console.WriteLine(sb.ToString());
            }
            
        }
    
        public class LineItems
        {
            public string Item1 { get; set; }
            public string Item2 { get; set; }
            public string Item3 { get; set; }
            public override string ToString()
            {
                return $"    <tr><td>{Item1}</td> <td>{Item2}</td><td>{Item3}</td></tr>";
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Thursday, November 7, 2019 1:41 PM
    Moderator
  • Hi AA_NED, 

    Thank you for posting here.

    For your question, you want to send a table to email.

    You can refer to the following code.

            private void button1_Click(object sender, EventArgs e)
            {
                string path = @"your file path ";
    
                string messageBody = "";
                string htmlTableStart = "<table style=\"border-collapse:collapse; text-align:center;\" >";
                string htmlTableEnd = "</table>";
                string htmlHeaderRowStart = "<tr style =\"background-color:#6FA1D2; color:#ffffff;\">";
                string htmlHeaderRowEnd = "</tr>";
                string htmlTrStart = "<tr style =\"color:#555555;\">";
                string htmlTrEnd = "</tr>";
                string htmlTdStart = "<td style=\" border-color:#5c87b2; border-style:solid; border-width:thin; padding: 5px;\">";
                string htmlTdEnd = "</td>";
    
                messageBody = messageBody + htmlTableEnd;
                messageBody += htmlTableStart;
                messageBody += htmlHeaderRowStart;
                messageBody += htmlTdStart + "Name" + htmlTdEnd;
                messageBody += htmlTdStart + "Department" + htmlTdEnd;
                messageBody += htmlTdStart + "Age" + htmlTdEnd;
                messageBody += htmlHeaderRowEnd;
    
    
                using (var reader = new StreamReader(path))
                {
                    while (!reader.EndOfStream)
                    {
                        var line = reader.ReadLine();
    
                        if (line != null && line.Contains(","))
                        {
                           var parts = Array.ConvertAll(line.Split(','),
                                lineItem => lineItem.Trim());
                            messageBody = messageBody + htmlTrStart;
                            for (int i = 0; i < parts.Length; i++)
                            {                          
                                messageBody = messageBody + htmlTdStart + parts[i] + htmlTdEnd;                    
                            }
                            messageBody = messageBody + htmlTrEnd;
                        }
                    }
                }
                sendEmail(messageBody);
                MessageBox.Show("Success");          
            }
    
            public void sendEmail(string content)
            {
                MailAddress from = new MailAddress("email");
                MailAddress to = new MailAddress("email");
                MailMessage message = new MailMessage(from, to);
                message.Subject = "Test";
                message.IsBodyHtml = true;
                message.Body = content;
                string account = @"email";
                string password = @"password";
                SmtpClient smtp = new SmtpClient("Smtp.live.com", 587);
                smtp.EnableSsl = true;
                NetworkCredential NetworkCred = new NetworkCredential(account, password);
                smtp.Credentials = NetworkCred;
                smtp.Send(message);
            }
    

    Data in my text file.

    The email I received.

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by AA_NED Monday, November 11, 2019 10:19 AM
    Friday, November 8, 2019 6:55 AM
    Moderator
  • Thanks all for quick and positive reactions. I will work both solutions out and will get back to you. Your help is highly appreciated.
    Friday, November 8, 2019 10:10 AM
  • Hi AA_NED,

    Is your problem solved? If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 11, 2019 1:13 AM
    Moderator
  • Hi Xingyu Zhao,

    Yes Actually, It is finally solved.

    Thank you very much your solution works perfectly for me.

    Regards,

    AA_NED

    Monday, November 11, 2019 10:19 AM