網上介紹利用spring boot通過POP3、IMAP、SMTP等協議來發送電子郵件的資料非常多,然而介紹利用Exchange郵件服務器來發郵件的資料卻極少。下面來看看,如何在spring boot中用Exchange服務器來發郵件,以及需要注意的問題。
首先,你需要知道郵件服務器的地址,例如我下面的例子中服務器地址是https://webmail.XXXXX.com/EWS/Exchange.asmx(XXXX是公司域名,不便公開)。其次,要保證你能正確連接上這個郵件服務器,檢驗的方法是在瀏覽器中輸入這個服務器地址,會返回一個xml文件。某些公司有安全設置,只能在內部網絡中登錄這個服務器,或者需要通過VPN才可以登錄這個服務器。
如果上述的條件都滿足了,那么就可以開始編寫spring boot的代碼了。
一、引入依賴ews-java-api
<dependency> <groupId>com.microsoft.ews-java-api</groupId> <artifactId>ews-java-api</artifactId> <version>2.0</version> </dependency>
二、新建一個郵件配置類:ExchangeMailUtils
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; import java.net.URI; import java.net.URISyntaxException; public class ExchangeMailUtils { private String mailServer; //郵件服務器地址 private String user; //登錄用戶名 private String password; //登錄密碼 private String domain; //域 public ExchangeMailUtils(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.Exchange2007_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; } /** * 發送帶附件的mail * @param subject 郵件標題 * @param to 收件人列表 * @param cc 抄送人列表 * @param bodyText 郵件內容 * @param attachmentPaths 附件地址列表 * @throws Exception */ public void send(String subject, String[] to, String[] cc, String bodyText, String[] attachmentPaths) throws Exception { ExchangeService service = 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(); } /** * 發送不帶附件的mail * @param subject 郵件標題 * @param to 收件人列表 * @param cc 抄送人列表 * @param bodyText 郵件內容 * @throws Exception */ public void send(String subject, String[] to, String[] cc, String bodyText) throws Exception { send(subject, to, cc, bodyText, null); } }
三、控制類
@RestController public class SendEmail { @RequestMapping(value = {"/email"}, method = RequestMethod.GET, produces = "application/json;charset=UTF-8") public void sendemail( ) throws Exception { ExchangeMailUtils exchangeMailUtils=new ExchangeMailUtils("https://webmail.XXXXX.com/EWS/Exchange.asmx", "用戶名", "密碼","域"); String [] a={"收件人郵箱"}; String [] cc={}; exchangeMailUtils.send("主題",a,null,"本體"); } }
四、可能出現的錯誤:
1、The remote server returned an error: (403)Forbidden:出現這個錯誤可能是因為你不在內部網或者沒有通過VPN連接服務器。
2、Cannot invoke "String.equalsIgnoreCase(String)" because "scheme" is null:出現這個錯誤是因為郵件服務器地址漏寫了https: