'How to send a string as MJML with MailKit in ASP.NET core?

I want to send *Account_Name, Account, Date, Type, Income, Expense

  • Between two dates(startDate and endDate) Via email

SO I built email body like this:

            var model = new AllTransactionViewModel();
                model = emailAllTransactionsList(startDate, endDate);
MimeMessage message = new MimeMessage();
                message.From.Add(new MailboxAddress("karnarthi", "[email protected]"));
                message.To.Add(MailboxAddress.Parse("[email protected]"));
                message.Subject = "Transactions between " + startDate + "and " + endDate;

                var bodyBuilder = new BodyBuilder();
                        bodyBuilder.HtmlBody = $"<table><tr><th>Account</th><th>Amount</th><th>Date</th><th>Type</th></tr>";
                            foreach (var item in model.AllTransactions)
                            {
                                bodyBuilder.HtmlBody += $"<tr><td>{item.Account_Name}</td><td>{item.Amount}</td><td>{item.Date}</td><td>{item.Type}</td></tr>";
                            }
                            foreach (var item in model.IncomeLists)
                            {
                                bodyBuilder.HtmlBody += $"<b>Total Income: {item.Income}</b><br>";
                            }
                            foreach (var item in model.ExpenseLists)
                            {
                                bodyBuilder.HtmlBody += $"<b>Total Expense: {item.Expense}</b><br>";
                            }
                            bodyBuilder.TextBody = "This is some plain text";

                        message.Body = bodyBuilder.ToMessageBody();

I am sending this message like this:

            SmtpClient client = new SmtpClient();
                try
                {
                    client.Connect("Smtp.gmail.com", 465, true);
                    client.Authenticate("[email protected]", "<snip>");
                    client.Send(message);
                    Console.WriteLine("----- EMAIL SENT!! -----");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                finally
                {
                    client.Disconnect(true);
                    client.Dispose();
                } 

I have sent it succesfully, In the end Email looks like this

Now I want to use MJML Framework and make it responsive

so I have changed the email body like this:

            var model = new AllTransactionViewModel();
                model = emailAllTransactionsList(startDate, endDate);
                Console.WriteLine(model.AllTransactions);
    
                MimeMessage message = new MimeMessage();
                message.From.Add(new MailboxAddress("santosh kumar", "[email protected]"));
                message.To.Add(MailboxAddress.Parse("[email protected]"));
                message.Subject = "Transactions between " + startDate + "and " + endDate;

var bodyBuilder = new BodyBuilder();
bodyBuilder.HtmlBody = $"\<mjml\>\< mj - body \>\< mj - section \>\< mj - column \>\< mj - table \>\< tr \>\< th \> Account \</ th \>\< th \> Amount \</ th \>\< th \> Date \</ th \>\< th \> Type \</ th \> \</ tr \> \<tr\> ";
foreach (var item in model.AllTransactions)
{
bodyBuilder.HtmlBody += $" \< tr \>\< td \>{item.Account_Name}\</ td \>\< td  \> {item.Amount}\</ td \>\< td \>{item.Date}\</ td \>\<td\>{item.Type}\</td\>\</ tr \>";
}
bodyBuilder.HtmlBody += $"\</tr\>\</ mj - table \>\</ mj - column \>\</ mj - section \>\</ mj - body \>\</ mjml \>";
bodyBuilder.TextBody += $"This is some plain text";
message.Body = bodyBuilder.ToMessageBody();

but this EMAIL looks like this Only email email syntax is sending,

How can I send a STYLED EMAIL with MJML wit these Deails

Please Help me With this, I am so Close to doing this



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source