4

I am writing a Windows Console App to read emails from a specially setup email account on Office365. The account is all setup and we are able to receive emails from Outlook. The Console App is is will run on a schedule from a remote server and extract specific attachments from specific emails, then move those emails to another folder. I have chosen to use the MailKit library from MimeKit and have started to code a small test app which I am listing below:

When ran the debugger on this code, I hit an error at client.Authenticate with the exception raised as "AuthenticationException". The userName and passWord I am using in my code is correct and the same I use in Outlook. Am I doing the basics right here ? Can I provide passWord in plain text or is there a specific format I need to use ? Please let me know if I have not provided all the info hear and I will get them and post here.

using MailKit.Net.Imap;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace CAppHarvestEmail
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var userName = "bi@mydomain.co";
                var passWord = "mybipassword";
                using (var client = new ImapClient())
                {
                    client.Connect("outlook.office365.com", true);
                    client.AuthenticationMechanisms.Remove("XOAUTH2");
                    client.Authenticate(userName, passWord);
                    var inbox = client.Inbox;
                    inbox.Open(MailKit.FolderAccess.ReadOnly);
                    Console.WriteLine("Total messages: {0}", inbox.Count);
                    Console.WriteLine("Recent messages: {0}", inbox.Recent);
                    client.Disconnect(true);
                }
            }
            catch (Exception e)
            {
                throw;
            }
        }
    }
}
rarpal
  • 175
  • 1
  • 13

3 Answers3

2

You need to generate app password for your application https://support.office.com/en-us/article/create-an-app-password-for-office-365-3e7c860f-bda4-4441-a618-b53953ee1183 and change line

client.Connect("outlook.office365.com", 993, SecureSocketOptions.SslOnConnect);
Matija Sestak
  • 393
  • 1
  • 10
  • Thanks msestak, it appears that multi-factor authentication is not in place for this email account, so I dont see the option to create the app password. Do you think I need to encrypt my password in some way before supplying it ? – rarpal Mar 10 '20 at 15:46
  • you do need to supply the port 993 and you can use SecureSocketOptions.Auto – terrencep Mar 10 '20 at 15:54
2

I resorted to using the Microsoft Exchange Web Service instead of IMAP.

Example:

using System;
using Microsoft.Exchange.WebServices.Data;



ExchangeService _service;

Console.WriteLine("Registering Exchange connection");

_service = new ExchangeService
{
    Credentials = new WebCredentials(username, password)
};

// This is the office365 webservice URL
_service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");

PropertySet propSet = new PropertySet(BasePropertySet.FirstClassProperties);
propSet.Add(ItemSchema.MimeContent);
propSet.Add(ItemSchema.TextBody);

foreach (EmailMessage email in _service.FindItems(WellKnownFolderName.Inbox, new ItemView(10)))
{
    var message = EmailMessage.Bind(_service, email.Id, propSet);

    Console.WriteLine("Email body: " + message.TextBody);
    Console.WriteLine();
}

Reference

Note: this seems to be deprecated. Also, I am not sure if this uses Basic auth or not. I guess not, since that was supposed to stop working after October 2020 and I have just used this code for a quick test in July 2021.

Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
rarpal
  • 175
  • 1
  • 13
-1

supply the port 993 and you can use SecureSocketOptions.Auto

terrencep
  • 675
  • 5
  • 16