微軟exchange郵箱發送

使用java發送exchange類型的郵件,foxmail中配置如下圖:

需要的maven依賴如下:

<dependency><groupId>com.microsoft.ews-java-api</groupId><artifactId>ews-java-api</artifactId><version>2.0</version>
</dependency>
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional>
</dependency>

配置文件email.properties內容如下:

# Exchange郵箱配置
email.exchange.username=發件人郵箱
email.exchange.password=發件人郵箱密碼
email.exchange.server=郵箱服務器地址

?工具類代碼如下:

package com.utils;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.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;@Slf4j
public class ExchangeClient {private static final Properties PROPERTIES = new Properties();static {try {loadProperties();} catch (IOException e) {e.printStackTrace();}}private static void loadProperties() throws IOException {try (InputStream input = ExchangeClient.class.getClassLoader().getResourceAsStream("email.properties")) {PROPERTIES.load(input);}}private static String getProperty(String key) {return PROPERTIES.getProperty(key);}private final String hostname = getProperty("email.exchange.server");private final String username = getProperty("email.exchange.username");private final String password = getProperty("email.exchange.password");private final ExchangeVersion exchangeVersion;private final String domain;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 String message;private ExchangeClient(ExchangeClientBuilder builder) {this.exchangeVersion = builder.exchangeVersion;this.domain = builder.domain;this.subject = builder.subject;this.recipientTo = builder.recipientTo;this.recipientCc = builder.recipientCc;this.recipientBcc = builder.recipientBcc;this.attachments = builder.attachments;this.message = builder.message;}public static class ExchangeClientBuilder {private String hostname;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;public ExchangeClientBuilder() {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 = "";}/*** The hostname of the Exchange Web Service. It will be used for* connecting with URI https://hostname/ews/exchange.asmx** @param hostname the hostname of the MS Exchange Smtp Server.* @return the builder for chain usage.*/public ExchangeClientBuilder hostname(String hostname) {this.hostname = hostname;return this;}/*** The Exchange Web Server version.** @param exchangeVersion the Exchange Web Server version.* @return the builder for chain usage.*/public ExchangeClientBuilder exchangeVersion(ExchangeVersion exchangeVersion) {this.exchangeVersion = exchangeVersion;return this;}/*** The domain of the MS Exchange Smtp Server.** @param domain the domain of the Active Directory. The first part of*               the username. For example: MYDOMAIN\\username, set the MYDOMAIN.* @return the builder for chain usage.*/public ExchangeClientBuilder domain(String domain) {this.domain = domain;return this;}/*** The username of the MS Exchange Smtp Server. The second part of the* username. For example: MYDOMAIN\\username, set the username.** @param username the username of the MS Exchange Smtp Server.* @return the builder for chain usage.*/public ExchangeClientBuilder username(String username) {this.username = username;return this;}/*** The password of the MS Exchange Smtp Server.** @param password the password of the MS Exchange Smtp Server.* @return the builder for chain usage.*/public ExchangeClientBuilder password(String password) {this.password = password;return this;}/*** The subject for this send.** @param subject the subject for this send.* @return the builder for chain usage.*/public ExchangeClientBuilder subject(String subject) {this.subject = subject;return this;}/*** The recipient for this send.** @param recipientTo the recipient for this send.* @return the builder for chain usage.*/public ExchangeClientBuilder recipientTo(String recipientTo) {this.recipientTo = recipientTo;return this;}/*** You can specify one or more email address that will be used as cc* recipients.** @param recipientCc  the first cc email address.* @param recipientsCc the other cc email address for this send.* @return the builder for chain usage.*/public ExchangeClientBuilder 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;}/*** You can specify a list with email addresses that will be used as cc* for this email send.** @param recipientCc the list with email addresses that will be used as*                    cc for this email send.* @return the builder for chain usage.*/public ExchangeClientBuilder recipientCc(List<String> recipientCc) {this.recipientCc = recipientCc;return this;}/*** You can specify one or more email address that will be used as bcc* recipients.** @param recipientBcc  the first bcc email address.* @param recipientsBcc the other bcc email address for this send.* @return the builder for chain usage.*/public ExchangeClientBuilder 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;}/*** You can specify a list with email addresses that will be used as bcc* for this email send.** @param recipientBcc the list with email addresses that will be used*                     as bcc for this email send.* @return the builder for chain usage.*/public ExchangeClientBuilder recipientBcc(List<String> recipientBcc) {this.recipientBcc = recipientBcc;return this;}/*** You can specify one or more email address that will be used as cc* recipients.** @param attachment  the first attachment.* @param attachments the other attachments for this send.* @return the builder for chain usage.*/public ExchangeClientBuilder 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;}/*** You can specify a list with email attachments that will be used for* this email send.** @param attachments the list with email attachments that will be used*                    for this email send.* @return the builder for chain usage.*/public ExchangeClientBuilder attachments(List<String> attachments) {this.attachments = attachments;return this;}/*** The body of the email message.** @param message the body of the email message.* @return the builder for chain usage.*/public ExchangeClientBuilder message(String message) {this.message = message;return this;}/*** Build a mail.** @return an EmailApacheUtils object.*/public ExchangeClient build() {return new ExchangeClient(this);}}public boolean sendExchange() {// The Exchange Server Version.ExchangeService exchangeService = new ExchangeService(exchangeVersion);// Credentials to sign in the MS Exchange Server.ExchangeCredentials exchangeCredentials = new WebCredentials(username, password, domain);exchangeService.setCredentials(exchangeCredentials);// URL of exchange web service for the mailbox.try {exchangeService.setUrl(new URI("https://" + hostname + "/ews/Exchange.asmx"));} catch (URISyntaxException ex) {log.info("An exception occured while creating the uri for exchange service.", ex);return false;}// The email.EmailMessage emailMessage;try {emailMessage = new EmailMessage(exchangeService);emailMessage.setSubject(subject);emailMessage.setBody(MessageBody.getMessageBodyFromText(message));} catch (Exception ex) {log.info("An exception occured while setting the email message.", ex);return false;}// TO recipient.try {emailMessage.getToRecipients().add(recipientTo);} catch (ServiceLocalException ex) {log.info("An exception occured while sstting the TO recipient(" + recipientTo + ").", ex);return false;}// CC recipient.for (String recipient : recipientCc) {try {emailMessage.getCcRecipients().add(recipient);} catch (ServiceLocalException ex) {log.info("An exception occured while sstting the CC recipient(" + recipient + ").", ex);return false;}}// BCC recipientfor (String recipient : recipientBcc) {try {emailMessage.getBccRecipients().add(recipient);} catch (ServiceLocalException ex) {log.info("An exception occured while sstting the BCC recipient(" + recipient + ").", ex);return false;}}// Attachements.for (String attachmentPath : attachments) {try {emailMessage.getAttachments().addFileAttachment(attachmentPath);} catch (ServiceLocalException ex) {log.info("An exception occured while setting the attachment.", ex);return false;}}try {emailMessage.send();log.info("An email is send.");} catch (Exception ex) {log.info("An exception occured while sending an email.", ex);return false;}return true;}}

調用代碼如下:

