- 引入工具包
<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;
/**
* @ClassName ExchangeMailUtil
* @Description microsoft邮件工具类
* @Author Miccke
* @Date 2022/5/18 9:29
* @Version V2.0
*/
@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;
}
/**
* 创建邮件服务
*
* @return 邮件服务
*/
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;
}
/**
* 收取邮件
*
* @param receiveMailNum 最大收取邮件数
* @throws Exception 全局异常捕获
*/
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;
}
/**
* 收取邮件
*
* @param receiveMailNum 最大收取邮件数
* @throws Exception 全局异常捕获
*/
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());
// BodyType 分为两种 HTML、Text
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;
}
/**
* 收取所有邮件
*
* @throws Exception
*/
public List<EmailMessage> scanMail() throws Exception {
return scanMail(-1);
}
/**
* 发送带附件的mail
*
* @param subject 邮件标题
* @param to 收件人列表
* @param cc 抄送人列表
* @param bodyText 邮件内容
* @param attachmentPaths 附件地址列表
* @throws Exception
*/
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();
}
/**
* 发送不带抄送人的邮件
*
* @param subject 标题
* @param to 收件人列表
* @param bodyText 邮件内容
* @throws Exception
*/
public void sendMail(String subject, String[] to, String bodyText) throws Exception {
sendMail(subject, to, null, bodyText);
}
/**
* 发送不带附件的mail
*
* @param subject 邮件标题
* @param to 收件人列表
* @param cc 抄送人列表
* @param bodyText 邮件内容
* @throws Exception
*/
public void sendMail(String subject, String[] to, String[] cc, String bodyText) throws Exception {
sendMail(subject, to, cc, bodyText, null);
}
/**
* 发送邮件主方法
*
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// Outlook Web Access路径通常为/EWS/exchange.asmx
ExchangeMailUtil mailUtil = new ExchangeMailUtil(
"https://mail.****.com/ews/exchange.asmx",
"gulushuai1",
"Miccke123456");
mailUtil.scanMailByFolder(-1, "华星OA");
}
}
文章来源地址https://www.toymoban.com/news/detail-527615.html
文章来源:https://www.toymoban.com/news/detail-527615.html
到了这里,关于Java集成Outlook邮件操作的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!