0

I want to open mail to and inside the body of my email I want to create a table and insert values inside my model. So I execute outlook like this:

 var mail = $"mailto:test@test.com?subject=ProjectListTest&body={finalString}";

My question is, how can I create a table and add to body of mailto?

Table headers: Name, Customer

so inside each row I want to use something like:

var finalString = string.Empty;
foreach(var customer in CustomerList)
 {
      finalString = finalString + customer.Name + customer.CustomerKey
 }

Is it possible to achieve this? what is the correct format to create a table in Outlook. Regards

Ruben
  • 155
  • 1
  • 9

2 Answers2

1

pIf the table would be created using the html mail body format, then you can use the following method to generate it:

public string GenerateMailBodyWithTable(List<Customer> customers)
{
    StringBuilder stringBuilder = new StringBuilder();

    stringBuilder.Append($"<html>{ Environment.NewLine }<body>{ Environment.NewLine }");

    if (customers.Count > 0)
    {
        stringBuilder.Append($"<table><tr><th>Name</th><th>Key</th></tr>{ Environment.NewLine }");

        foreach (Customer customer in customers)
        {
            stringBuilder.Append($"<tr><th>{ customer._name }</th><th>{ customer._key }</th></tr>{ Environment.NewLine }");
        }

        stringBuilder.Append($"<table>{ Environment.NewLine }");
    }
    else
    {
        stringBuilder.Append($"<p>No customers<p>{ Environment.NewLine }");
    }

    stringBuilder.Append($"</html>{ Environment.NewLine }</body>");

    return stringBuilder.ToString();
}

After generating the html body you can perform the following action to fill the mailbody:

Outlook.Application outlookApp = new Outlook.Application();
        Outlook.MailItem mailMessage = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);

        mailMessage.HTMLBody = GenerateMailBodyWithTable(customers);
        mailMessage.Display(true);

Don't forget to place this using statement:

using Outlook = Microsoft.Office.Interop.Outlook;
Ruben
  • 155
  • 1
  • 9
Iliass Nassibane
  • 651
  • 7
  • 15
0

First at all, you have to create your custom HTML:

string finalString = "<table><tr><td><b>Name</b></td><td><b>Customer</b></td></tr>";
foreach(var customer in CustomerList)
{
    finalString += "<tr><td>" + customer.Name + "</td><td>" + customer.CustomerKey + "</td></tr>";
}
finalString += "</table>";

If you are using WinForms and you want to send an email with this body, you can use MailMessage Class from System.Net.Mail and sending it with SmtpClient. This way:

MailMessage mail = new MailMessage("from", "mailto", "Subject", finalString);
mail.IsBodyHtml = true; //Important
SmtpClient smtp = new SmtpClient("serverSMTP");
smtp.EnableSsl = USE_SSL;
smtp.Port = YOUR_PORT;
smtp.Credentials = new System.Net.NetworkCredential("email", "password");
smtp.Send(correo);

If you want to simulate a "mailto" action, you can use:

string command = $"mailto:test@test.com?subject=ProjectListTest&body={finalString}";  
Process.Start(command); 

Regards

  • I try to use in with mailto as you suggest but it just print html code into outlook body instead create table – Ruben May 05 '20 at 01:58
  • I dont understand. If you read the email with Outlook, this show you HTML code instead of the table? Did you try to send the email to another mailbox? Gmail? The result is the same? – Gonzalo Bustamante May 05 '20 at 03:08