Creating a new Outlook .msg file programmatically in C#
[code:c#]
// Creates a new Outlook Application Instance
Outlook.Application objOutlook = new Outlook.Application();
// Creating a new Outlook Message from the Outlook Application Instance
Outlook.MailItem mic = (Outlook.MailItem)(objOutlook.CreateItem(Outlook.OlItemType.olMailItem));
// Assigns the "TO", "CC" and "BCC" Fields
mic.To = toTextBox.Text;
mic.CC = ccTextBox.Text;
mic.BCC = bccTextBox.Text;
// Assigns the Subject Field
mic.Subject = subjectTextBox.Text;
// Switch the Importance ComboBox to identify the Mail Message Importance Level
switch (importanceComboBox.SelectedItem.ToString())
{
case "High":
mic.Importance = Outlook.OlImportance.olImportanceHigh;
break; case "Normal":
mic.Importance = Outlook.OlImportance.olImportanceNormal;
break;
case "Low":
mic.Importance = Outlook.OlImportance.olImportanceLow;
break;
}
// Define the Mail Message Body. In this example, you can add in HTML content to the mail message body
mic.HTMLBody = messageTextBox.Text;
// Adds Attachment to the Mail Message.
// Note: You could add more than one attachment to the mail message.
// All you need to do is to declare this relative to the number of attachments you have.
mic.Attachments.Add(attachmentOneTextBox.Text,Outlook.OlAttachmentType.olByValue,1,"Attachment Name");
// Save the message to C:\demo.msg. Alternatively you can create a SaveFileDialog to
// allow users to choose where to save the file
mic.SaveAs(@"C:\demo.msg", Outlook.OlSaveAsType.olMSG);
[/code]