Counting Emails in inbox from SSIS (C#) - The remote server returned an error: (401) Unauthorized

Viewed 83

I am trying to create a connection to the ExchangeService from a script component (C#) in SSIS to be able to count the total number of emails in an inbox.

My error comes when I try to use the connection - so far I've been testing the connection to simply try and send a test email to myself and its on msg.send() that I get the error:

The remote server returned an error: (401) Unauthorized

I have been using the WebServices (EWS) NuGet Package with the code below:

    ExchangeService exService = new ExchangeService(ExchangeVersion.Exchange2013_SP1);

    string username = "emailUser";
    string pwd = "MyPwd";
    string domain = "NetBIOS_DOMAIN";

    exService.Credentials = new WebCredentials(username, pwd, domain);

    exService.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");

    EmailMessage msg = new EmailMessage(exService)
    {
        Subject = "This is a test!!!",
        Body = "This is the message body"
    };

    msg.ToRecipients.Add("emailUser@domain.co.uk");
    msg.Send(); 

I've not got as far as looking into inboxes yet because as soon as I try to bind a folder I get the same error so figured this was an easier test.

I have tried both WebCredentials() and NetworkCredential() - both give the same error.

I have been primarily following the notes from here

A search on the error led me to try all manor of variances with the username/domain as outlined in an answer here

And I have tested my remote connectivity with this tool which gives the same error (Screenshot)

I am testing this with my own credentials at present but eventually it would be replaced with a different used or a service account. I think everything is pointing at me entering them wrong somehow but I'm 2 days in and totally stumped.

Thanks all

0 Answers
Related