serverName 接收邮件地址
user 用户信息
pwd 密码
path 邮件临时储存路径
max 每次接收邮件的最大数量
/
public int receive(String serverName, String user, String pwd, String path, int max) throws Exception {
//新建ExchangeVersionExchange2007_SP1版本的Exchange服务
ExchangeService service = new ExchangeService(ExchangeVersionExchange2007_SP1);
String[] userInfo = usersplit("/");
//用户认证信息
ExchangeCredentials credentials = new WebCredentials(userInfo[1], pwd,userInfo[0]);
servicesetCredentials(credentials);
//设置Exchange连接的服务器地址
servicesetUrl(new URI(serverName));
//绑定邮箱
Folder inbox = Folderbind(service, WellKnownFolderNameInbox);
//获取邮箱文件数量
int count = inboxgetTotalCount();
if(max > 0) count = count > max max : count;
//循环获取邮箱邮件
ItemView view = new ItemView(count);
FindItemsResults<Item> findResults = servicefindItems(inboxgetId(), view);
for (Item item : findResultsgetItems()) {
EmailMessage message = EmailMessagebind(service, itemgetId());
List<Attachment> attachs = messagegetAttachments()getItems();
try{
if(messagegetHasAttachments()){
for(Attachment f : attachs){
if(f instanceof FileAttachment){
//接收邮件到临时目录
File tempZip = new File(path,fgetName());
((FileAttachment)f)load(tempZipgetPath());
}
}
//删除邮件
messagedelete(DeleteModeHardDelete);
}
}catch(Exception err){
logequals(err);
}
}
return count;
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)