More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

share code 23-May-2019 09:49

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

Code level: beginner    Code area: Outlook and .NET Printer Friendly Version
Title: Basic Outlook Automation
Description: How to Aoutomate Outlook from C#, Basics
Date: 22-Feb-2005  08:05
Code level: beginner
Code area: Outlook and .NET
Posted by: Helmut Obertanner
This message is displayed as VB.NET
 /// This Code is adopted from Microsoft Knowlegdebase Article:  <>

using System.Runtime.InteropServices ; 
using Outlook = Microsoft.Office.Interop.Outlook  ;

// The Outlook Application Object
Outlook.ApplicationClass myOutlookApplication = null;

// Create the Application Object
myOutlookApplication = new Outlook.ApplicationClass ();

// Get the Namespace Object
Outlook.NameSpace myNameSpace = myOutlookApplication.GetNamespace("MAPI");

// Define a missing Object for COM interop
object myMissing = System.Reflection.Missing.Value ;  

// Logon to Namespace
myNameSpace.Logon(myMissing, myMissing, myMissing, myMissing);

// Create a new Contact
Outlook.ContactItem myNewContact = (Outlook.ContactItem) myOutlookApplication.CreateItem (Outlook.OlItemType.olContactItem );

// Setup Contact information...
myNewContact.FullName = "Helmut Obertanner";
System.DateTime myBirthDate = DateTime.Parse ("1.6.1968");
myNewContact.Birthday = myBirthDate;
myNewContact.CompanyName = "DATALOG Software AG";
myNewContact.HomeTelephoneNumber = "49-89-888888888";
myNewContact.Email1Address = "";
myNewContact.JobTitle = "Developer";
myNewContact.HomeAddress = "Frundsbergstr. 60\n80637 Munich";

// Save Contact

// CleanUp
Marshal.ReleaseComObject (myNewContact);

// Create a new Appointment
Outlook.AppointmentItemClass myNewAppointment = (Outlook.AppointmentItemClass) myOutlookApplication.CreateItem (Outlook.OlItemType.olAppointmentItem );

// Schedule it for two minutes from now...
DateTime myAppointmentDate = DateTime.Now.AddMinutes (2.0);
myNewAppointment.Start = myAppointmentDate;

// Set other appointment info...
myNewAppointment.Duration = 60;
myNewAppointment.Subject = "Meeting to discuss plans...";

myNewAppointment.Body = "Meeting with Helmut to discuss plans.";
myNewAppointment.Location = "Home Office";
myNewAppointment.ReminderMinutesBeforeStart = 1;
myNewAppointment.ReminderSet = true;

// Save Appointment

// CleanUp
Marshal.ReleaseComObject (myNewAppointment);

// Create a Mail Object
Outlook.MailItemClass myNewMail = (Outlook.MailItemClass) myOutlookApplication.CreateItem (Outlook.OlItemType.olMailItem  ); 

myNewMail.To = "";
myNewMail.Subject = "About our meeting...";
myNewMail.Body = "Hi James,\n\n" +
				"\tI'll see you in two minutes for our meeting!\n\n" +
				"Btw: I've added you to my contact list!";

// Send the message!

// CleanUp
Marshal.ReleaseComObject (myNewMail);

myNameSpace.Logoff ();
Marshal.ReleaseComObject (myNameSpace);
Marshal.ReleaseComObject (myOutlookApplication);
All 64comments
Page [ 1 2 3 4 5 6 7 Next >>  
  24-Feb-2005  06:47   
ya but it gives me access denied error....
what then...
  24-Feb-2005  07:01   
If you mean the Security Warning Dialog:
then you have to Click:
Allow Access
Or use Redemption to avoid Security Prompts,
or Implement this Programm as "Safe AddIn".

Greets, Helmut Obertanner
  17-Mar-2005  10:55   
Hi All,

   When we try to use this Code in a Web Service i am getting an Error: Access Denied at line

new Outlook.ApplicationClass();

Let me know hot to resolve this..

  18-Mar-2005  02:25   
Hello, if you programm a Webservice, i wouldn't use an outlook class, i would use Webdav or cdo, not outlook.
The webservice runs under a different account and without a GUI, don't use outlook object model (oom) from a service.

Greets, Helmut Obertanner
  13-Apr-2005  06:12   
Helmut Obertanner wrote on 24-Feb-2005 07:01
"Or use Redemption to avoid Security Prompts"

What's Redemption ? How to use it ??
  13-Apr-2005  07:18   
Hello Tomas,

Redemption is a great COM library wich you can use to avoid security Dialogs.
You can use it like any other COM Library.
Install it, add it as Reference to your project and use it.

You can get it here:

Another SuperDeveloperMustHaveTool is Outlook Spy.

Greets, Helmut Obertanner
[DATALOG Software AG]
  02-May-2005  10:56   
I'm looking for a simple piece of code which will allow me to grab an attachment from a folder and email it to someone. Any ideas?
  03-May-2005  07:45   
Richard, take a look at the Attachments.Add method, along with Helmut's sample above.
  17-Aug-2005  23:39   
I'm looking for a piece of code that will allow me to display my contact's emails in a windows form. Any ideas?
  26-Oct-2005  18:51   
After i create the do i save it on the disk...please provide an example.
Page [ 1 2 3 4 5 6 7 Next >>