Note: Forums will be making significant UX changes to address key usability improvements surrounding search, discoverability and navigation. To learn more about these changes please visit the announcement which can be found HERE.
Is it possible to access Outlook from the SMTP library? ( C# console application )

Locked Is it possible to access Outlook from the SMTP library? ( C# console application )

  • Saturday, August 11, 2012 12:27 AM
     
     

    I have a C# application that has to attach a file and email it to people.

    Currently I've been having it use the SMTP library and putting in the recipient addresses by hand (technically, by a configuration file). But I realize it would be a lot better to do the emails by emailing global distribution lists rather than manually inputting the emails by hand.

    Is there a way to send emails to global distribution lists via a configuration file using SMTP? Or will I have to use Microsofts' Outlook dll? If I have to use Outlook, could someone point me to some example code to get me started?

All Replies

  • Saturday, August 11, 2012 6:43 AM
     
     Proposed

    Accessing outlook is independent of using SMTP, you can use the following:

    The following demostrates how to retreive data from items within an Outlook folder (called "MySubFolderName" under the Inbox folder) using .NET:

    First add a reference to the Outlook COM object your project:

    1. In VS.NET right click on References and choose Add Reference.
    2. Select the COM tab
    3. Choose "Microsoft Outlook 11.0 Object Library" (this is for MS Office 2003 - I think 10.0 is for Office XP) and click Select.
    4. Click OK.

    Note that you can access any Outlook/Exchange object types, eg Appointments, Notes, Tasks, Emails etc - just use intellisense to select which one (eg Microsoft.Office.Interop.Outlook. ... - see definition of variable called 'item' below).

    Here's the 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;
    }

    • Proposed As Answer by kishhr Saturday, August 11, 2012 6:44 PM
    •  
  • Monday, August 13, 2012 8:45 PM
     
     

    I might want to have been more specific.


    I'm trying to send an email to a global distribution list in Microsoft Outlook using SMTP. Is that possible or do I have to use MS's Outlook COM

  • Friday, August 31, 2012 9:48 AM
     
     

    Hi Lance,

    From your description , I ‘d like to move this post to  the most related forum .

    There has more  experts in this aspect ,  you may have more luck getting answers .

    Thanks for your understanding .

    Regards ,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us