Exchange connection 401 unauthorized

hi,
i encounter some trouble to connect to my exchange email account with aspose.email

here the situation :

i’m a interns on an it departement in a big firm and i have to write a little program to calculate the size of an exchange mailbox.

here the code:

Dim client As IEWSClient = EWSClient.GetEWSClient(“https://myserverexchange/ews/exchange.asmx”, “login”, “password”, “domain”)

this line throw an exception : 401 unauthorized

a little precision :
this line work fine at home but not when i am inside the company.

the company has an intranet and a proxy

the thing i wan’t to know is :

does my code have a problem in it ?

or does the exchange server simply refuse my request because of a bad configuration ?

thanks for your help and sorry for my bad english (it’s the first time i’m posting a message on a forum)

Hi horace,

Thank you for using Aspose.Email.

In case of proxy server, you can specify the proxy server as shown in the following sample code. Please verify the Exchange Server URL from your network administrator for accessing it via the API and feel free to contact us if we can be of any additional help to you in this regard.

Sample Code:

const string mailboxUri = “[https://exchange.domain.com/ews/Exchange.asmx ](https://exchange.domain.com/ews/Exchange.asmx)”; 

const string domain = @"";

const string username = @"username";

const string password = @"password";

NetworkCredential credential = new NetworkCredential(username, password, domain);

IEWSClient client = EWSClient.GetEWSClient(mailboxUri, credential);

WebProxy prox = new WebProxy("10.0.0.1", 8080);

prox.Credentials = new NetworkCredential("ProxyUsername", "ProxyPassword", "ProxyDomain");

prox.BypassProxyOnLocal = true;

client.Proxy = prox;

hi again,

thanks for your reply.

I have test your code (but in vb.net, i hope i have correctly translate it)
it throw the same exception 401 unauthorized.

But i finally find a way to make things work.
it’s to use the autodiscover function of the exchange web service api.
i don’t know why this function work and not the other one.
Maybe you folks could tell me ?

i’m a newby here so i don’t know how to close the subject.

thanks again for the support

Hi Horace,

Aspose.Email mail clients work fine if network credentials are initialized properly. We are glad to know that your issue is resolved. For your reference following is the alternate VB .NET version of the above mentioned sample code:

Const mailboxUri As String = "https://exchange.domain.com/ews/Exchange.asmx"
Const domain As String = ""
Const username As String = "username"
Const password As String = "password"
Dim credential As New NetworkCredential(username, password, domain)
Dim client As IEWSClient = EWSClient.GetEWSClient(mailboxUri, credential)
Dim prox As New WebProxy(“10.0.0.1”, 8080)
prox.Credentials = New NetworkCredential(“ProxyUsername”, “ProxyPassword”, “ProxyDomain”)
prox.BypassProxyOnLocal = True
client.Proxy = prox

Please feel free to write us back if you have any other query related to Aspose.Email.