Click here to Skip to main content
15,881,812 members
Articles / Productivity Apps and Services / Microsoft Office

Attaching Files and Creating New Mails in Outlook

Rate me:
Please Sign up or sign in to vote.
4.07/5 (9 votes)
11 Dec 2009CPOL2 min read 60.6K   1.5K   19   9
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:

Image 1

Then we can add the following code:

C#
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)


Written By
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

 
QuestionThanks bro Pin
Mohammad Imran122-Jan-23 23:07
Mohammad Imran122-Jan-23 23:07 
QuestionMany thanks, dear Pin
Member 19443651-Aug-16 1:55
Member 19443651-Aug-16 1:55 
QuestionMicrosoft.Office.Interop.Outlook Pin
Dave the Golfer16-Mar-15 11:42
Dave the Golfer16-Mar-15 11:42 
I am a beginner and are trying to format an email that I want to open in Outlook so the user can add the email addresses as appropriate.
I am using Visual Studio 2013 and have Office 365 loaded but I cannot find any references to Microsoft.Office.Interop.Outlook or any other of the Office references.
(Using Menu/Project/Add Reference to open the Reference Manager in Visual Studio)
How do I get this to be available?
AnswerRe: Microsoft.Office.Interop.Outlook Pin
SreeniTheGinie25-Jun-15 5:25
SreeniTheGinie25-Jun-15 5:25 
GeneralMy vote of 5 Pin
TheCrazyTim23-May-13 15:13
TheCrazyTim23-May-13 15:13 
GeneralVery good article Pin
speedinveins11-Dec-09 3:56
speedinveins11-Dec-09 3:56 
GeneralMy vote of 1 Pin
yunusatmaca10-Dec-09 19:08
yunusatmaca10-Dec-09 19:08 
QuestionWhy do it this way ? Pin
gaurav_verma_mca10-Dec-09 18:14
gaurav_verma_mca10-Dec-09 18:14 
AnswerRe: Why do it this way ? Pin
SreeniTheGinie11-Dec-09 0:47
SreeniTheGinie11-Dec-09 0: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.