Click here to Skip to main content
Click here to Skip to main content

Attaching Files and Creating New Mails in Outlook

, 11 Dec 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
Code to open the "New Message" window in Outlook and attach items

Introduction

I have searched the net for many articles that can open the simple mail interface and attach an item an keep the "New Message" window ready for execution. Almost all the articles either suggested MAPI or a simple mailto: link. Now, MAPI has memory leaks which make it difficult to use and "mailto: " doesn't work for attachment always.

Background

Hence I researched the Microsoft.Office.Interop.Outlook namespace. The other codes on the internet allow us to send mail while Outlook is open, while Outlook is closed: most of the code throws an exception. Hence I started using the MailItem class in the same namespace and found a solution to attach files, and create the "New message" window without actually having to run Outlook or use MAPI.

Using the Code

The code uses the Microsoft.Office.Interop.Outlook component. For this component to be present, we must have Outlook installed on our system. Then when we go to the solution explorer and access the references node and choose add a new reference from the context menu, we will be able to access this component in the "Add reference" window under the .NET tab as shown in the picture below:

Then we can add the following code:

using Outlook=Microsoft.Office.Interop.Outlook;    

Class ClassDisplayMail
{
    Public void DisplayOutlook()
    {
         Outlook.Application objApp =new Outlook.Applicaiton();
         Outlook.MailItem mail=null;
         mail=(Outlook.MailItem)objApp.CreateItem(Outlook.OlItemType.olMailItem);
         //The CreateItem method returns an object which has to be typecast to MailItem 
         //before using it.
         mail.Attachments.Add((object)@"C:\me.doc",
		Outlook.OlAttachmentType.olEmbeddeditem,
     1, (object)"Attachment");
    //The parameters are explained below
    mail.To="me@abc.com;test@def.com";
    mail.Cc="con@def.com";//All the mail lists have to be separated by the ';'
    }
}
//Then we can use either of the following: Add the following code after mail.Cc

//To send email:
mail.Send();
//To show email window
mail.Display();

The Add method of the Attachments of the MailItem class has 4 parameters:

  1. The first parameter: The file path to be attached.
  2. The second parameter: The attachment type, by default we should use embedItem to get the attachment, it has other values like ByValue and ByReference to use which you can refer to MSDN.
  3. The 3rd parameter is order in which you want the attachment to be added, 1 means 1st attachment, 2 means 2nd attachment, etc.
  4. The 4th parameter is the display name we want to give to the attachment, if you want the file name you can use the FileInfo.Name method of the FileInfo class to do so.

Points of Interest

By using the different OlItemTypes parameter in the CreateItem method of the Application interface, we can create calendar invites, tasks notes, etc. Just use the OlItemTypes.olCalendar, or OlItemTypes.Notes, etc.

History

  • 11th December, 2009: Initial post

This is my first ever post on The Code Project. Thanks for this website's support during many times of need. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

SreeniTheGinie
Product Manager Nexshore Technologies Pvt. Ltd.
India India
I am working on Microsoft Technologies for 4 years, during my free time i love to play cricket, basketball and a lot of video games.

Comments and Discussions

 
GeneralMy vote of 5 PinmemberTheCrazyTim23-May-13 16:13 
GeneralVery good article Pinmemberspeedinveins11-Dec-09 4:56 
GeneralMy vote of 1 Pinmemberyunusatm10-Dec-09 20:08 
QuestionWhy do it this way ? Pinmembergaurav_verma_mca10-Dec-09 19:14 
AnswerRe: Why do it this way ? PinmemberSreeniTheGinie11-Dec-09 1:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141220.1 | Last Updated 11 Dec 2009
Article Copyright 2009 by SreeniTheGinie
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid