Microsoft KB Archive/310263

= How to use the Microsoft Outlook Object Library to send a message that has attachments by using Visual C# =

Article ID: 310263

Article Last Modified on 6/29/2007

-

APPLIES TO


 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Office Outlook 2003
 * Microsoft Outlook 2002 Standard Edition
 * Microsoft Visual C# 2005

-



This article was previously published under Q310263



SUMMARY
This article describes how to use the Microsoft Outlook 2002 Object Library or the Microsoft Office Outlook 2003 Object Library to send a message (that is, a MailItem object) that contains attachments by using Microsoft Visual C#.



MORE INFORMATION
To use the Microsoft Outlook Object Library to send a message that contains attachments, follow these steps:  Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. Under Project Types, click Visual C# Projects.

Note In Visual Studio 2005, click Visual C# under Project Types. Under Templates, click Console Application.

By default, Class1.cs is created.

Note In Microsoft Visual Studio 2005, Program.cs is created by default. Add a reference to the Microsoft Outlook 2002 Object Library or to the Microsoft Office Outlook 2003 Object Library. To do so, follow these steps:  On the Project menu, click Add Reference. Click the COM tab, click the Microsoft Outlook Object Library that you want to use, and then click Select.

Note In Visual Studio 2005, you do not have to click Select.</li> In the Add References dialog box, click OK.</li> If you are prompted to generate wrappers for the libraries that you selected, click Yes.</li></ol> </li>  In the Class1.cs code window, replace the code with the following: using System; //TO DO: If you use the Microsoft Outlook 11.0 Object Library, uncomment the following line. //using Outlook = Microsoft.Office.Interop.Outlook;

namespace Outlook_SendMailItem {   public class Class1 {       public static int Main(string[]args) {           try {               // Create the Outlook application by using inline initialization. Outlook.Application oApp = new Outlook.Application;

//Create the new message by using the simplest approach. Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);

//Add a recipient. // TODO: Change the following recipient where appropriate. Outlook.Recipient oRecip = (Outlook.Recipient)oMsg.Recipients.Add(&quot;e-mail address&quot;); oRecip.Resolve;

//Set the basic properties. oMsg.Subject = &quot;This is the subject of the test message&quot;; oMsg.Body = &quot;This is the text in the message.&quot;;

//Add an attachment. // TODO: change file path where appropriate String sSource = &quot;C:\\setupxlg.txt&quot;; String sDisplayName = &quot;MyFirstAttachment&quot;; int iPosition = (int)oMsg.Body.Length + 1; int iAttachType = (int)Outlook.OlAttachmentType.olByValue; Outlook.Attachment oAttach = oMsg.Attachments.Add(sSource,iAttachType,iPosition,sDisplayName);

// If you want to, display the message. // oMsg.Display(true); //modal

//Send the message. oMsg.Save; oMsg.Send;

//Explicitly release objects. oRecip = null; oAttach = null; oMsg = null; oApp = null; }

// Simple error handler. catch (Exception e)           { Console.WriteLine(&quot;{0} Exception caught: &quot;, e); }

//Default return value. return 0;

}

} } </li> Search for TODO in the code, and then modify the code for your environment.</li> Press F5 to build and to run the program.</li> Make sure that the e-mail message has been sent and received.</li></ol>

Note The security features in Outlook will cause one or more prompts to occur before the message is sent. For more information, see the &quot;References&quot; section.

<div class="references_section">