C# .NET - outlook problem in windows application

Asked By preethi r
06-Jun-08 05:54 AM
c# code to retrive all the items present in the inbox of the outlook into datagrid view....

check here..  check here..

06-Jun-08 06:05 AM

hi,

tryt the below code...

Microsoft.Office.Interop.Outlook.Application app = null;
Microsoft.Office.Interop.Outlook._NameSpace ns = null;
Microsoft.Office.Interop.Outlook.PostItem item = null;
Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null;

try
{
  app = new Microsoft.Office.Interop.Outlook.Application();
  ns = app.GetNamespace("MAPI");
  ns.Logon(null,null,false, false);

  inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
  subFolder = inboxFolder.Folders["MySubFolderName"]; //folder.Folders[1]; also works
  Console.WriteLine("Folder Name: {0}, EntryId: {1}", subFolder.Name, subFolder.EntryID);
  Console.WriteLine("Num Items: {0}", subFolder.Items.Count.ToString());

  for(int i=1;i<=subFolder.Items.Count;i++)
  {
    item = (Microsoft.Office.Interop.Outlook.PostItem)subFolder.Items[i];
    Console.WriteLine("Item: {0}", i.ToString());
    Console.WriteLine("Subject: {0}", item.Subject);
    Console.WriteLine("Sent: {0} {1}" item.SentOn.ToLongDateString(), item.SentOn.ToLongTimeString());
    Console.WriteLine("Categories: {0}", item.Categories);
    Console.WriteLine("Body: {0}", item.Body);
    Console.WriteLine("HTMLBody: {0}", item.HTMLBody);
  }
}
catch (System.Runtime.InteropServices.COMException ex)
{
  Console.WriteLine(ex.ToString());
}
finally
{
  ns = null;
  app = null;
  inboxFolder = null;
}

See this  See this

06-Jun-08 06:05 AM

To use outlook in c# we have to instantiate the Outlook Application object. We are referring the outlook name space couple of times .so, rename outlook name space rename like this.

using OutLook = Microsoft.Office.Interop.Outlook;

First we have to create outlook application interface object.

OutLook._Application outlookObj = new OutLook.Application();

 

Just go thr this link which will give you all the details of outlook in .net.............

 

http://msdn.microsoft.com/en-us/library/aa479345.aspx

 

http://www.c-sharpcorner.com/UploadFile/rambab/OutlookIntegration10282006032802AM/OutlookIntegration.aspx

 

http://www.codeguru.com/csharp/csharp/cs_syntax/operators/article.php/c14293/

 

Best Luck!!!!!!!!!!!!!!
Sujit.

outlook  outlook

06-Jun-08 12:46 PM

Add outlook COM object reference to your windows app,

using Outlook = Microsoft.Office.Interop.Outlook;

Then you should be able to instantiate an Outlook object and make requests of it:

Outlook.Application app = new Outlook.ApplicationClass();
Outlook.NameSpace NS = app.GetNamespace("MAPI");
Outlook.MAPIFolder inboxFld = NS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
 
This will give you access to inboxFld, which will allow you to iterate through the contents of the inbox! You can also change this to iterate through notes, or through calendar entries, tasks, etc. as you want.

For example, to iterate through your mail you can do:

foreach(Outlook.MailItem t in inboxFld.Items)
{
 Now you can loop through the attachments as,

    foreach(Outlook.Attachment Atmt in t.Attachments){
       'save the attachment to some filename, just for ex., c:\test.doc
        Atmt.SaveAsFile "C:\test.doc"
    }

}

Hello  Hello
06-Jun-08 12:51 PM

Try this part of code, its in vb, convert to c#

Dim _objItem As Outlook.MailItem = Nothing
Dim _objInboxFolder As Outlook.MAPIFolder = Nothing
Dim _objSubfolder As Outlook.MAPIFolder
Dim _outlook As Outlook.MailItemClass
Dim _outlookObj As New Outlook.Application
Dim _outlookNamespace As Outlook.NameSpace
_outlookNamespace = _outlookObj.GetNamespace("MAPI")
_outlookNamespace.Logon(Nothing, Nothing, False, False)
_objInboxFolder = _outlookNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
_objSubfolder = _objInboxFolder.Folders(1);

