How to create Outlook email item using C# and vb.net

The following code example demonstrates how to create a new Outlook e-mail item with the specified information such as To, subject or body.
In this example we will create a new Outlook email item and display. The following code sample requires a reference of ‘Microsoft.Office.Interop.Outlook’ library in your project.

create Outlook email item using C-sharp

You can also Specify the level of importance for a created mail item by setting the mailItem.Importance property. You can set the following enum values to the mailItem.

olImportanceLow    Item is marked as low importance.
olImportanceNormal   Item is marked as medium importance.
olImportanceHigh   Item is marked as high importance.

[VB.net]

  1. Private Sub CreateMailItem()
  2.         Dim oApp As Outlook.Application = New Outlook.Application
  3.         Dim mailItem As Outlook.MailItem = oApp.CreateItem(Outlook.OlItemType.olMailItem)
  4.         mailItem.Subject = "This is the test message"
  5.         mailItem.To = "contactus@authorcode.com"
  6.         mailItem.CC = "supporttools@authorcode.com"
  7.         mailItem.Body = "This is the test message"
  8.         mailItem.Importance = Outlook.OlImportance.olImportanceNormal
  9.         mailItem.Display(False)
  10.         mailItem = Nothing
  11.         oApp = Nothing
  12.     End Sub

 
[C#]

  1. private void CreateMailItem()
  2. {
  3.  Outlook.Application oApp = new Outlook.Application();
  4.  Outlook.MailItem mailItem =(Outlook.MailItem) oApp.CreateItem(Outlook.OlItemType.olMailItem);
  5.  mailItem.Subject = "This is the test message";
  6.  mailItem.To = "contactus@authorcode.com";
  7.  mailItem.CC = "supporttools@authorcode.com";
  8.  mailItem.Body = "This is the test message";
  9.  mailItem.Importance = Outlook.OlImportance.olImportanceNormal;
  10.  mailItem.Display(false);
  11.  mailItem = null;
  12.  oApp = null;
  13. }

  • sumit kumar

    This code is Good To work on the local system. But If the code is deployed on the server. The client system not access the outlook message.
    If u have a solution for that Plsease suggest me.

  • http://www.authorcode.com Hirendra Sisodiya

    Hi Sumit..

    This code will run on any machines that contains Outlook locally, so You need to install outlook on  each machine from where you want to open outlook message window.

    thanks