 public static void main(String[] args) {ExchangeClient exchangeClient = new ExchangeClient.ExchangeClientBuilder().exchangeVersion(ExchangeVersion.Exchange2010).recipientTo("收件人郵箱").recipientCc("抄送人郵箱1", "抄送人郵箱2").recipientBcc("密送人郵箱").subject("郵件主題").message("郵件內容").build();boolean sendExchange = exchangeClient.sendExchange();System.err.println("send result:" + sendExchange);}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/10567.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/10567.shtml
英文地址,請注明出處:http://en.pswp.cn/web/10567.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

PCIE協議-2-事務層規范-Message Request Rules

2.2.8 消息請求規則 本文檔定義了以下幾組消息&#xff1a; INTx 中斷信號電源管理錯誤信號鎖定事務支持插槽電源限制支持廠商定義消息延遲容忍度報告&#xff08;LTR&#xff09;消息優化緩沖區沖洗/填充&#xff08;OBFF&#xff09;消息設備就緒狀態&#xff08;DRS&#…

【系統架構師】-案例篇(八)數據流圖

數據流&#xff1a;數據流是系統中數據的流動&#xff0c;它可以是輸入、輸出或存儲在系統中的數據。 數據處理過程&#xff1a;數據處理過程是對數據進行處理的單元&#xff0c;可以是一個物理設備或軟件模塊。 數據存儲&#xff1a;數據存儲是系統中存儲數據的單元&#xff0…

焦作定制在線教育系統上線,小學英語教案怎么寫?教案要怎么下筆?

說到小學英語這也是當前&#xff0c;學生們的重點&#xff0c;那作為配套的輔導機構&#xff0c;要怎么寫教案?這也是需要關注的地方&#xff0c;因為教案關系著教學&#xff0c;有了它學生們上課才會更有效率&#xff0c;所以&#xff0c;會寫教案也是上課的第一步。 教案要怎…

小紅書·電商運營課:小紅書開店流程,小紅書電商如何運營(18節視頻課)

課程目錄 第1節課:學習流程以及后續實操流程注意事項 第2節課:小紅書店鋪類型解析以及開店細節 第3節課:小紅書電商運營兩種玩法之多品店鋪解析 第4節課:小紅書電商運營兩種玩法之單品店鋪解析 第5節課:選品課(多品類類目推薦) 第6節課:選品課(多品類類目推薦) 第7節課:…

百度GL地圖實現某一段路的路況(new BMapGL.DrivingRouteLine)

功能描述&#xff1a; 1.百度地圖實現點擊地圖出現起點&#xff0c;再次點擊出現終點&#xff08;起點終點能拖動&#xff09;繪制完終點后獲取該路的路況并且起點和終點可以拖動實現實時更新&#xff08;新繪制的路段的&#xff09;路況 2.地點搜索 效果如下&#xff1a; 關鍵…

Springboot+Vue項目-基于Java+MySQL的制造裝備物聯及生產管理ERP系統(附源碼+演示視頻+LW)

大家好&#xff01;我是程序猿老A&#xff0c;感謝您閱讀本文&#xff0c;歡迎一鍵三連哦。 &#x1f49e;當前專欄&#xff1a;Java畢業設計 精彩專欄推薦&#x1f447;&#x1f3fb;&#x1f447;&#x1f3fb;&#x1f447;&#x1f3fb; &#x1f380; Python畢業設計 &…

Web自動化-日志收集

目標 1. 理解日志的相關概念 2. 掌握日志的基本用法 3. 掌握日志的高級用法 一、日志相關概念 目標 1. 了解日志的概念 2. 理解日志的作用 3. 掌握常見的日志級別 1. 日志 概念&#xff1a;日志就是用于記錄系統運行時的信息&#xff0c;對一個事件的記錄&#xff1b…

ffmpeg解析rtsp流獲取視頻的寬高

要使用FFmpeg來解析RTSP流并獲取視頻的寬度和高度,你可以使用avformat_find_stream_info函數來獲取流的信息,然后從AVStream結構體中讀取視頻的寬度和高度。以下是一個簡單的示例代碼: #include <libavformat/avformat.h> int main(int argc, char *argv[]) {AVForm…

ppt轉pdf的java實現

一、實現方式 java采用jacob包的功能&#xff0c;把ppt演示文稿轉換為pdf。 支持文件格式&#xff1a;pptx,ppt 二、事先準備 1、依賴于office&#xff0c;需安裝office辦公軟件 2、需要下載一個jacob-1.20-x64.dll的文件&#xff0c;放到java的bin目錄下。 文件可以網上搜…

電影網站|基于SSM+vue的電影網站系統(源碼+數據庫+文檔)

電影網站 目錄 基于SSMvue的電影網站系統 一、前言 二、系統設計 三、系統功能設計 1 系統功能模塊 2 管理員功能模塊 四、數據庫設計 五、核心代碼 六、論文參考 七、最新計算機畢設選題推薦 八、源碼獲取&#xff1a; 博主介紹&#xff1a;??大廠碼農|畢設布道…

Linux sliplogin命令教程:如何使用sliplogin命令建立SLIP服務器(附實例詳解和注意事項)

Linux sliplogin命令介紹 sliplogin&#xff08;Serial Line Internet Protocol Login&#xff09;命令用于將SLIP接口加入標準輸入&#xff0c;把一般終端機的連線變成SLIP連線。通常可用來建立SLIP服務器&#xff0c;讓遠端電腦以SLIP連線到服務器。 Linux sliplogin命令適…

貪心算法-----檸檬水找零

今日題目&#xff1a;leetcode860 題目鏈接&#xff1a;點擊跳轉題目 分析&#xff1a; 顧客只會給三種面值&#xff1a;5、10、20&#xff0c;先分類討論 當收到5美元時&#xff1a;不用找零&#xff0c;面值5張數1當收到10美元時&#xff1a;找零5美元&#xff0c;面值5張數…

未授權訪問:JBoss未授權訪問漏洞

目錄 1、漏洞原理? 2、環境搭建 3、未授權訪問 4、利用jboss.deployment getshell 防御手段 今天繼續學習各種未授權訪問的知識和相關的實操實驗&#xff0c;一共有好多篇&#xff0c;內容主要是參考先知社區的一位大佬的關于未授權訪問的好文章&#xff0c;還有其他大佬…

【Ubuntu 安裝erlang】

apt-get 安裝 apt-get install erlang或 源碼安裝 git clone https://github.com/erlang/otp.git cd otp git checkout maint-25 # current latest stable version ./configure make make install安裝完后&#xff0c;驗證是否成功 # 命令行輸入 erl

7.用戶、角色、菜單表SQL

用戶與角色是 多對多的關系&#xff1b; 角色與菜單權限 多對多的關系&#xff1b; 菜單權限表 create table acl_permission (id char(19) not null DEFAULT COMMENT 編號,pid CHAR(19) not null DEFAULT COMMENT 所屬上級,name VARCHAR(20) not NULL DEFAULT COMMENT …

C語言經典例題-7

1.計算三角形的周長和面積 題目描述&#xff1a; 根據給出的三角形3條邊a, b, c&#xff08;0 < a, b, c < 100,000&#xff09;&#xff0c;計算三角形的周長和面積。 輸入描述: 一行&#xff0c;三角形3條邊&#xff08;能構成三角形&#xff09;&#xff0c;中間用…

【ARM 嵌入式 C 入門及漸進 12.3 -- 將數值的第 s 位到 e 位清零】

請閱讀【嵌入式開發學習必備專欄】 文章目錄 將數值的第 s 位到 e 位清零 將數值的第 s 位到 e 位清零 為了定義一個VAL_CLR_BITS(val, s, n)宏&#xff0c;該宏將變量val的第s位到第n位清零&#xff08;假設n > s&#xff09;&#xff0c;其余位的值保持不變&#xff0c;我…

系統集成項目管理工程師第4章思維導圖發布

2024年開年&#xff0c;軟考系統集成項目管理工程師官方教程&#xff0c;迎來了闊別7年的大改版&#xff0c;改版之后的軟考中項考試&#xff0c;離同宗兄弟高項考試漸行漸遠。 中項第3版教程&#xff0c;僅僅從教程來看&#xff0c;其難度已經不亞于高級的信息系統項目管理師&…

數據結構與算法學習筆記三---循環隊列的表示和實現(C語言)

目錄 前言 1.為啥要使用循環隊列 2.隊列的順序表示和實現 1.定義 2.初始化 3.銷毀 4.清空 5.空隊列 6.隊列長度 7.獲取隊頭 8.入隊 9.出隊 10.遍歷隊列 11.完整代碼 前言 本篇博客介紹棧和隊列的表示和實現。 1.為啥要使用循環隊列 上篇文章中我們知道了順序隊列…

Hive Transaction事務表(含實現原理)

Hive Transaction事務表 在Hive中&#xff0c;事務表&#xff08;Transactional Tables&#xff09;允許用戶執行事務性操作&#xff0c;包括ACID&#xff08;原子性、一致性、隔離性、持久性&#xff09;特性。事務表是在Hive 0.14版本引入的&#xff0c;并且在后續版本中不斷…