Skip to content


Reading Mails from secure exchange server in Java

Posted in Internet, J2EE, Java, Technology.

1. Export the certificate for the exchange server from Internet explorer Certificate export wizard e.g. after opening https://webmail.hostname.com, (View certificate >Details > Copy to File). Say the exported file name is certificate.cer
2. Create keystore from the certificate using java keytool (which will create keystore file in current directory). It will ask for a password, give anything (it does not matter)
keytool -import -alias webmail.hostname.com -keystore keystore -file certificate.cer
3. Now While running the Java application pass the VM parameter : -Djavax.net.ssl.trustStore=
Path_To_keystoreFile

Sample code to read mail is provided below (Its uses JEC API: http://www.javaexchangeconnector.com/)

import java.text.*;
import java.util.*;
import jec.*;
import jec.dto.*;
public class FetchSecureExchangeMail {
public static void main(String args[]) throws Exception {
ExchangeConnectorFactory factory = new ExchangeConnectorFactory();
ExchangeConnectorInterface connector = factory.createExchangeConnector(“webmail.hostname.com”, “username”,”password”, “Exchange”, true, “username@hostname.com”);
SimpleDateFormat dateFormat = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
Date sinceDate = dateFormat.parse(“2007-04-25 06:00:00″);
HashSet fromEmailAddressFilter = null;
HashSet toEmailAddressFilter = null;
int maxEmailsReturned = 100;
ArrayList emailsArrayList = connector.getEmails(
sinceDate,
fromEmailAddressFilter,
toEmailAddressFilter,
maxEmailsReturned);
System.out.println(“emailsArrayList.size() : ” + emailsArrayList.size());
if (emailsArrayList.size() > 0) {
ExchangeEmailDTO email1 = (ExchangeEmailDTO) emailsArrayList.get(0);
System.out.println(“email1 email1.getSubject(): ” +
email1.getSubject());
}
}
}

Note from Author :
I maintain my website, write articles and reply comments because of my interest/hobby to share information and knowledge.

If you liked the post, Please
Follow me on twitter: http://twitter.com/pankajbatracom
Join My facebook page: http://www.facebook.com/pankajbatra.blog
Join me on LinkedIn: http://www.linkedin.com/in/batrapankaj
Or subscribe to updates by Email by clicking here

To subscribe for updates, Enter your email address:

Related posts:

  1. Sample java code to read mails from your gmail account
  2. How to Enable remote debugging of a java application
  3. Executing Javascript in Java code
  4. Class Loading in Java
  5. Export File name in servlet/jsp


One Response

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. samir says

    Hello,
    what is ExchangeEmailDTO?

    Thanks!







Some HTML is OK

or, reply to this post via trackback.