1

Summary: I need to login to my mailbox using EWS and but i keep getting 440/401 error.

question: Anything obvious in my code as to why I keep getting either 401 or 440 errors?

Code:

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using Microsoft.Exchange.WebServices;
using Microsoft.Exchange.WebServices.Data;
using Microsoft.Exchange.WebServices.Autodiscover;


namespace MailboxListenerEWS
{
    class Program
    {
        //Authentication to exchange 2007 with webdav and filebasedauth (FBA) in C#
        //can hardcode a username to connect a mailbox with
        internal static string dUser = "username";//username to log into email account
        internal static string dDomain = "domain";//domain of username used
        internal static string dPassword = "Password";//password of username used
        internal static string MailBoxAliasName = "mailboxname";//mailbox to authenticate too
        internal static string ExchangeServerName = "exchangeName"; //not always needed
        internal static string ReadAttachments = "0"; //1 means read attachments, 0 means dont
        internal static string MailBoxEarliestDateToRead = "2011-01-05T00:00:00.000Z";//date of emails to read from



        static void Main(string[] args)
        {
            //Connect to server
            //ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
            //service.Credentials = new NetworkCredential("name", "pwd", "domain");
            service.Credentials = new NetworkCredential("dUser", "dPassword", "dDomain");

            //log in to mailbox
            try
            {
                //service.Url = new Uri(serviceurl);
                //Console.WriteLine(serviceurl);
                //service.AutodiscoverUrl(MailBoxAliasName);
                service.Url = new Uri("https://" + ExchangeServerName + "/EWS/" + MailBoxAliasName + "/inbox");
            }
            catch (AutodiscoverRemoteException ex)
            {
                Console.WriteLine("Exception thrown: " + ex.Error.Message);
                Console.ReadLine();
            }


            //List folders
            try
            {
                //Listing all the subfolders of the Inbox
                FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(int.MaxValue));
                foreach (Item item in findResults)
                {
                    Console.WriteLine(item.Subject);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception thrown: " + ex.Message);
                Console.ReadLine();
            }


        }
    }
}

1 Answer 1

2

Are you sure your server uses 2007 SP1? Also, if you're not sure of the correct URL, try using the AutoDiscover, where "[email protected]" is an actual email address on your domain.

service.AutodiscoverUrl("[email protected]");

Also, you're setting your URL equal to a URi . I don't think that's legal; is it even compiling?

Here is some sample code that I used as a starting point when I implemented something similar - codeproject.com

5
  • It is using service pack 2 but ExchangeVersion.Exchange2007_SP2 came up as an error. Ill try going back to the autodiscover method, the reason i didnt use it is because it just hung for a while. May 16, 2011 at 19:59
  • also auto discover gives me the error "The Autodiscover service couldn't be located." May 16, 2011 at 20:01
  • see my new comment/question on URL/URi.
    – WEFX
    May 16, 2011 at 20:18
  • its breaking on the find items section. What should my URL be then? May 16, 2011 at 20:30
  • 2
    Setting the Url property to a new Uri works just fine. Even the sample code on codeproject.com you referred to has this code. Other than that I have no problems with your answer :)
    – Halvard
    Aug 14, 2013 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.