Can not connect to Exchange via IMAP

Dear Aspose,

we have a problem in connectintion of email component to Exchange server via IMAP.
One of our customers have following enviroment configured following way:
1. Configure MS Exchange to require password in encrypted format - STARTTLS
2. Disable port 993 by the firewall on the Exchange server (custommer uses this port for other applications).
IMAP configuration export is attached to this post.

We need to connect to this server in encrypted format.
We have following code:
var client = !options.IgnoreCertificateChainErrors ?
new Aspose.Email.Imap.ImapClient(options.Host, options.Port, options.UserName, options.Password, securityOptions) :
new Aspose.Email.Imap.ImapClient(options.Host, options.Port, options.UserName, options.Password, ValidateCertificateIgnoringChainErrors, securityOptions);
client.SelectFolder("Inbox");

The port is set to 143.
ValidateCertificateIgnoringChainErrors is simple method that just checks SslPolicyErrors.

If we try to set securityOptions to SecurityOptions.Auto the client tries to send credentials in nonsecure way and throws exception:
ImapException: 002 BAD Command received in Invalid state.
at e?..Initialize(String , Int32 )
at e?.˜?.ƒ()
at ?.ƒ.??()
at Aspose.Email.Imap.ImapClient.get_CurrentFolder()
at Aspose.Email.Imap.ImapClient.SelectFolder(String folderName, Boolean readOnly)
The communication catched is following:
*OK The Microsoft Exchange IMAP4 service is ready.
001 CAPABILITY
*CAPABILITY IMAP4 IMAP4rev1 LOGINDISABLED STARTTLS CHILDREN IDLE NAMESPACE LITERAL+
001 OK CAPABILITY completed.
002 LOGIN username password
002 BAD Command received in Invalid state.

If we try to set securityOptions to SecurityOptions.SSLImplicit the communication freezes for a while and we ended with this:
System.IO.IOException: The handshake failed due to an unexpected packet format.
at e?..Initialize(String , Int32 )
at e?.˜?.ƒ()
at ?.ƒ.??()
at Aspose.Email.Imap.ImapClient.get_CurrentFolder()
at Aspose.Email.Imap.ImapClient.SelectFolder(String folderName, Boolean readOnly)

The communication then looks like this:
*OK The Microsoft Exchange IMAP4 service is ready.
001 CAPABILITY
002 STARTTLS
* CAPABILITY IMAP4 IMAP4rev1 LOGINDISABLED STARTTLS CHILDREN IDLE NAMESPACE LITERAL+
001 OK CAPABILITY completed.
002 OK Begin TLS negotiation now.
then it seems that negotiation begins, but communication freezes after while and is reseted in 40 seconds.

It seems that there is a problem in swichting communication to SSL.
If we try to connect with Thunderbird client with security set to STARTTLS, it is successfull and this client work properly.

Could you fix this?

Best regards,
Ondrej


Hi Ondrej,


Thank you for contacting Aspose support team,

I have reviewed your requirement and would like to share that it can be further analyzed only if some test account is available on the said exchange. It will help us to test the scenario on Thunderbird and Aspose.Email library here and log the activity for our consideration.

Could you please arrange a temporary test account and provide us the credentials for our testing? Also please make sure that test account remains available (if provided to us) until the analysis is completed and some solution is suggested

Your cooperation and understanding is highly appreciated in this regard.

Hi Kashif,


I have analyzed our problem again. I made a mistake.
I thought that implicit security options do what we need, but I have found that this is usually called explicit security.

Our problem:
We need to start communication on nonsecure port (143) and negotiate secure connection - it means start SSL/TLS.
This is done via STARTTLS command in IMAP.
This is usually called Explicit security mode.
I think that SecurityOptions.SSLExplicit should do exactly this. Is this right?

When I set port to 143 and security option to SecurityOptions.SSLExplicit, the simle code from original post ends with null reference exception:


System.NullReferenceException: Object reference not set to an instance of an object.

at e?.�.Initialize(String , Int32 )

at e?.�?.��()

at ?.�.??()

at Aspose.Email.Imap.ImapClient.get_CurrentFolder()

