标签:
/** serverName 接收邮件地址 user 用户信息 pwd 密码 path 邮件临时储存路径 max 每次接收邮件的最大数量 **/ public int receive(String serverName, String user, String pwd, String path, int max) throws Exception { //新建ExchangeVersion.Exchange2007_SP1版本的Exchange服务 ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); String[] userInfo = user.split("/"); //用户认证信息 ExchangeCredentials credentials = new WebCredentials(userInfo[1], pwd,userInfo[0]); service.setCredentials(credentials); //设置Exchange连接的服务器地址 service.setUrl(new URI(serverName)); //绑定邮箱 Folder inbox = Folder.bind(service, WellKnownFolderName.Inbox); //获取邮箱文件数量 int count = inbox.getTotalCount(); if(max > 0) count = count > max ? max : count; //循环获取邮箱邮件 ItemView view = new ItemView(count); FindItemsResults<Item> findResults = service.findItems(inbox.getId(), view); for (Item item : findResults.getItems()) { EmailMessage message = EmailMessage.bind(service, item.getId()); List<Attachment> attachs = message.getAttachments().getItems(); try{ if(message.getHasAttachments()){ for(Attachment f : attachs){ if(f instanceof FileAttachment){ //接收邮件到临时目录 File tempZip = new File(path,f.getName()); ((FileAttachment)f).load(tempZip.getPath()); } } //删除邮件 message.delete(DeleteMode.HardDelete); } }catch(Exception err){ log.equals(err); } } return count; }
标签:
原文地址:http://my.oschina.net/u/2291124/blog/525136