Java集成Outlook邮件操作
- 引入工具包
<dependency>
<groupId>com.microsoft.ews-java-api</groupId>
<artifactId>ews-java-api</artifactId>
<version>2.0</version>
</dependency>
- 工具类
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import microsoft.exchange.webservices.data.core.ExchangeService;
import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
import microsoft.exchange.webservices.data.core.enumeration.property.BodyType;
import microsoft.exchange.webservices.data.core.enumeration.property.WellKnownFolderName;
import microsoft.exchange.webservices.data.core.exception.service.local.ServiceLocalException;
import microsoft.exchange.webservices.data.core.service.folder.Folder;
import microsoft.exchange.webservices.data.core.service.item.EmailMessage;
import microsoft.exchange.webservices.data.core.service.item.Item;
import microsoft.exchange.webservices.data.credential.ExchangeCredentials;
import microsoft.exchange.webservices.data.credential.WebCredentials;
import microsoft.exchange.webservices.data.property.complex.MessageBody;
import microsoft.exchange.webservices.data.search.FindFoldersResults;
import microsoft.exchange.webservices.data.search.FindItemsResults;
import microsoft.exchange.webservices.data.search.FolderView;
import microsoft.exchange.webservices.data.search.ItemView;
import org.apache.commons.lang3.StringUtils;
@Slf4j
public class ExchangeMailUtil {
private String mailServer;
private String user;
private String password;
private String domain;
public ExchangeMailUtil(String mailServer, String user, String password) {
this.mailServer = mailServer;
this.user = user;
this.password = password;
}
public ExchangeMailUtil(String mailServer, String user, String password, String domain) {
this.mailServer = mailServer;
this.user = user;
this.password = password;
this.domain = domain;
}
private ExchangeService getExchangeService() {
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
ExchangeCredentials credentials;
if (domain == null) {
credentials = new WebCredentials(user, password);
} else {
credentials = new WebCredentials(user, password, domain);
}
service.setCredentials(credentials);
try {
service.setUrl(new URI(mailServer));
} catch (URISyntaxException e) {
e.printStackTrace();
}
return service;
}
public List<EmailMessage> scanMail(int receiveMailNum) throws Exception {
if (receiveMailNum < -1) throw new Exception("请输入正确的查询数量");
ExchangeService exchangeService = this.getExchangeService();
Folder inbox = Folder.bind(exchangeService, WellKnownFolderName.Inbox);
int receiveNum = receiveMailNum == -1 ? inbox.getTotalCount()
: Math.min(inbox.getTotalCount(), receiveMailNum);
FindItemsResults<Item> findResults = exchangeService.findItems(inbox.getId(), new ItemView(receiveNum));
List<EmailMessage> result = new ArrayList<>();
for (Item item : findResults.getItems()) {
EmailMessage message = EmailMessage.bind(exchangeService, item.getId());
if (message.getBody().getBodyType() != BodyType.Text) continue;
result.add(message);
}
return result;
}
public List<EmailMessage> scanMailByFolder(int receiveMailNum, String targetFolderName) throws Exception {
ExchangeService exchangeService = this.getExchangeService();
FindFoldersResults foldersResults = exchangeService.findFolders(
WellKnownFolderName.Inbox, new FolderView(999999));
System.out.println("文件夹总数量:" + foldersResults.getTotalCount());
if (StringUtils.isEmpty(targetFolderName)) targetFolderName = WellKnownFolderName.Inbox.name();
String finalTargetFolderName = targetFolderName;
Folder targetFolder = foldersResults.getFolders().stream().filter(folder -> {
try {
return folder.getDisplayName().equals(finalTargetFolderName);
} catch (ServiceLocalException e) {
e.printStackTrace();
return Boolean.FALSE;
}
}).collect(Collectors.toList()).stream().findFirst().get();
List<EmailMessage> emailMessageList = new ArrayList<>();
System.out.println("文件夹名称:" + targetFolder.getDisplayName());
ArrayList<Item> findItemsResults = targetFolder.findItems(
new ItemView(targetFolder.getTotalCount())).getItems();
for (Item item : findItemsResults) {
EmailMessage emailMessage = EmailMessage.bind(exchangeService, item.getId());
if (emailMessage.getBody().getBodyType() != BodyType.HTML) continue;
System.out.println("**************************************************);
System.out.println("【发送人】" + emailMessage.getSender());
System.out.println("【接收人】" + emailMessage.getReceivedBy());
System.out.println("【标题】" + emailMessage.getSubject());
System.out.println("【邮件内容】" + emailMessage.getBody());
emailMessageList.add(emailMessage);
}
return emailMessageList;
}
public List<EmailMessage> scanMail() throws Exception {
return scanMail(-1);
}
public void sendMail(String subject, String[] to, String[] cc, String bodyText, String[] attachmentPaths) throws Exception {
ExchangeService service = this.getExchangeService();
EmailMessage msg = new EmailMessage(service);
msg.setSubject(subject);
MessageBody body = MessageBody.getMessageBodyFromText(bodyText);
body.setBodyType(BodyType.HTML);
msg.setBody(body);
for (String toPerson : to) {
msg.getToRecipients().add(toPerson);
}
if (cc != null) {
for (String ccPerson : cc) {
msg.getCcRecipients().add(ccPerson);
}
}
if (attachmentPaths != null) {
for (String attachmentPath : attachmentPaths) {
msg.getAttachments().addFileAttachment(attachmentPath);
}
}
msg.send();
}
public void sendMail(String subject, String[] to, String bodyText) throws Exception {
sendMail(subject, to, null, bodyText);
}
public void sendMail(String subject, String[] to, String[] cc, String bodyText) throws Exception {
sendMail(subject, to, cc, bodyText, null);
}
public static void main(String[] args) throws Exception {
ExchangeMailUtil mailUtil = new ExchangeMailUtil(
"https://mail.****.com/ews/exchange.asmx",
"****",
"****");
mailUtil.scanMailByFolder(-1, "TEST");
}
}