微软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);}
相关文章:

微软exchange邮箱发送
使用java发送exchange类型的邮件,foxmail中配置如下图: 需要的maven依赖如下: <dependency><groupId>com.microsoft.ews-java-api</groupId><artifactId>ews-java-api</artifactId><version>2.0</ve…...

AI绘画Stable Diffusion SDXL 超赞!高质量万能大模型,写实人像、时尚设计、建筑设计、电影制作—筑梦工业XLV4.0
大家好,我是阿威 今天为大家带来了一款多功能大模型——Dream Tech XL | 筑梦工业XL V4.0。该模型是大佬Dr_Dream基于V3.0训练而来的迭代版本,在提升画面质感的同时,对于提示词理解能力有跨越式提升,可以做到100%还原提示词。筑梦…...

数字人捕捉、建模与合成
在感知系统中,我们与外部合作者一起创建逼真的 3D 人类,其行为可以像虚拟世界中的真实人类一样。这项工作在今天有许多实际应用,并且对于元宇宙的未来至关重要。但是,在感知系统中,我们的目标是科学的——通过重现人类…...
Yarn:下一代JavaScript包管理器的安装与实战指南
当然,让我们深入探讨Yarn——一个高效、可靠的JavaScript包管理器,它为前端开发带来了新的速度和便利。Yarn由Facebook、Google、Exponent和Tilde公司共同推出,旨在解决npm(Node.js包管理器)存在的问题,如依…...
JVM进程缓存 Caffeine
JVM进程缓存 Caffeine 初识Caffeine Caffeine是一个基于Java8开发的,提供了近乎最佳命中率的高性能的本地缓存库。 ben-manes/caffeine: A high performance caching library for Java (github.com) 实例代码 Test void testBasicOps() {// 创建缓存对象Cache&…...

c++ 线程交叉场景试验
1.需求 处理一个列表的数据,要求按照列表的数据处理10个数据可以使用多线程处理,但是针对每个线程,1~10的处理顺序不能变。每个数据的处理必须原子,即只有一个线程可以针对某个数据进行处理,但是10个数据是可以由10个…...

Cell:如何升华你的单细胞数据——PCF空间单细胞蛋白组联合scRNA-seq解析骨髓微环境
骨髓微环境非常复杂,含有不同的细胞类型,包括了造血、间充质、内皮、血管平滑肌和神经谱系细胞等。非造血细胞对于骨髓造血非常关键。然而,这些细胞在人骨髓中的异质性和空间定位在很大程度上仍未被表征。来自佩雷尔曼医学院的研究者使用scRN…...
vue强制刷新组件
在Vue中强制刷新一个组件,通常不是一个推荐的做法,因为Vue的响应式系统设计就是为了自动处理依赖的更新。要强制重新渲染组件,以下是几种方法: 使用key属性: 最常见的方法是改变组件的key属性。当key发生变化时&#x…...

分享5款对工作学习有帮助的效率软件
今天再来推荐5个超级好用的效率软件,无论是对你的学习还是办公都能有所帮助,每个都堪称神器中的神器,用完后觉得不好用你找我。 1.文件复制——ClipClip ClipClip是一款功能强大、操作简便的文件复制与管理软件。它改变了传统的复制粘…...
redis秒杀(PHP版本)
前提提要 今天产品端提了个需求,院校组要求借调我去帮忙,因为我以前做过商城,现在他们需求做一个积分商城,需要做一个秒杀模块,结果毫无意外的我被借调过去了,刚好可以复习一下以前的知识,现在介…...

图形用户界面(GUI)在AI去衣技术中的作用与重要性
引言: 随着人工智能技术的不断进步,AI去衣这一概念逐渐进入公众视野。它指的是利用深度学习算法,从图片或视频中自动移除人物的衣物,生成相应的“裸体”图像。尽管这项技术在道德和隐私方面引发了诸多争议,但其背后的技…...

如何阅读:一个已被证实的低投入高回报的学习方法的笔记
系列文章目录 如何有效阅读一本书笔记 如何阅读:一个已被证实的低投入高回报的学习方法 麦肯锡精英高效阅读法笔记 读懂一本书笔记 文章目录 系列文章目录第一章 扫清阅读障碍破解读不快、读不进去的谜题一切为了阅读小学教师让你做,但中学老师阻止你做的…...

pycharm 安装“通义灵码“并测试
过程:“File>setting>Plugins” 提示: 翻译之后: 点击"接受"之后,提示一下图片,点击ok 安装完成: 安装完"通义灵码"之后,需要登陆,登陆后测试 参考…...
React 之 useMemo Hook (九)
useMemo 是 React 的一个Hook,它允许你“记住”一些计算值,只有在依赖项之一发生变化时才会重新计算这些值。这有助于避免不必要的重新计算和渲染,从而提高应用程序的性能。 代码栗子(计算一个斐波那契数列的值)&#…...

短视频矩阵系统源码saas开发--可视化剪辑、矩阵托管、多功能合一开发
短视频矩阵系统源码saas开发(可视化剪辑、矩阵托管、智能私信聚合、线索转化、数据看板、seo关键词、子账号等多个板块开发) 短视频矩阵系统是一种集成了多种功能的系统,旨在帮助用户在短视频平台上进行高效的内容创作、管理和发布。根据您提…...
百度大模型文心一言api 请求错误码 一览表
错误码说明 千帆大模型平台API包含两类,分别为大模型能力API和大模型平台管控API,具体细分如下: 大模型能力API 对话Chat续写Completions向量Embeddings图像Images 大模型平台管控API 模型管理Prompt工程服务管理模型精调数据管理TPM&RP…...
Unity调用智谱API(简单操作 文本实时翻译)
代码展示: using Newtonsoft.Json; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI;public class ZhiPuAi : MonoBehaviour {// API的端点URLpublic string…...

Android 开机启动扫描SD卡apk流程源码分析
在开机的时候,装在SD卡的apk和装在系统盘的apk扫描过程不一样,系统盘apk在系统启动过程中扫描,而SD卡上的就不是,等系统启动好了才挂载、扫描,下面就说下SD扫描的流程: 在SystemServer启动MountService&am…...

如何恢复回收站中被删除的文件?3个恢复策略,实测有用!
“刚刚一不小心把回收站清空了,大家有什么好用的方法可以帮我恢复回收站中删除的文件吗?快帮帮我吧!” 在使用电脑的过程中,我们有时可能会不小心将重要的文件或文件夹删除到回收站,并且随后可能进一步从回收站中彻底删…...

Unity---版本控制软件
13.3 版本控制——Git-1_哔哩哔哩_bilibili Git用的比较多 Git 常用Linux命令 pwd:显示当前所在路径 ls:显示当前路径下的所有文件 tab键自动补全 cd:切换路径 mkdir:在当前路径下创建一个文件夹 clear:清屏 vim…...

利用最小二乘法找圆心和半径
#include <iostream> #include <vector> #include <cmath> #include <Eigen/Dense> // 需安装Eigen库用于矩阵运算 // 定义点结构 struct Point { double x, y; Point(double x_, double y_) : x(x_), y(y_) {} }; // 最小二乘法求圆心和半径 …...

汽车生产虚拟实训中的技能提升与生产优化
在制造业蓬勃发展的大背景下,虚拟教学实训宛如一颗璀璨的新星,正发挥着不可或缺且日益凸显的关键作用,源源不断地为企业的稳健前行与创新发展注入磅礴强大的动力。就以汽车制造企业这一极具代表性的行业主体为例,汽车生产线上各类…...

【Zephyr 系列 10】实战项目:打造一个蓝牙传感器终端 + 网关系统(完整架构与全栈实现)
🧠关键词:Zephyr、BLE、终端、网关、广播、连接、传感器、数据采集、低功耗、系统集成 📌目标读者:希望基于 Zephyr 构建 BLE 系统架构、实现终端与网关协作、具备产品交付能力的开发者 📊篇幅字数:约 5200 字 ✨ 项目总览 在物联网实际项目中,**“终端 + 网关”**是…...

多模态大语言模型arxiv论文略读(108)
CROME: Cross-Modal Adapters for Efficient Multimodal LLM ➡️ 论文标题:CROME: Cross-Modal Adapters for Efficient Multimodal LLM ➡️ 论文作者:Sayna Ebrahimi, Sercan O. Arik, Tejas Nama, Tomas Pfister ➡️ 研究机构: Google Cloud AI Re…...
Android Bitmap治理全解析:从加载优化到泄漏防控的全生命周期管理
引言 Bitmap(位图)是Android应用内存占用的“头号杀手”。一张1080P(1920x1080)的图片以ARGB_8888格式加载时,内存占用高达8MB(192010804字节)。据统计,超过60%的应用OOM崩溃与Bitm…...

什么是Ansible Jinja2
理解 Ansible Jinja2 模板 Ansible 是一款功能强大的开源自动化工具,可让您无缝地管理和配置系统。Ansible 的一大亮点是它使用 Jinja2 模板,允许您根据变量数据动态生成文件、配置设置和脚本。本文将向您介绍 Ansible 中的 Jinja2 模板,并通…...

算法笔记2
1.字符串拼接最好用StringBuilder,不用String 2.创建List<>类型的数组并创建内存 List arr[] new ArrayList[26]; Arrays.setAll(arr, i -> new ArrayList<>()); 3.去掉首尾空格...

基于SpringBoot在线拍卖系统的设计和实现
摘 要 随着社会的发展,社会的各行各业都在利用信息化时代的优势。计算机的优势和普及使得各种信息系统的开发成为必需。 在线拍卖系统,主要的模块包括管理员;首页、个人中心、用户管理、商品类型管理、拍卖商品管理、历史竞拍管理、竞拍订单…...

RabbitMQ入门4.1.0版本(基于java、SpringBoot操作)
RabbitMQ 一、RabbitMQ概述 RabbitMQ RabbitMQ最初由LShift和CohesiveFT于2007年开发,后来由Pivotal Software Inc.(现为VMware子公司)接管。RabbitMQ 是一个开源的消息代理和队列服务器,用 Erlang 语言编写。广泛应用于各种分布…...

FFmpeg:Windows系统小白安装及其使用
一、安装 1.访问官网 Download FFmpeg 2.点击版本目录 3.选择版本点击安装 注意这里选择的是【release buids】,注意左上角标题 例如我安装在目录 F:\FFmpeg 4.解压 5.添加环境变量 把你解压后的bin目录(即exe所在文件夹)加入系统变量…...