微软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.mzph.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…

小红书·电商运营课:小红书开店流程,小红书电商如何运营(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…

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;✌️大厂码农|毕设布道…

贪心算法-----柠檬水找零

今日题目&#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

系统集成项目管理工程师第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;并且在后续版本中不断…

LabVIEW天然气压缩因子软件设计

LabVIEW天然气压缩因子软件设计 项目背景 天然气作为一种重要的能源&#xff0c;其压缩因子的准确计算对于流量的计量和输送过程的优化具有关键意义。传统的计算方法不仅步骤繁琐&#xff0c;而且难以满足现场快速响应的需求。因此&#xff0c;开发一款既能保证计算精度又便于…

使用Pandas对Data列进行基于顺序的分组排列

目录 一、引言 二、Pandas库简介 三、按照数据列中元素出现的先后顺序进行分组排列 四、案例分析 五、技术细节探讨与扩展应用 1. 技术细节 2. 扩展应用 3. 示例代码&#xff1a;用户行为分析 4. 进阶应用&#xff1a;分组后的聚合操作 5. 分组后的数据筛选 6. 分组…

【DevOps】Linux 安全:iptables 组成、命令及应用场景详解

导读&#xff1a;全面掌握 iptables&#xff1a;从基础到实践 在 Linux 系统中&#xff0c;iptables 是一个非常强大的工具&#xff0c;它不仅是系统管理员用来构建和管理网络防火墙的首选工具&#xff0c;而且也是一个功能丰富的网络流量处理系统。无论是进行包过滤、监控网络…

学习笔记:【QC】Android Q qmi扩展nvReadItem/nvWriteItem

一、qmi初始化 流程图 初始化流程: 1、主入口&#xff1a; vendor/qcom/proprietary/qcril-hal/qcrild/qcrild/rild.c int main(int argc, char **argv) { const RIL_RadioFunctions *(*rilInit)(const struct RIL_Env *, int, char **); rilInit RIL_Init; funcs rilInit…

李彦宏回顾大模型重构百度这一年

“大模型我们走在最前面&#xff0c;我们需要去勇闯无人区&#xff0c;需要去冒前人没有冒过的风险。”近日&#xff0c;在百度一场内部颁奖活动中&#xff0c;百度创始人、董事长兼首席执行官李彦宏指出&#xff0c;百度一直坚信技术可以改变世界&#xff0c;会一直沿着这条路…

docker的centos容器使用yum报错

错误描述 学习docker过程中&#xff0c;基于 centos 镜像自定义新的镜像。拉取一个Centos镜像&#xff0c;并运行容器&#xff0c;容器安装vim&#xff0c;报错了。 报错&#xff1a;Error: Failed to download metadata for repo appstream: Cannot prepare internal mirror…