Click here to Skip to main content
13,346,966 members (71,335 online)
Click here to Skip to main content
Add your own
alternative version


19 bookmarked
Posted 10 Dec 2009

Attaching Files and Creating New Mails in Outlook

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


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.


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;
         //The CreateItem method returns an object which has to be typecast to MailItem 
         //before using it.
     1, (object)"Attachment");
    //The parameters are explained below
    mail.Cc="";//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:
//To show email window

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.


  • 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. 


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


About the Author

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.

You may also be interested in...


Comments and Discussions

QuestionMany thanks, dear Pin
Member 19443651-Aug-16 2:55
memberMember 19443651-Aug-16 2:55 
QuestionMicrosoft.Office.Interop.Outlook Pin
Dave the Golfer16-Mar-15 12:42
memberDave the Golfer16-Mar-15 12:42 
AnswerRe: Microsoft.Office.Interop.Outlook Pin
SreeniTheGinie25-Jun-15 6:25
memberSreeniTheGinie25-Jun-15 6:25 
GeneralMy vote of 5 Pin
TheCrazyTim23-May-13 16:13
memberTheCrazyTim23-May-13 16:13 
GeneralVery good article Pin
speedinveins11-Dec-09 4:56
memberspeedinveins11-Dec-09 4:56 
GeneralMy vote of 1 Pin
yunusatm10-Dec-09 20:08
memberyunusatm10-Dec-09 20:08 
QuestionWhy do it this way ? Pin
gaurav_verma_mca10-Dec-09 19:14
membergaurav_verma_mca10-Dec-09 19:14 
Now you code will do exactly what you are trying to acheive. But why do it this way.

1. We are now bound to have outlook on the machine.

If I want to send emails why not use simple smtp classes

you may wanna check out this article Email Notification Framework[^]
AnswerRe: Why do it this way ? Pin
SreeniTheGinie11-Dec-09 1:47
memberSreeniTheGinie11-Dec-09 1:47 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 11 Dec 2009
Article Copyright 2009 by SreeniTheGinie
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid