How to use the Microsoft Outlook Object Library to send an HTML formatted message by using Visual C#

Article translations Article translations
Article ID: 310262 - View products that this article applies to.
This article was previously published under Q310262
Expand all | Collapse all

INTRODUCTION

This article describes how to use the Microsoft Outlook 2002 Object Library or the Microsoft Office Outlook 2003 Object Library to send an HTML formatted message by using Microsoft Visual C#.

MORE INFORMATION

To use the Outlook 2002 Object Library or the Outlook 2003 Object Library to send an HTML formatted message by using Microsoft Visual C#, follow these steps:
  1. In Microsoft Visual Studio .NET or in Microsoft Visual Studio 2005, create a new Console Application project:
    1. On the File menu, point to New, and then click Project.
    2. Under Project Types, click Visual C# Projects.

      Note In Microsoft Visual C# 2005, click Visual C# under Project Types.
    3. Under Templates, click Console Application.
    4. Click OK. By default, a file that is named Class1.cs is created.

      Note In Microsoft Visual C# 2005, Program.cs is created by default.
  2. Add a reference to either the Outlook 2002 Object Library or the Outlook 2003 Object Library. To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. Click the COM tab.
    3. On the COM tab, click Microsoft Outlook 11.0 Object Library if you are using Outlook 2003, or click Microsoft Outlook 10.0 Object Library if you are using Outlook 2002.
    4. Click Select.

      Note In Visual C# 2005, you do not have to click Select.
    5. In the Add References dialog box, click OK.

      Note If you receive a message to generate wrappers for the libraries that you selected, click Yes.
  3. In the Class1.cs code window, replace all the existing code with the following code:
    using System;
    using System.Reflection;     // to use Missing.Value
    // TO DO: If you use the Microsoft Outlook 11.0 Object Library, uncomment the following line.
    // using Outlook = Microsoft.Office.Interop.Outlook;
    
    namespace SendHTMLMail
    {
       public class Class1
       {
          public static int Main(string[] args)
          {
             try
             {
                // Create the Outlook application.
                Outlook.Application  oApp = new Outlook.Application();
    
                // Get the NameSpace and Logon information.
                Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
    
                // Log on by using a dialog box to choose the profile.
                oNS.Logon(Missing.Value, Missing.Value, true, true); 
    
                // Alternate logon method that uses a specific profile.
                // TODO: If you use this logon method, 
                //  change the profile name to an appropriate value.
                //oNS.Logon("YourValidProfile", Missing.Value, false, true); 
    			
                // Create a new mail item.
                Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
    
                // Set the subject.
                oMsg.Subject = "Send Using OOM in C#";
    
                // Set HTMLBody.
                String sHtml;
                sHtml = "<HTML>\n" + 
                   "<HEAD>\n" +
                   "<TITLE>Sample GIF</TITLE>\n" +
                   "</HEAD>\n" +
                   "<BODY><P>\n" + 
                   "<h1><Font Color=Green>Inline graphics</Font></h1></P>\n" +
                   "</BODY>\n" + 
                   "</HTML>";
                oMsg.HTMLBody = sHtml;
    
                // Add a recipient.
                Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
                // TODO: Change the recipient in the next line if necessary.
                Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("email address");
                oRecip.Resolve();
    
                // Send.
                oMsg.Send();
    
                // Log off.
                oNS.Logoff();
    
                // Clean up.
                oRecip = null;
                oRecips = null;
                oMsg = null;
                oNS = null;
                oApp = null;
             }
    
             // Simple error handling.
             catch (Exception e)
             {
                Console.WriteLine("{0} Exception caught.", e);
             }  
    
             // Default return value.
             return 0;
      
          }
       }
    }
  4. In this code, make any necessary changes where you see the "TO DO" comments.
  5. Press F5 to build and then run the program.
  6. Verify that the e-mail message has been sent and has been received.

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web site:
http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx
For more information about the Outlook 2002 e-mail security features and about how those features can affect custom solutions, click the following article number to view the article in the Microsoft Knowledge Base:
290500 Description of the developer-related e-mail security features in Outlook 2002

Properties

Article ID: 310262 - Last Review: June 29, 2007 - Revision: 5.7
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
Keywords: 
kbinterop kboutlookobj kbcode kbhowto KB310262

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com