console.Write("Folder Name: " & _objSubfolder.Name & " Entry ID" & _objSubfolder.EntryID)
console.Write("Num Items: " & _objSubfolder.Items.Count)
Dim _itemCount As Integer = _objSubfolder.Items.Count
While (_itemCount <> 0)
_objItem = CType(_objSubfolder.Items(_itemCount), Outlook.PostItem)
console.Write("Item " & _objItem.Subject.ToString)
_itemCount -= 1
End While

ty this preethi  ty this preethi
06-Jun-08 12:56 PM

Outlook.Application app = new Outlook.ApplicationClass();

Outlook.NameSpace NS = app.GetNamespace("MAPI");

Outlook.MAPIFolder inboxFld = NS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);

This will give you access to inboxFld, which will allow you to iterate through the contents of the inbox!  You can also change this to iterate through notes, or through calendar entries, tasks, etc. as you want.

For example, to iterate through your mail you can do:


Console.WriteLine(t.Subject);

}

OutLook Problem  OutLook Problem
06-Jun-08 03:17 PM
Hello Preethi
I hope you are posting this at now 3rd time And Yuo must have got the things about the OutLook.

Add COM component in your project
Now create the OutLook Interop Object.
OutLook stores  all the Inforamtion On MAPI Folders so it depends upon the Server where this folders are located as on local or on server.
 You have to check the MAPI Folder presents in the OutLook
and iterate through the types of the MAPI Folder that you want to get data from
sample :
 

using OutLook = Microsoft.Office.Interop.Outlook;

OutLook._Application outlookObj = new OutLook.Application();

OutLook.MAPIFolder fldContacts = (OutLook.MAPIFolder)outlookObj.Session.GetDefaultFolder(OutLook.OlDefaultFolders.olFolderContacts);


This all in detail can be found at :
 http://www.c-sharpcorner.com/UploadFile/rambab/OutlookIntegration10282006032802AM/OutlookIntegration.aspx

Happy Coding Takecare

