Barak Rosenfeld
Barak Rosenfeld

Reputation: 337

sending a data table with outlook API in C#

I have a data table and I want to send it out with an email using the outlook API how can I do that?

code example would be great. I am attaching the meanwhile message I want to send

private Microsoft.Office.Interop.Outlook.MailItem mailItem; // an email item.

public void SetUpMessage(string SendTo,string CC, string subject, string messageBody,string Link)                
{
    app = new Microsoft.Office.Interop.Outlook.Application();
    mailItem = app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);

    mailItem.To = SendTo;
    mailItem.CC = CC;
    mailItem.Subject = subject;
    mailItem.HTMLBody = messageBody + "<a href=\"" + Link + "\">Link</ID></a>";            
}

Upvotes: 0

Views: 2449

Answers (2)

Eugene Astafiev
Eugene Astafiev

Reputation: 49455

You may also consider using the Word object model for manipulating the body of emails. The Chapter 17: Working with Item Bodies article describes all possible ways in depth.

Also I'd recommend using the Resolve or ResolveAll methods to resolve a Recipient object against the Address Book.

You may find a sample code in the How To: Create and send an Outlook message programmatically article.

Upvotes: 1

Nick Otten
Nick Otten

Reputation: 712

I'm not sure what kind of data you have in the data table. But if its just a simple text table you could use the html table tag in your htmlBody. Just create a simple loop trough the columns of the data table to create your header row and then a loop trough your cell's to add the values.

a quick idea of the code given that the datatable is called dtData (note I just wrote this quickly out of my head so it may contain a type or two, but the idea should be clear enough)

                    StringBuilder table = new StringBuilder();                    
                    table.Append("<table style=\"width:100%\">\n<tr>\n");
                    int columnsCount = dtData.Columns.Count;
                    foreach (DataColumn column in dtData.Columns)
                    {
                        table.Append("<td>" + column.ColumnName + "</td>\n");
                    }
                    table.Append("</tr>\n");
                    foreach (DataRow row in dtData.Rows)
                    {
                        table.Append("<tr>\n");
                        for (int i = 0; i < columnsCount; i++)
                        {
                            table.Append("<td>" + row[i] + "</td>\n");
                        }
                        table.Append("</tr>\n");
                    }
                    table.Append("</table>\n");

                    //use table as your messagebody into given code

Upvotes: 1

Related Questions