您好,登錄后才能下訂單哦!
這篇文章主要講解了“Java怎么通過exchange協議發送郵件”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Java怎么通過exchange協議發送郵件”吧!
pom.xml 導入包
<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服務地址,如果不知道找運維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 */@Componentpublic 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協議發送 * @param to 收件人 * @param subject 郵件主題 * @param content 正文 * @param filePath 附件 * * @throws Exception */ public boolean sendMail(String to, String subject, String content, String filePath) { logger.info("exchange郵件發送 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); //支持多個收件人 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; } }
感謝各位的閱讀,以上就是“Java怎么通過exchange協議發送郵件”的內容了,經過本文的學習后,相信大家對Java怎么通過exchange協議發送郵件這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。