zaterdag 3 oktober 2009

Programmatically creating an email in Outlook 2007

This code creates a new email object, reads an html file to set as its body, adds any attachments from a list of filenames and saves it in the drafts folder of your Outlook client.
using Outlook = Microsoft.Office.Interop.Outlook;

Outlook.Application otlApp = new Outlook.Application();
Outlook._MailItem otlMail = (Outlook._MailItem)otlApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
otlMail.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;

string html = "";
System.IO.Stream stream = File.OpenRead(filename);
System.IO.StreamReader reader = new System.IO.StreamReader(stream);
html = reader.ReadToEnd();
reader.Close();

otlMail.HTMLBody = html;
object oAttachType = Outlook.OlAttachmentType.olByReference;
foreach (string attachmentPath in attachmentPaths)
{
Outlook.Attachment oAttach = otlMail.Attachments.Add(attachmentPath, oAttachType, oMissing, oMissing);
}
otlMail.Display(false);
otlMail.Save();

Geen opmerkingen:

Een reactie posten