封装发送Exchange邮箱的工具
package com.tbc.app.im.util;
import lombok.Data;
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.exception.service.local.ServiceLocalException;
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;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.*;
/**
* Exchange 邮件发送工具
*
* @author gekangkang
* @date 2019/11/6 19:42
*/
@Slf4j
@Data
public class ExchangeClient {
private final String hostname;
private final ExchangeVersion exchangeVersion;
private final String domain;
private final String username;
private final String password;
private final String subject;
private final String recipientTo;
private final List<String> recipientCc;
private final List<String> recipientBcc;
private final List<String> attachments;
private final List<UrlAttachments> urlAttachments;
private final String message;
private final String userid;
private final String corpCode;
public static class UrlAttachments {
private String fileName;
private String fileUrl;
public UrlAttachments(String fileName, String fileUrl) {
this.fileName = fileName;
this.fileUrl = fileUrl;
}
}
public ExchangeClient(ExchangeClientImplBuilder builder) {
this.hostname = builder.hostname;
this.exchangeVersion = builder.exchangeVersion;
this.domain = builder.domain;
this.username = builder.username;
this.password = builder.password;
this.subject = builder.subject;
this.recipientTo = builder.recipientTo;
this.recipientCc = builder.recipientCc;
this.recipientBcc = builder.recipientBcc;
this.attachments = builder.attachments;
this.message = builder.message;
this.urlAttachments = builder.urlAttachments;
this.userid = builder.userid;
this.corpCode = builder.corpCode;
}
public static class ExchangeClientImplBuilder {
private String hostname;
private String userid;
private String corpCode;
private ExchangeVersion exchangeVersion;
private String domain;
private String username;
private String password;
private String subject;
private String recipientTo;
private List<String> recipientCc;
private List<String> recipientBcc;
private List<String> attachments;
private String message;
private List<UrlAttachments> urlAttachments;
public ExchangeClientImplBuilder() {
this.userid = "";
this.corpCode = "";
this.exchangeVersion = ExchangeVersion.Exchange2010_SP1;
this.hostname = "";
this.username = "";
this.password = "";
this.subject = "";
this.recipientTo = "";
this.recipientCc = new ArrayList<>(0);
this.recipientBcc = new ArrayList<>(0);
this.attachments = new ArrayList<>(0);
this.message = "";
this.urlAttachments = new ArrayList<>(0);
}
/**
* @param hostname
* @return
* @Desc Exchange Web服务的主机名
*/
public ExchangeClientImplBuilder hostname(String hostname) {
this.hostname = hostname;
return this;
}
/**
* @param exchangeVersion
* @return
* @Desc Exchange Web服务器版本。
*/
public ExchangeClientImplBuilder exchangeVersion(ExchangeVersion exchangeVersion) {
this.exchangeVersion = exchangeVersion;
return this;
}
/**
* @param domain
* @return
* @Desc smtp服务器的域。
*/
public ExchangeClientImplBuilder domain(String domain) {
this.domain = domain;
return this;
}
/**
* @param username
* @return
* @Desc MS Exchange Smtp服务器的用户名 发送用户
*/
public ExchangeClientImplBuilder username(String username) {
this.username = username;
return this;
}
/**
* @param userId
* @return
* @Desc
*/
public ExchangeClientImplBuilder userid(String userId) {
this.userid = userId;
return this;
}
/**
* @param corpCode
* @return
* @Desc
*/
public ExchangeClientImplBuilder corpCode(String corpCode) {
this.corpCode = corpCode;
return this;
}
/**
* @param password
* @return
* @Desc 发送用户的密码
*/
public ExchangeClientImplBuilder password(String password) {
this.password = password;
return this;
}
/**
* @param subject
* @return
* @Desc 邮件主题
*/
public ExchangeClientImplBuilder subject(String subject) {
this.subject = subject;
return this;
}
/**
* @param recipientTo
* @return
* @Desc 收件人
*/
public ExchangeClientImplBuilder recipientTo(String recipientTo) {
this.recipientTo = recipientTo;
return this;
}
/**
* @param recipientCc
* @param recipientsCc
* @return
* @Desc 抄送人
*/
public ExchangeClientImplBuilder recipientCc(String recipientCc, String... recipientsCc) {
// Prepare the list.
List<String> recipients = new ArrayList<>(1 + recipientsCc.length);
recipients.add(recipientCc);
recipients.addAll(Arrays.asList(recipientsCc));
// Set the list.
this.recipientCc = recipients;
return this;
}
/**
* @param recipientCc
* @return
* @Desc 抄送人
*/
public ExchangeClientImplBuilder recipientCc(List<String> recipientCc) {
this.recipientCc = recipientCc;
return this;
}
/**
* @param recipientBcc
* @param recipientsBcc
* @return
* @Desc 密送人
*/
public ExchangeClientImplBuilder recipientBcc(String recipientBcc, String... recipientsBcc) {
// Prepare the list.
List<String> recipients = new ArrayList<>(1 + recipientsBcc.length);
recipients.add(recipientBcc);
recipients.addAll(Arrays.asList(recipientsBcc));
// Set the list.
this.recipientBcc = recipients;
return this;
}
/**
* @param recipientBcc
* @return
* @Desc 密送人
*/
public ExchangeClientImplBuilder recipientBcc(List<String> recipientBcc) {
this.recipientBcc = recipientBcc;
return this;
}
/**
* @param attachment
* @param attachments
* @return
* @Desc 附件
*/
public ExchangeClientImplBuilder attachments(String attachment, String... attachments) {
// Prepare the list.
List<String> attachmentsToUse = new ArrayList<>(1 + attachments.length);
attachmentsToUse.add(attachment);
attachmentsToUse.addAll(Arrays.asList(attachments));
// Set the list.
this.attachments = attachmentsToUse;
return this;
}
/**
* @return
* @Desc 附件
*/
public ExchangeClientImplBuilder urlAttachments(List<UrlAttachments> urlAttachments) {
this.urlAttachments = urlAttachments;
return this;
}
/**
* @param attachments
* @return
* @Desc 附件
*/
public ExchangeClientImplBuilder attachments(List<String> attachments) {
this.attachments = attachments;
return this;
}
/**
* @param message
* @return
* @Desc 邮件正文
*/
public ExchangeClientImplBuilder message(String message) {
this.message = message;
return this;
}
/**
* @return
* @Desc 创建邮件
*/
public ExchangeClient build() {
return new ExchangeClient(this);
}
}
/**
* @return
* @Desc 发送邮件
*/
public void sendExchange() throws IOException, ServiceLocalException {
// Exchange服务器版本。
ExchangeService exchangeService = new ExchangeService(exchangeVersion);
// 要在MS Exchange服务器上签名的凭据。
ExchangeCredentials exchangeCredentials = new WebCredentials(username, password, domain);
exchangeService.setCredentials(exchangeCredentials);
// 邮箱的exchange web服务的URL
try {
exchangeService.setUrl(new URI("https://" + hostname + "/ews/Exchange.asmx"));
exchangeService.setTimeout(5000);
} catch (URISyntaxException ex) {
log.error("创建与服务端的连接发生异常", ex);
if (exchangeService != null) {
exchangeService.close();
}
return;
}
// 设置邮件信息
EmailMessage emailMessage;
try {
emailMessage = new EmailMessage(exchangeService);
emailMessage.setSubject(subject);
emailMessage.setBody(MessageBody.getMessageBodyFromText(message));
} catch (Exception ex) {
log.error("设置邮件发生异常", ex);
return;
}
// 设置收件人
try {
emailMessage.getToRecipients().add(recipientTo);
} catch (ServiceLocalException ex) {
log.error("设置邮件收件人发生异常.", ex);
return;
}
// 设置抄送人
for (String recipient : recipientCc) {
try {
emailMessage.getCcRecipients().add(recipient);
} catch (ServiceLocalException ex) {
log.error("设置邮件抄送人发生异常.", ex);
return;
}
}
// 设置邮件密送人
for (String recipient : recipientBcc) {
try {
emailMessage.getBccRecipients().add(recipient);
} catch (ServiceLocalException ex) {
log.error("设置邮件密送人发生异常.", ex);
return;
}
}
// 设置本地附件
for (String attachmentPath : attachments) {
try {
emailMessage.getAttachments().addFileAttachment(attachmentPath);
} catch (ServiceLocalException ex) {
log.error("设置邮件附件发生异常", ex);
return;
}
}
// 设置url附件
for (UrlAttachments urlAttachment : urlAttachments) {
String fileName = urlAttachment.fileName;
emailMessage.getAttachments().addFileAttachment(fileName, new URL(urlAttachment.fileUrl).openStream());
}
try {
emailMessage.send();
log.info("邮件发送成功.");
} catch (Exception ex) {
log.error("邮件发送异常.", ex);
return;
}
}
}
构建发送邮件参数
//imSentMessage 是封装了邮件信息的实体对象
public ExchangeClient convertExchange(ImSentMessage imSentMessage) {
if (filterNull(imSentMessage)) return null;
EmailServerProperty server = switchEmailServer(imSentMessage.getCorpCode());
if (null == server) return null;
ExchangeClient.ExchangeClientImplBuilder builder = new ExchangeClient.ExchangeClientImplBuilder();
builder.username(server.getSmtpAccount());
builder.password(server.getSmtpPassword());
builder.exchangeVersion(ExchangeVersion.Exchange2010_SP2);
builder.message(imSentMessage.getMessageContent());
builder.subject(imSentMessage.getMessageTitle());
builder.hostname(server.getSmtpHost());
builder.userid(imSentMessage.getImUser().getUserId());
builder.corpCode(imSentMessage.getImUser().getCorpCode());
//收件人
builder.recipientTo(imSentMessage.getImUser().getEmail());
convertExchangeAttachment(builder, imSentMessage.getAttachmentFile());
return builder.build();
}
//构建成功之后直接调用sendExchange即可发送邮件,发送的时候建议使用多线程提高发送效率
来源:CSDN
作者:名字真的重要吗
链接:https://blog.csdn.net/qq_41878811/article/details/103458233