reply of outlook into datagrid  reply of outlook into datagrid
09-Jun-08 02:45 AM
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Outlook = Microsoft.Office.Interop.Outlook;
 
 
namespace EmailScan
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }
 
    private void Form1_Load(object sender, EventArgs e)
    {
      Microsoft.Office.Interop.Outlook.Application app = null;
      Microsoft.Office.Interop.Outlook._NameSpace ns = null;
 
      app = new Microsoft.Office.Interop.Outlook.Application();
      ns = app.GetNamespace("MAPI");
      ns.Logon(null, null, false, false);
 
 
 
      // Obtain Inbox
      Outlook.Folder folder =
      ns.GetDefaultFolder(
      Outlook.OlDefaultFolders.olFolderInbox)
      as Outlook.Folder;
      // Create filter
      string filter = "";
      Outlook.Table table =
      folder.GetTable(filter,
      Outlook.OlTableContents.olUserItems);
      // Remove default columns
      table.Columns.RemoveAll();
      // Add using built-in name
      table.Columns.Add("EntryID");
      table.Columns.Add("Subject");
      table.Columns.Add("ReceivedTime");
      table.Columns.Add("SenderEmailAddress");
      table.Sort("ReceivedTime", Outlook.OlSortOrder.olDescending);
      // Add using namespace
      // Date received
      table.Columns.Add(
      "urn:schemas:httpmail:datereceived");
 
      this.dataGridView1.DataSource = table;
       
    }
  }
}
reply of outlook into datagrid  reply of outlook into datagrid
09-Jun-08 02:46 AM
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Outlook = Microsoft.Office.Interop.Outlook;
 
 
namespace EmailScan
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }
 
    private void Form1_Load(object sender, EventArgs e)
    {
      Microsoft.Office.Interop.Outlook.Application app = null;
      Microsoft.Office.Interop.Outlook._NameSpace ns = null;
 
      app = new Microsoft.Office.Interop.Outlook.Application();
      ns = app.GetNamespace("MAPI");
      ns.Logon(null, null, false, false);
 
 
 
      // Obtain Inbox
      Outlook.Folder folder =
      ns.GetDefaultFolder(
      Outlook.OlDefaultFolders.olFolderInbox)
      as Outlook.Folder;
      // Create filter
      string filter = "";
      Outlook.Table table =
      folder.GetTable(filter,
      Outlook.OlTableContents.olUserItems);
      // Remove default columns
      table.Columns.RemoveAll();
      // Add using built-in name
      table.Columns.Add("EntryID");
      table.Columns.Add("Subject");
      table.Columns.Add("ReceivedTime");
      table.Columns.Add("SenderEmailAddress");
      table.Sort("ReceivedTime", Outlook.OlSortOrder.olDescending);
      // Add using namespace
      // Date received
      table.Columns.Add(
      "urn:schemas:httpmail:datereceived");
 
      this.dataGridView1.DataSource = table;
       
    }
  }
}
Create New Account
help
How to get the messageclass of a selected item in Outlook Office Hello, i'm trying to recognize in my COM-Addin if a mail or appointment commandbar. Therefore i implemented the OnNewInspector event and the OnSelectionChange event. public partial class ThisAddIn { Outlook.Application applicationObject; private Outlook.Inspectors _inspectors; Outlook.Explorer explorer; Outlook.Selection selection; private void ThisAddIn_Startup(object sender, System.EventArgs e) { _inspectors = 3D Application.Inspectors; _inspectors.NewInspector + = 3D new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler (ThisApplication_NewWindowOpen); explorer = 3D Application.ActiveExplorer(); explorer.SelectionChange + = 3D new Outlook.ExplorerEvents_10_SelectionChangeEventHandler( Explorer_SelectionChange
unable to solve the problem(read .ost file using C#.net) C# .NET Microsoft.Office.Interop.Outlook. Application objOutlookApplication = new Microsoft.Office.Interop.Outlook. Application (); Microsoft.Office.Interop.Outlook. NameSpace objNamespace = objOutlookApplication.GetNamespace( "MAPI" ); Microsoft.Office.Interop.Outlook. MAPIFolder objMAPIFolder = null ; try { objMAPIFolder = ( MAPIFolder )objNamespace.OpenSharedFolder(@" \ AppData \ Local \ Microsoft \ Outlook \ abc" , null , null
to an email. My code is below . Any help would be greatly appreciated. void ConnectEvents() { ((Outlook. InspectorEvents_10_Event )Inspector).Activate + = new Microsoft.Office.Interop.Outlook. InspectorEvents_10_ActivateEventHandler (InspectorWrapper_Activate); ((Outlook. InspectorEvents_10_Event )Inspector).BeforeMaximize + = new Microsoft.Office.Interop.Outlook. InspectorEvents_10_BeforeMaximizeEventHandler (InspectorWrapper_BeforeMaximize); ((Outlook. InspectorEvents_10_Event )Inspector).BeforeMinimize + = new Microsoft.Office.Interop.Outlook. InspectorEvents_10_BeforeMinimizeEventHandler (InspectorWrapper_BeforeMinimize); ((Outlook. InspectorEvents_10_Event )Inspector).BeforeMove + = new Microsoft.Office.Interop.Outlook. InspectorEvents_10_BeforeMoveEventHandler (InspectorWrapper_BeforeMove
deploy 2003 vsto addin on outlook 2007 problem Outlook Hi, Have created and deployed successfully, an outlook vsto addin for 2003. But i am unable to install it on office 2007 for one of the machines as, it is unable to install O2003PIA as it also requires office sp2. Whenever i try to install offce sp2 it says product version not found. Is the files and folders for 2003 on the system. Is there something i am missing. Outlook Program Add-Ins Discussions Outlook 2003 (1) Outlook 2007 (1) Office (1) Word (1) VSTO (1) XP (1) NewInspectorEventHandler (1) SetTrackForReplyForward (1) A