1

I was able to succeed via a package I found called EAGetMail. Unfortunately, I realized soon after that they have a token system and this is not a free approach.

There are a couple other choices available, like using Outlook Mail REST API, and MimeKit, but I'm lost on how to achieve my end result because no "start to finish" code is available on either of these references that demonstrates how to parse an Inbox for an account.

I've started to write this with the help of Mimekit, but am not sure if this is the proper way at all.

I must imagine it looks something like:

using (var client = new SmtpClient ())
{
    client.Connect("outlook.office365.com", 587);
    client.Authenticate("myemail@office365account.com", "mypassword");

    var message = MimeMessage.Load(stream);
}

I don't know how to setup the stream mentioned above, and I don't know if it's possible to do this with Mimekit and Office 365.

I'm open to seeing a solution for this in any other approach that's not through EAGetMail. If anyone has a lightweight solution ranging from actual establishing a connection, to pulling messages from the inbox, would be great to see!

LatentDenis
  • 2,839
  • 12
  • 48
  • 99
  • 1
    Not tried with Office365... But have you tried [Exchange Web Services](https://msdn.microsoft.com/en-us/library/office/dd877045(v=exchg.140).aspx)? – IronAces May 23 '17 at 12:58
  • 1
    EWS will work with an Office 365 Email account. https://stackoverflow.com/questions/32355440/connection-to-office-365-by-ews-api – Bearcat9425 May 23 '17 at 13:14
  • @Bearcat9425, I will try it this way, but would love to see your solution if you have one handy. – LatentDenis May 23 '17 at 13:23
  • 1
    There are a great many posts on here on how to read from EWS, also here is the MSDN on how to use the API, https://msdn.microsoft.com/en-us/library/office/dn535506(v=exchg.150).aspx#Anchor_2 – Bearcat9425 May 23 '17 at 13:35
  • @Bearcat9425 Got it working! thanks for your awesome suggestions! – LatentDenis May 23 '17 at 19:03
  • @DanielShillcock You were right! I just posted my code as an answer. Using `EWS` – LatentDenis May 23 '17 at 19:04

1 Answers1

0

I've got it using EWS (Exchange Web Services). Here's my code:

private static bool RedirectionUrlValidationCallback(string redirectionUrl)
{
    // The default for the validation callback is to reject the URL.
    bool result = false;

    Uri redirectionUri = new Uri(redirectionUrl);

    // Validate the contents of the redirection URL. In this simple validation
    // callback, the redirection URL is considered valid if it is using HTTPS
    // to encrypt the authentication credentials. 
    if (redirectionUri.Scheme == "https")
    {
        result = true;
    }
    return result;
}
static void Main(string[] args)
{
    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);

    service.Credentials = new WebCredentials("email@myemail.com", "myPassword");
    service.AutodiscoverUrl("email@myemail.com", RedirectionUrlValidationCallback);

            //creates an object that will represent the desired mailbox
    Mailbox mb = new Mailbox(@"email@myemail.com");

    //creates a folder object that will point to inbox folder
    FolderId fid = new FolderId(WellKnownFolderName.Inbox, mb);

    //this will bind the mailbox you're looking for using your service instance
    Folder inbox = Folder.Bind(service, fid);

    //load items from mailbox inbox folder
    if (inbox != null)
    {
        FindItemsResults<Item> items = inbox.FindItems(new ItemView(100));

        foreach (var item in items)
        {
            item.Load();
            Console.WriteLine("Subject: " + item.Subject);
        }
    }
}   
LatentDenis
  • 2,839
  • 12
  • 48
  • 99