Java通過exchange協(xié)議發(fā)送郵件
本文實(shí)例為大家分享了Java通過exchange協(xié)議發(fā)送郵件的具體代碼,供大家參考,具體內(nèi)容如下
pom.xml 導(dǎo)入包
<dependency> <groupId>com.microsoft.ews-java-api</groupId> <artifactId>ews-java-api</artifactId> <version>2.0</version> </dependency>
application.properties 配置信息
#郵箱地址 youjia.exchange.mail.username=123@abc.com #郵箱密碼 youjia.exchange.mail.password=123456 #郵箱exchange服務(wù)地址,如果不知道找運(yùn)維 youjia.exchange.mail.host=https://*****/ews/exchange.asmx
代碼
package com.youjia.found.manager; import com.youjia.found.common.util.Check; 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 org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import javax.mail.internet.InternetAddress; import java.net.URI; /** * <P>exchange郵件處理類</P> * * @author eric * @date 2020/2/6 11:08 AM * @since */ @Component public class MailExchangeManager { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Value("${youjia.exchange.mail.username}") private String username ; @Value("${youjia.exchange.mail.password}") private String password; @Value("${youjia.exchange.mail.host}") private String host ; /** * 使用Exchange協(xié)議發(fā)送 * @param to 收件人 * @param subject 郵件主題 * @param content 正文 * @param filePath 附件 * * @throws Exception */ public boolean sendMail(String to, String subject, String content, String filePath) { logger.info("exchange郵件發(fā)送 to:{}, subject:{}, content:{},filePath:{}", to, subject, content,filePath); boolean isOK=false; ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); ExchangeCredentials credentials = new WebCredentials(username,password); service.setCredentials(credentials); try { service.setUrl(new URI(host)); EmailMessage msg = new EmailMessage(service); msg.setSubject(subject); MessageBody body = MessageBody.getMessageBodyFromText(content); body.setBodyType(BodyType.HTML); msg.setBody(body); //支持多個(gè)收件人 InternetAddress[] addresses = InternetAddress.parse(to); for (InternetAddress address : addresses) { msg.getToRecipients().add(address.getAddress()); } if (Check.notEmpty(filePath)) { msg.getAttachments().addFileAttachment(filePath); } msg.send(); isOK=true; } catch (Exception e) { logger.error(e.getMessage(),e); isOK= false; } return isOK; } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springboot集成springsecurity 使用OAUTH2做權(quán)限管理的教程
這篇文章主要介紹了springboot集成springsecurity 使用OAUTH2做權(quán)限管理的教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-12-12Java中隨機(jī)數(shù)的產(chǎn)生方式與原理詳解
這篇文章主要介紹了Java中隨機(jī)數(shù)的產(chǎn)生方式與原理詳解的相關(guān)資料,需要的朋友可以參考下2016-11-11Java利用Socket實(shí)現(xiàn)網(wǎng)絡(luò)通信功能
在早期的網(wǎng)絡(luò)編程中,Socket是很常見的實(shí)現(xiàn)技術(shù)之一,比如早期的聊天室,就是基于這種技術(shù)進(jìn)行實(shí)現(xiàn)的,另外現(xiàn)在有些消息推送,也可以基于Socket實(shí)現(xiàn),本文小編給大家介紹了Java利用Socket實(shí)現(xiàn)網(wǎng)絡(luò)通信功能的示例,需要的朋友可以參考下2023-11-11uploadify上傳及后臺(tái)文件合法性驗(yàn)證的代碼解析
這篇文章主要介紹了uploadify上傳及后臺(tái)文件合法性驗(yàn)證的代碼解析,整段代碼分為后臺(tái)上傳方法,文件合法性驗(yàn)證類,前端上傳js,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11SpringMVC中Invalid bound statement (not f
本文主要介紹了SpringMVC中Invalid bound statement (not found)常見報(bào)錯(cuò)問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05