2

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

Shadow336
  • 21
  • 1
  • Have you tried logging into OWA ? basically you should use the same username format you use in OWA if its office365 the only thing that will work is the UPN – Glen Scales Aug 30 '20 at 23:37
  • I attempted an OWA log in [here](https://webmail.hostedexchange.be/owa/auth/logon.aspx?replaceCurrent=1&url=https%3a%2f%2fwebmail.hostedexchange.be%2fowa%2f) with no luck, but it is office365, so I've amended the code to drop the domain and use my fully qualified email address as username, and password - but still no luck. my code now looks like this: string username = "fName.lName@domain.co.uk"; string pwd = "password"; // string domain = ""; exService.UseDefaultCredentials = false; exService.Credentials = new WebCredentials(username, pwd); – Shadow336 Sep 01 '20 at 09:46
  • I don't know if this helps, but when I try to allow the service to AutodiscoverUrl it hangs forever. Even using default credentials. – Shadow336 Sep 01 '20 at 11:31
  • So the OWA link you pointed to was for an Exchange 2010 hosted service, some of these older hosted solutions didn't offer EWS access. As a first step you should be able to logon to the Mailbox with OWA else how do you know if the creds are even valid ? (Eg it maybe asking you to change the password because this is the first logon etc) – Glen Scales Sep 01 '20 at 23:51
  • I have access to the inbox in office through outlook which confirms the creds. Signing in to Outlook.Live.com for example is fine. Everywhere I've read for office365 accounts should just be a full email address, and password.. it seems so simple. I have recently read however that this method isn't supported anymore since the release of the graphs API, and I'm wondering if the unauth error I'm getting is due to Office requiring an OAuth connection now? Is graphs my only way forward? I can't think of why else this wouldnt work.. – Shadow336 Sep 02 '20 at 07:57

0 Answers0