at Aspose.Email.Imap.ImapClient.SelectFolder(String folderName, Boolean readOnly)

at Aspose.Email.Imap.ImapClient.SelectFolder(String folderName)

There is visible in Wireshark capture that communication ends almost immediately when server begins negotiate TLS.

This is possible simulate on usual Exchange server withou any special setting.

Can you fix this problem?
Best regards,
Ondrej

Hi Ondrej,

I have tested this at my end by setting the security options to None which represents a non-secure connection and connecting to Exchange server. The following code was executed without any error and retreived the number of messages from the inbox.

Sample Code:

using (ImapClient client = new ImapClient(

“[exchange.aspose.com ](http://exchange.aspose.com/)”,

143,

"usernameasposeemail.test4@aspose.com",

“password”,

SecurityOptions.None))

{

client.SelectFolder(“Inbox”);

ImapMessageInfoCollection messageInfoCol = client.ListMessages();

}

The definition of Security Options inside the API is as follow:
None = 0 ---- Connection is not secured
SSLExplicit = 1 ------ Uses the STARTTLS command to start SSL connection
SSLImplicit = 2 ------ Establishes a SSL connection first
Auto = 256 ------- Auto Selection mode

Please try the settings in the code sample above and let us know your feedback.

Hi Kashif,


it works good with nonsecure communication but we need a secure one.
We need to communicate with Exchange via port 143 and use explicit secure communication.
This does not work as I wrote.
There is an exception in the code when we try this (see my previous post).
You have confirmed that SSLExplicit is exactly what we need, so can you fix it?
Because it does not work.

Ondrej

Hi Ondrej,

Could you please make sure that your server is configured properly for this purpose? You can easily verify this by using the same settings as you are using in our API. For using STARTTLS, this is a requirement to be configured for your desired port. Our components support all these variants and to check if your server is configured properly, please try to configure Microsoft Outlook with the same settings. If this works good, I would request you to please create a test account on your server and share its credentials with us so that we can investigate the issue at our end and assist you further.

Hi Kashif,

the configuration of the exchange server is ok.
I have tried to connect with thunderbird client with STARTTLS security settings and it connects properly. But aspose connection failed - there is and exception I have mentioned - this I consider as a bug.
I have made wireshark captures from both connections so you can analyze it (filter connection on server address - 10.0.0.90).
About the test account - I cannot provide it (and the request sounds little bit odd to me), but it is standard exchange server so I think you should be able to replicate it.

Hi Ondrej,

The issue has been logged as EMAILNET-34699 in our issue tracking system for further investigation by our development team. We’ll notify you as soon as there is some information available in this regard and appreciate your patience until then.

Hi Kashif,


thank you for creating an issue.
Can you give us an estimate when this could be fixed?
We need it to manage exceptations of our customers and partners.

Thank you,
Ondrej

Hi Ondrej,


I have checked the status of this issue and found that it will be available through our future release Aspose.Email for .NET 5.2 which is expected in the month of April 2015. I will inform you about any other update in this regard.

The issues you have found earlier (filed as EMAILNET-34699) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.

Dear Aspose,

I am connecting to exchange server through EWS and using impersonation but i am getting only 20 user ,it has around 300 user could you please explain.

@Harish2020,

I have observed the issue shared by you and request you to please try using Aspose.Email for .NET 20.1 on your end. In case there is still an issue please share the working sample project along with credentials that we may test on our end to help you out.

Here is the code 

for (EmailAddress emailAddress : contactmailAdd) {

			clientforexchange_input.impersonateUser(3, emailAddress.getAddress());
			try {
				ExchangeMailboxInfo mialinfo = clientforexchange_input.getMailboxInfo();
				clientforexchange_input.getMailboxInfo(mialinfo.getMailboxUri());
				System.out.println(emailAddress.getAddress());
				userList.add(emailAddress.getAddress());
			} catch (Exception e) {
				System.out.println("ooooooooooooooooooooooooooooooooooooo");
			}

i am getting only 20 contacts in my list while there are 300

@Harish2020,

I have observed the sample code and it seems to be fine. However, in order to proceed further with investigation on our end, we requested for account credentials that we may use to test and verify the issue on our end.