I am testing the Microsoft Exchange Web Service Java API (version 1.2) to read mails from a server. Here is my code:
String url = \"https://
This is an Example of EWS JAVA api 1.2,This is one of the ways you can try
package com.ea.connector.exchange;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import microsoft.exchange.webservices.data.BasePropertySet;
import microsoft.exchange.webservices.data.ExchangeCredentials;
import microsoft.exchange.webservices.data.ExchangeService;
import microsoft.exchange.webservices.data.FindItemsResults;
import microsoft.exchange.webservices.data.Item;
import microsoft.exchange.webservices.data.ItemSchema;
import microsoft.exchange.webservices.data.ItemView;
import microsoft.exchange.webservices.data.LogicalOperator;
import microsoft.exchange.webservices.data.OffsetBasePoint;
import microsoft.exchange.webservices.data.PropertySet;
import microsoft.exchange.webservices.data.SearchFilter;
import microsoft.exchange.webservices.data.SortDirection;
import microsoft.exchange.webservices.data.WebCredentials;
import microsoft.exchange.webservices.data.WebProxy;
import microsoft.exchange.webservices.data.WellKnownFolderName;
import org.apache.commons.httpclient.NTCredentials;
import com.ea.connector.exchange.bean.ExchangeConnectionParamBean;
import com.ea.connector.exchange.util.ExchMessageProperties;
import com.ea.connector.net.ExchGetItemRequest;
public class Test {
protected static ArrayList<String> propertiesToFetch = new ArrayList<String>();
private static ExchangeService mService;
// private Settings mSettings;
//
// private int imailCount;
private Date dStartDate;
private Date dEndDate;
private static ExchangeConnectionParamBean objParamBean;
public void setPropertiesToBeFetched(List<String> filter) {
propertiesToFetch.addAll(filter);
}
public Date getConnectorStartDate() {
return dStartDate;
}
public void setConnectorStartDate(Date dStartDate) {
this.dStartDate = dStartDate;
}
public ExchangeConnectionParamBean getParamBean() {
return objParamBean;
}
public void setParambean(ExchangeConnectionParamBean objParambean) {
Test.objParamBean = objParambean;
}
public Date getConnectorEndDate() {
return dEndDate;
}
public void setConnectorEndDate(Date dEndDate) {
this.dEndDate = dEndDate;
}
public static void main(String []args) throws URISyntaxException,Exception
{
setCredentialsAndGetExchRequest();
System.out.println("conncection established");
}
protected static ExchGetItemRequest setCredentialsAndGetExchRequest() throws URISyntaxException,Exception{
@SuppressWarnings("unused")
FindItemsResults<Item> resultMails;
mService = new ExchangeService();
mService.setUrl(new URI("https://outlook.office365.com/EWS/Exchange.asmx"));
WebProxy paramWebProxy=new WebProxy("ptbproxy.domain.com",8080);
mService.setWebProxy(paramWebProxy);
ExchangeCredentials cred = new WebCredentials("EA_Test_mailbox@domain.com","Zuxu0000");
NTCredentials ntCredentials = new NTCredentials("EA_Test_mailbox@domain.com","Zuxu0000", "",
"");
mService.setCredentials(cred);
// ProxyHost httpProxy=new ProxyHost("ptbproxy.domain.com",8080);
// UsernamePasswordCredentials cred1=new UsernamePasswordCredentials("EA_Test_mailbox@domain.com","Zuxu0000");
// ExchGetItemRequest req = new ExchGetItemRequest(String.valueOf(new URI("http://outlook.office365.com/EWS/Exhanges.asmx")),ntCredentials);
ExchGetItemRequest req = new ExchGetItemRequest("https://outlook.office365.com/EWS/Exhange.asmx",ntCredentials);
SearchFilter filter = getSearchFilter();
PropertySet propertySet = new PropertySet(BasePropertySet.IdOnly);
ItemView itemView = new ItemView(1000, 0, OffsetBasePoint.Beginning);
itemView.getOrderBy().add(ItemSchema.DateTimeReceived, SortDirection.Descending);
itemView.setPropertySet(propertySet);
//mService.setTimeout(500000);
req.setProperties(new ExchMessageProperties().getExchMessageProperties(propertiesToFetch));
/*Fetching of mail ids start here*/
resultMails = getMails(filter, itemView);
return req;
}
protected static SearchFilter getSearchFilter() throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
Date d1 = sdf.parse("2014-11-04 00:00:00");
Date d2 = sdf.parse("2014-11-05 00:00:00");
SearchFilter greaterThanEq = new SearchFilter.IsGreaterThanOrEqualTo(
ItemSchema.DateTimeReceived,sdf.format(d1));
SearchFilter lessThan = new SearchFilter.IsLessThan(
ItemSchema.DateTimeReceived, sdf.format(d2));
SearchFilter mailFilter = new SearchFilter.IsEqualTo(
ItemSchema.ItemClass, "IPM.Note");
return new SearchFilter.SearchFilterCollection(LogicalOperator.And,
greaterThanEq, lessThan, mailFilter);
}
private static FindItemsResults<Item> getMails(SearchFilter searchFilter,
ItemView itemView) throws Exception {
FindItemsResults<Item> items = null;
int count = 0;
int maxTries = 3;
while (true) {
try {
items = mService.findItems(WellKnownFolderName.Inbox,
searchFilter, itemView);
break;
} catch (Exception e) {
if (++count == maxTries)
throw e;
}
}
return items;
}
}
The problem is due to the authentication scheme used. By default, EWS uses NTLM, but my Exchange server is not configured to accept this kind of authentication. I have to use LDAP authentication.
I got the above mentioned error and spend many hours trying to fix it. My ultimate solution was to give up on EWS Version 1.2 completely and use the following javax.mail code like this:
package javaapplication4;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;
public class JavaApplication4 {
public static void main(String[] args) throws Exception {
new JavaApplication4().send_email();
}
private void send_email() throws Exception
{
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.yourserver.net");
props.put("mail.from", "yourusername@youremailaddress.com");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.ssl.enable", "false");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "587");
Authenticator authenticator = new Authenticator();
props.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
Session session = Session.getInstance(props, authenticator);
MimeMessage msg = new MimeMessage(session);
msg.setFrom();
msg.setRecipients(Message.RecipientType.TO, "yourusername@youremailaddress.com");
// also tried @gmail.com
msg.setSubject("JavaMail ssl test");
msg.setSentDate(new Date());
msg.setText("Hello, world!\n");
Transport transport;
transport = session.getTransport("smtp");
transport.connect();
msg.saveChanges();
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
}
private class Authenticator extends javax.mail.Authenticator {
private PasswordAuthentication authentication;
public Authenticator() {
String username = "yourusername@youremailaddress.com";
String password = "yourpassword";
authentication = new PasswordAuthentication(username, password);
}
protected PasswordAuthentication getPasswordAuthentication() {
return authentication;
}
}
}
You will need to get the javamail-1.4.7
and load the mail.jar
from (http://www.oracle.com/technetwork/java/index-138643.html) into the project.
One thing we did which shed light on the situation is download Thunderbird mail client which can auto-discover information about the exchange server to make sure all of our settings were right.