代码如下:
import microsoft.exchange.webservices.data.ConnectingIdType;
import microsoft.exchange.webservices.data.EmailMessage;
import microsoft.exchange.webservices.data.ExchangeCredentials;
import microsoft.exchange.webservices.data.ExchangeService;
import microsoft.exchange.webservices.data.ExchangeVersion;
import microsoft.exchange.webservices.data.FindItemsResults;
import microsoft.exchange.webservices.data.Folder;
import microsoft.exchange.webservices.data.ImpersonatedUserId;
import microsoft.exchange.webservices.data.Item;
import microsoft.exchange.webservices.data.ItemSchema;
import microsoft.exchange.webservices.data.ItemView;
import microsoft.exchange.webservices.data.SortDirection;
import microsoft.exchange.webservices.data.WebCredentials;
import microsoft.exchange.webservices.data.WellKnownFolderName;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class ExchangeMailUtil {
public static void main(String[] args) {
String aduser = "aaa";
String adpass = "aaa";
String addomain = "xxxx.com.cn";
String suffix = "@xxxx.com.cn";
String uri = "https://mail.xxxx.com.cn/ews/exchange.asmx";
// 要查询的邮箱名称,不带后缀
String sid = "xiaoming";
try {
String count = getCount(aduser, adpass, addomain, uri, suffix, sid);
// System.out.println("count=" + count);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getCount(String aduser, String adpass, String addomain, String uri, String suffix, String sid)
throws Exception {
String count = "0";
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
ExchangeCredentials credentials = new WebCredentials(aduser, adpass, addomain);
// ExchangeCredentials credentials = new WebCredentials("bbbb",
// "Aa123456","fr");
service.setCredentials(credentials);
ImpersonatedUserId other = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, sid + suffix);
service.setImpersonatedUserId(other);
service.setUrl(new URI(uri));
service.setTraceEnabled(true);
// 服务地址ַ
try {
// 获取收件箱
Folder inbox = Folder.bind(service, WellKnownFolderName.Inbox);
//System.out.println("未读邮件数:" + inbox.getUnreadCount());
count = inbox.getUnreadCount() + " ";
} catch (Exception e) {
e.printStackTrace();
}
return count;
}
public static JSONObject getMailList(String aduser, String adpass, String addomain,
String uri, String suffix, String sid,int rowsize) throws Exception {
JSONObject result = new JSONObject();
//ExchangeService版本
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
//用户名、密码、域
ExchangeCredentials credentials = new WebCredentials(aduser, adpass, addomain);
service.setCredentials(credentials);
ImpersonatedUserId other = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, sid + suffix);
service.setImpersonatedUserId(other);
service.setUrl(new URI(uri));
service.setTraceEnabled(true);
// 服务地址ַ
try {
// 获取收件箱
Folder inbox = Folder.bind(service, WellKnownFolderName.Inbox);
//创建过滤器条件,查询几封邮件
ItemView view = new ItemView(rowsize);
//按照时间顺序收取
view.getOrderBy().add(ItemSchema.DateTimeReceived, SortDirection.Descending);
//查询
FindItemsResults<Item> findResults = service.findItems(inbox.getId(), view);
JSONArray arr = new JSONArray();
for (Item item : findResults.getItems()) {
EmailMessage message = EmailMessage.bind(service, item.getId());
message.load();
JSONObject json = new JSONObject();
json.put("title", message.getSubject()); // 邮件标题
json.put("sender", message.getSender().getName()); // 发件人
json.put("date", DateUtil.convertDateToString(message.getDateTimeReceived(), DateUtil.PATTERN_DATETIME)); // 接收时间
json.put("url", message.getSender().getAddress()); // 链接
//System.out.println(message.getSender());
//System.out.println("Sub -->" + item.getSubject());
arr.add(json);
}
result.put("data", arr);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}