首先引入maven依赖
<dependency>
<groupId>com.microsoft.ews-java-api</groupId>
<artifactId>ews-java-api</artifactId>
<version>2.0</version>
</dependency>
package cn.kanyun.test;
import java.io.File;
import java.net.URI;
import org.apache.commons.io.FileUtils;
import microsoft.exchange.webservices.data.core.ExchangeService;
import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
import microsoft.exchange.webservices.data.core.enumeration.property.WellKnownFolderName;
import microsoft.exchange.webservices.data.core.enumeration.service.ConflictResolutionMode;
import microsoft.exchange.webservices.data.core.service.item.EmailMessage;
import microsoft.exchange.webservices.data.core.service.item.Item;
import microsoft.exchange.webservices.data.core.service.schema.EmailMessageSchema;
import microsoft.exchange.webservices.data.credential.ExchangeCredentials;
import microsoft.exchange.webservices.data.credential.WebCredentials;
import microsoft.exchange.webservices.data.property.complex.AttachmentCollection;
import microsoft.exchange.webservices.data.property.complex.FileAttachment;
import microsoft.exchange.webservices.data.search.FindItemsResults;
import microsoft.exchange.webservices.data.search.ItemView;
import microsoft.exchange.webservices.data.search.filter.SearchFilter;
import microsoft.exchange.webservices.data.property.complex.Attachment;
public class MainTest4 {
public static void main(String[] args) {
try {
scanMail("*****","*****");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 扫描邮件
*
* @param emailAddress
* @param password
* @throws Exception
*/
public static void scanMail(String emailAddress, String password) throws Exception {
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
// 校验邮箱地址是否正确
String name = emailAddress.split("@", 2)[0];
// 初始化的新实例WebCredentials与用户名、 密码和身份验证的帐户的域。
ExchangeCredentials credentials = new WebCredentials(name, password, "账户的域");
// 获取或设置用于验证与 Exchange Web Services (EWS) 的凭据
service.setCredentials(credentials);
// PropertySet detailedPropertySet = new PropertySet(BasePropertySet.FirstClassProperties, AppointmentSchema.Recurrence);
// 创建过滤器, 条件为邮件未读.
SearchFilter filter = new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false);
// 设置Exchange Web服务的URL
service.setUrl(new URI("https://mail.****.com/ews/Exchange.asmx"));
// 读取多少封邮件
ItemView view = new ItemView(10);
// WellKnownFolderName是枚举类,定义在用户的邮箱中使用的公共文件夹名称,如Inbox:"收件箱"文件夹 Calendar:""日历"文件夹"
FindItemsResults<Item> findResults = service.findItems(WellKnownFolderName.Inbox, filter, view);
System.out.println("找到:" + findResults.getItems().size() + "封邮件");
// service.loadPropertiesForItems(findResults.getItems(), detailedPropertySet);
for (Item item : findResults.getItems()) {
// 判断是否有附件
if (!item.getHasAttachments()) {
continue;
}
System.out.println("有附件的邮件!");
EmailMessage message = EmailMessage.bind(service, item.getId());
// 设置邮件为已读状态
message.setIsRead(true);
// 将对邮件的改动提交到服务器
message.update(ConflictResolutionMode.AlwaysOverwrite);
System.out.println("发送人->" + message.getSender());
System.out.println("标题--->" + item.getSubject());
System.out.println("邮件ID->" + item.getId());
// 邮件内容,html形式
System.out.println(item.getBody().toString());
// 附件列表
AttachmentCollection attachments = message.getAttachments();
for (Attachment attachment : attachments) {
// 附件名
System.out.println(attachment.getName());
// 指示是否是内嵌附件
System.out.println(attachment.getIsInline());
// 加载附件
attachment.load();
FileAttachment fileAttachment = (FileAttachment) attachment;
// 必须先调用Attachment.load()方法才能获取到内容
byte[] content = fileAttachment.getContent();
// 将文件保存到指定位置
FileUtils.writeByteArrayToFile(new File("C:\\Users\\admin\\Desktop\\Script-2.sql"), content);
System.out.println("+++++++++");
}
}
}
}
上述代码直接搜索的是收件箱中的邮件,其实还是指定目录进行获取邮件
public static void scanMail(String emailAddress, String password) throws Exception {
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
String name = emailAddress.split("@", 2)[0];
ExchangeCredentials credentials =
new WebCredentials(name, password, "msxf");
service.setCredentials(credentials);
service.setUrl(new URI("https://mail.xxxx.com/ews/Exchange.asmx"));
// 读取多少封邮件
ItemView view = new ItemView(100);
// 读取文件夹(即每页显示多少个,最终展示几页) 这里与常见的分页类似,因此此值可以设置为较大值,来避免分页,当然根据情况来定
FolderView folderView = new FolderView(100);
// 搜索文件夹,第一个参数为父文件夹,这里设置为了收件箱,因为收件箱是本来就有的文件夹,同样的参考WellKnownFolderName枚举
FindFoldersResults foldersResults = service.findFolders(WellKnownFolderName.Inbox, folderView);
System.out.println("文件夹总数量:" + foldersResults.getTotalCount());
for (Folder folder : foldersResults.getFolders()) {
// folder.getId().getFolderName() 这一个api并不是获取文件夹名称,因为id和folerName是互斥的
System.out.println("文件夹名称:" + folder.getDisplayName());
FindItemsResults<Item> findItemsResults = folder.findItems(view);
for (Item item : findItemsResults.getItems()) {
EmailMessage message = EmailMessage.bind(service, item.getId());
System.out.println("发送人->" + message.getSender());
System.out.println("标题--->" + message.getSubject());
System.out.println("邮件ID->" + message.getId());
}
}
微软 EWS API : https://docs.microsoft.com/zh-cn/dotnet/api/microsoft.exchange.webservices.data?view=exchange-ews-api