1. pom.xml 导入包
<!-- 微软邮箱发送依赖-->
<dependency>
<groupId>com.microsoft.ews-java-api</groupId>
<artifactId>ews-java-api</artifactId>
<version>2.0</version>
</dependency>
2. 邮件发送代码
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.service.item.EmailMessage;
import microsoft.exchange.webservices.data.credential.ExchangeCredentials;
import microsoft.exchange.webservices.data.credential.WebCredentials;
import microsoft.exchange.webservices.data.property.complex.MessageBody;
public class MailExchangeManager {
/**
*
* @param username 发件人用户名
* @param password 发件人密码
* @param sendAddress 发件人地址
* @param to 收件人数组
* @param cc 抄送人数组
* @param subject 主题
* @param content 内容
* @param filePath 附件路径
* @return 发送结果
*/
public boolean sendMail(String username,String password,String sendAddress,String[] to, String[] cc,
String subject, String content, String[] filePath) {
boolean result=true;
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
ExchangeCredentials credentials = new WebCredentials(username,password);
service.setCredentials(credentials);
try {
service.autodiscoverUrl(sendAddress);
EmailMessage msg = new EmailMessage(service);
msg.setSubject(subject);
MessageBody body = MessageBody.getMessageBodyFromText(content);
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 (filePath != null) {
for (String attachmentPath : filePath) {
msg.getAttachments().addFileAttachment(attachmentPath);
}
}
msg.send();
} catch (Exception e) {
result=false;
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
MailExchangeManager mailExchangeManager = new MailExchangeManager();
String[] to={"-----@163.com"};
String[] cc={"---@qq.com","---@126.com"};
String[] filePath={"D:\\---\\Desktop\\aaaa.xlsx",
"D:\\---\\Desktop\\aaaa - 副本.xlsx"};
mailExchangeManager.sendMail("---","---",
"---",to,cc,
"test1819","ceshinierong",filePath);
}
}
参考: