We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Connecting to Exchange using DefaultCredentials

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

I have successfully connected to an Exchange server via the ExchangeWebClient by specifying the username/password and sent an email. However I see that there is an overloaded method to use iCredential to make the connection to the exchange server.

Can this be used to connect to the exchange server by using the system.net.CredentialCache.DefaultCredentials i.e. By using the user’s credentials who’s currently logged in. An example in VB.Net would be greatly appreciated.

Thanks

Hi,

Thank you for inquiry.

You may use “System.Net.CredentialCache.DefaultNetworkCredentials” to get the credentials of the logged in user to connect to Exchange mailbox. The connectivity code is given below:

Dim credential As NetworkCredential = System.Net.CredentialCache.DefaultNetworkCredentials
Dim client As ExchangeWebServiceClient = New ExchangeWebServiceClient(mailboxURI, credential)

Hi Saqib

Dim credential As NetworkCredential = System.Net.CredentialCache.DefaultNetworkCredentials
Dim mailboxuri As String = "https://servername/ews/exchange.asmx"
Dim client As ExchangeWebServiceClient = New ExchangeWebServiceClient(mailboxuri, credential)
client.Send(message)

I get “The request failed with HTTP status 401: Unauthorised”, Using the below sends the message

Dim client As ExchangeWebServiceClient = New ExchangeWebServiceClient("https://servername/ews/exchange.asmx", "username", "password", "domain") client.Send(message)

Can you advise me what I’m doing wrong

Regards

Hi,

Could you please check by listing some messages from Inbox or other folder and see if you get the same error message?

Hi,

I tried to list messages as well as send emails with Exchange Server 2010 and latest version of Aspose.Network for .NET 6.6 with 2 different domain users. I got the messages of the logged-in user and also the mail was sent. Below is my code:

Dim credential As NetworkCredential = System.Net.CredentialCache.DefaultNetworkCredentials
Dim client As ExchangeWebServiceClient = New ExchangeWebServiceClient(mailboxURI, credential)
Dim message As MailMessage = New MailMessage()
message.From = New MailAddress(“user1”, True)
message.To.Add(New MailAddress(“user2”, True))
message.Subject = "test"
message.Body = "test"
client.Send(message)

Hi

Sending still fails for me with "401 UnAuthorised" when using "credentials" with your example code.

Listing messages from my exchange Inbox (also a version using ExchangeWebService) works

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Dim mailboxuri As String = "https://serveraddress/exchange/username"

Dim client As ExchangeClient = New ExchangeClient(mailboxuri, "username", "password", "domain")

Dim msgCollection As ExchangeMessageInfoCollection = client.ListMessages(client.MailboxInfo.InboxUri)

For Each msgInfo As ExchangeMessageInfo In msgCollection

MessageBox.Show("Subject: " & msgInfo.Subject & vbCrLf & _

"From: " & msgInfo.From.ToString() & vbCrLf & _

"To: " & msgInfo.To.ToString() & vbCrLf & _

"Sent Date: " & msgInfo.Date.ToString() & vbCrLf & _

"Read? " & msgInfo.IsRead.ToString() & vbCrLf & _

"Message ID: " & msgInfo.MessageId & vbCrLf & _

"Unique URI: " & msgInfo.UniqueUri & vbCrLf & _

"==================================")

Next msgInfo

However when trying to use “credentials” it fails with 401 UnAuthorised, also applies to a version using ExchangeWebService

Dim credential As NetworkCredential = System.Net.CredentialCache.DefaultNetworkCredentials

Dim mailboxuri As String = "https://serveraddress/exchange/username"

Dim client As ExchangeClient = New ExchangeClient(mailboxuri, credential)

Dim msgCollection As ExchangeMessageInfoCollection = client.ListMessages(client.MailboxInfo.InboxUri)

For Each msgInfo As ExchangeMessageInfo In msgCollection

MessageBox.Show("Subject: " & msgInfo.Subject & vbCrLf & _

"From: " & msgInfo.From.ToString() & vbCrLf & _

"To: " & msgInfo.To.ToString() & vbCrLf & _

"Sent Date: " & msgInfo.Date.ToString() & vbCrLf & _

"Read? " & msgInfo.IsRead.ToString() & vbCrLf & _

"Message ID: " & msgInfo.MessageId & vbCrLf & _

"Unique URI: " & msgInfo.UniqueUri & vbCrLf & _

"==================================")

Next msgInfo

We are using Exchange 2007 and I have tried both versions 6.4 (from Aspose.Total) & now 6.6 of Aspose.Network for .NET. I have a couple of days off next week, so maybe delayed in responding.

Regards

Ian

Hi

Any progress?

Regards Ian

Hello Ian,

I am sorry, but I could not reproduce this issue at my end. I tried with 2 different Exchange users with the same program. It lists messages for each respective user, as I used DefaultNetworkCredentials.

Could you please try to run the program on the Exchange Server directly, to check if there is some firewall or network issue?

Hi Saqib

Same error when trying it on the server. Can I confirm how you are connecting to the exchange server is it via HTTP or HTTPS, I read on MSDN that DefaultNetworkCredentials can't be used to connect HTTPS and that's what I'm connecting to. Could this be the cause of my issue?

If so is connecting via username/password the only solution on HTTPS connections?

Regards

Ian

Hello Ian,

I used https with ExchangeWebServiceClient class. The URL is in the form of “[https://exchange-server-host/ews/Exchange.asmx”](https://exchange-server-host/ews/Exchange.asmx%E2%80%9D) in this case.

Dim credential As NetworkCredential = System.Net.CredentialCache.DefaultNetworkCredentials
Dim client As ExchangeWebServiceClient = New ExchangeWebServiceClient(“[https://exchange-server-host/ews/Exchange.asmx”](https://exchange-server-host/ews/Exchange.asmx%E2%80%9D), credential)