Reading Mail from many different email platforms on c# windows forms application

Viewed 383

I'm trying to read mail from different mail platforms on c#. Many people say:

Microsoft.Office.Interop.Outlook.NameSpace nameSpace = 
application.GetNamespace("MAPI");
nameSpace.Logon("", "", Missing.Value, Missing.Value);

inboxFolder = 
NameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFo
lders.olFolderInbox);
Console.WriteLine("Folders: {0}", inboxFolder.Folders.Count);

Although this hasn't worked for me. It tells me Microsoft.Interop.Outlook doesn't exist. I want to find out how to do this

Thank you

2 Answers

It seems that you haven't added reference to Microsoft.Office.Interop namespace. You need to download Microsoft.Office.Interop.Outlook.dll before accessing Outlook. Here is a sample code to access the inbox folder:

using System;
using System.Collections.Generic;
using Outlook = Microsoft.Office.Interop.Outlook;
   public static class OutlookMailF
    {
        public static Outlook.ApplicationClass application = new Outlook.ApplicationClass();
        static Outlook.NameSpace nameSpace = application.GetNamespace("MAPI");
        static Outlook.MAPIFolder inbox = nameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
        static Outlook.MAPIFolder sent = nameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);

    public static List<Outlook.MailItem> GetInbox()
    {
        List<Outlook.MailItem> allMails = new List<Outlook.MailItem>();
        //inbox u tüm maillere ekle.
        foreach (object item in inbox.Items)
        {
            if (item is Outlook.MailItem)
            {
                allMails.Add(item as Outlook.MailItem);
            }
        }
    }
}

MAPI/Office.Interop.Outlook really would only useful if you were solely working with Exchange/Office accounts.If this is the case you are going to need to add this namespace as a reference. If you do not have this on your computer, you can download and install the Office.Interop package.

For other email providers, for the most part; retrieving email is done using either the POP3 or IMAP protocals. There are several packages and/or projects available that you can add into your project => search for C# IMAP or C# POP3

To send email, you would use the SMTP protocal, which is available within the Net Framework.

Related