开发者

c# ews login error

开发者 https://www.devze.com 2023-03-06 13:28 出处:网络
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?

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(it开发者_运维知识库em.Subject);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception thrown: " + ex.Message);
                Console.ReadLine();
            }


        }
    }
}


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

service.AutodiscoverUrl("user@domain.com");

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

0

精彩评论

暂无评论...
验证码 换一张
取 消