当前位置: 首页 > news >正文

微软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类型的邮件&#xff0c;foxmail中配置如下图&#xff1a; 需要的maven依赖如下&#xff1a; <dependency><groupId>com.microsoft.ews-java-api</groupId><artifactId>ews-java-api</artifactId><version>2.0</ve…...

AI绘画Stable Diffusion SDXL 超赞!高质量万能大模型,写实人像、时尚设计、建筑设计、电影制作—筑梦工业XLV4.0

大家好&#xff0c;我是阿威 今天为大家带来了一款多功能大模型——Dream Tech XL | 筑梦工业XL V4.0。该模型是大佬Dr_Dream基于V3.0训练而来的迭代版本&#xff0c;在提升画面质感的同时&#xff0c;对于提示词理解能力有跨越式提升&#xff0c;可以做到100%还原提示词。筑梦…...

数字人捕捉、建模与合成

在感知系统中&#xff0c;我们与外部合作者一起创建逼真的 3D 人类&#xff0c;其行为可以像虚拟世界中的真实人类一样。这项工作在今天有许多实际应用&#xff0c;并且对于元宇宙的未来至关重要。但是&#xff0c;在感知系统中&#xff0c;我们的目标是科学的——通过重现人类…...

Yarn:下一代JavaScript包管理器的安装与实战指南

当然&#xff0c;让我们深入探讨Yarn——一个高效、可靠的JavaScript包管理器&#xff0c;它为前端开发带来了新的速度和便利。Yarn由Facebook、Google、Exponent和Tilde公司共同推出&#xff0c;旨在解决npm&#xff08;Node.js包管理器&#xff09;存在的问题&#xff0c;如依…...

JVM进程缓存 Caffeine

JVM进程缓存 Caffeine 初识Caffeine Caffeine是一个基于Java8开发的&#xff0c;提供了近乎最佳命中率的高性能的本地缓存库。 ben-manes/caffeine: A high performance caching library for Java (github.com) 实例代码 Test void testBasicOps() {// 创建缓存对象Cache&…...

c++ 线程交叉场景试验

1.需求 处理一个列表的数据&#xff0c;要求按照列表的数据处理10个数据可以使用多线程处理&#xff0c;但是针对每个线程&#xff0c;1~10的处理顺序不能变。每个数据的处理必须原子&#xff0c;即只有一个线程可以针对某个数据进行处理&#xff0c;但是10个数据是可以由10个…...

Cell:如何升华你的单细胞数据——PCF空间单细胞蛋白组联合scRNA-seq解析骨髓微环境

骨髓微环境非常复杂&#xff0c;含有不同的细胞类型&#xff0c;包括了造血、间充质、内皮、血管平滑肌和神经谱系细胞等。非造血细胞对于骨髓造血非常关键。然而&#xff0c;这些细胞在人骨髓中的异质性和空间定位在很大程度上仍未被表征。来自佩雷尔曼医学院的研究者使用scRN…...

vue强制刷新组件

在Vue中强制刷新一个组件&#xff0c;通常不是一个推荐的做法&#xff0c;因为Vue的响应式系统设计就是为了自动处理依赖的更新。要强制重新渲染组件&#xff0c;以下是几种方法&#xff1a; 使用key属性&#xff1a; 最常见的方法是改变组件的key属性。当key发生变化时&#x…...

分享5款对工作学习有帮助的效率软件

​ 今天再来推荐5个超级好用的效率软件&#xff0c;无论是对你的学习还是办公都能有所帮助&#xff0c;每个都堪称神器中的神器&#xff0c;用完后觉得不好用你找我。 1.文件复制——ClipClip ​ ClipClip是一款功能强大、操作简便的文件复制与管理软件。它改变了传统的复制粘…...

redis秒杀(PHP版本)

前提提要 今天产品端提了个需求&#xff0c;院校组要求借调我去帮忙&#xff0c;因为我以前做过商城&#xff0c;现在他们需求做一个积分商城&#xff0c;需要做一个秒杀模块&#xff0c;结果毫无意外的我被借调过去了&#xff0c;刚好可以复习一下以前的知识&#xff0c;现在介…...

图形用户界面(GUI)在AI去衣技术中的作用与重要性

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

如何阅读:一个已被证实的低投入高回报的学习方法的笔记

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

pycharm 安装“通义灵码“并测试

过程&#xff1a;“File>setting>Plugins” 提示&#xff1a; 翻译之后&#xff1a; 点击"接受"之后&#xff0c;提示一下图片&#xff0c;点击ok 安装完成&#xff1a; 安装完"通义灵码"之后&#xff0c;需要登陆&#xff0c;登陆后测试 参考…...

React 之 useMemo Hook (九)

useMemo 是 React 的一个Hook&#xff0c;它允许你“记住”一些计算值&#xff0c;只有在依赖项之一发生变化时才会重新计算这些值。这有助于避免不必要的重新计算和渲染&#xff0c;从而提高应用程序的性能。 代码栗子&#xff08;计算一个斐波那契数列的值&#xff09;&#…...

短视频矩阵系统源码saas开发--可视化剪辑、矩阵托管、多功能合一开发

短视频矩阵系统源码saas开发&#xff08;可视化剪辑、矩阵托管、智能私信聚合、线索转化、数据看板、seo关键词、子账号等多个板块开发&#xff09; 短视频矩阵系统是一种集成了多种功能的系统&#xff0c;旨在帮助用户在短视频平台上进行高效的内容创作、管理和发布。根据您提…...

百度大模型文心一言api 请求错误码 一览表

错误码说明 千帆大模型平台API包含两类&#xff0c;分别为大模型能力API和大模型平台管控API&#xff0c;具体细分如下&#xff1a; 大模型能力API 对话Chat续写Completions向量Embeddings图像Images 大模型平台管控API 模型管理Prompt工程服务管理模型精调数据管理TPM&RP…...

Unity调用智谱API(简单操作 文本实时翻译)

代码展示&#xff1a; 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流程源码分析

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

如何恢复回收站中被删除的文件?3个恢复策略,实测有用!

“刚刚一不小心把回收站清空了&#xff0c;大家有什么好用的方法可以帮我恢复回收站中删除的文件吗&#xff1f;快帮帮我吧&#xff01;” 在使用电脑的过程中&#xff0c;我们有时可能会不小心将重要的文件或文件夹删除到回收站&#xff0c;并且随后可能进一步从回收站中彻底删…...

Unity---版本控制软件

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

挑战杯推荐项目

“人工智能”创意赛 - 智能艺术创作助手&#xff1a;借助大模型技术&#xff0c;开发能根据用户输入的主题、风格等要求&#xff0c;生成绘画、音乐、文学作品等多种形式艺术创作灵感或初稿的应用&#xff0c;帮助艺术家和创意爱好者激发创意、提高创作效率。 ​ - 个性化梦境…...

零门槛NAS搭建:WinNAS如何让普通电脑秒变私有云?

一、核心优势&#xff1a;专为Windows用户设计的极简NAS WinNAS由深圳耘想存储科技开发&#xff0c;是一款收费低廉但功能全面的Windows NAS工具&#xff0c;主打“无学习成本部署” 。与其他NAS软件相比&#xff0c;其优势在于&#xff1a; 无需硬件改造&#xff1a;将任意W…...

VB.net复制Ntag213卡写入UID

本示例使用的发卡器&#xff1a;https://item.taobao.com/item.htm?ftt&id615391857885 一、读取旧Ntag卡的UID和数据 Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click轻松读卡技术支持:网站:Dim i, j As IntegerDim cardidhex, …...

vue3 字体颜色设置的多种方式

在Vue 3中设置字体颜色可以通过多种方式实现&#xff0c;这取决于你是想在组件内部直接设置&#xff0c;还是在CSS/SCSS/LESS等样式文件中定义。以下是几种常见的方法&#xff1a; 1. 内联样式 你可以直接在模板中使用style绑定来设置字体颜色。 <template><div :s…...

探索Selenium:自动化测试的神奇钥匙

目录 一、Selenium 是什么1.1 定义与概念1.2 发展历程1.3 功能概述 二、Selenium 工作原理剖析2.1 架构组成2.2 工作流程2.3 通信机制 三、Selenium 的优势3.1 跨浏览器与平台支持3.2 丰富的语言支持3.3 强大的社区支持 四、Selenium 的应用场景4.1 Web 应用自动化测试4.2 数据…...

命令行关闭Windows防火墙

命令行关闭Windows防火墙 引言一、防火墙:被低估的"智能安检员"二、优先尝试!90%问题无需关闭防火墙方案1:程序白名单(解决软件误拦截)方案2:开放特定端口(解决网游/开发端口不通)三、命令行极速关闭方案方法一:PowerShell(推荐Win10/11)​方法二:CMD命令…...

从零手写Java版本的LSM Tree (一):LSM Tree 概述

&#x1f525; 推荐一个高质量的Java LSM Tree开源项目&#xff01; https://github.com/brianxiadong/java-lsm-tree java-lsm-tree 是一个从零实现的Log-Structured Merge Tree&#xff0c;专为高并发写入场景设计。 核心亮点&#xff1a; ⚡ 极致性能&#xff1a;写入速度超…...

表单设计器拖拽对象时添加属性

背景&#xff1a;因为项目需要。自写设计器。遇到的坑在此记录 使用的拖拽组件时vuedraggable。下面放上局部示例截图。 坑1。draggable标签在拖拽时可以获取到被拖拽的对象属性定义 要使用 :clone, 而不是clone。我想应该是因为draggable标签比较特。另外在使用**:clone时要将…...

智警杯备赛--excel模块

数据透视与图表制作 创建步骤 创建 1.在Excel的插入或者数据标签页下找到数据透视表的按钮 2.将数据放进“请选择单元格区域“中&#xff0c;点击确定 这是最终结果&#xff0c;但是由于环境启不了&#xff0c;这里用的是自己的excel&#xff0c;真实的环境中的excel根据实训…...

宠物车载安全座椅市场报告:解读行业趋势与投资前景

一、什么是宠物车载安全座椅&#xff1f; 宠物车载安全座椅是一种专为宠物设计的车内固定装置&#xff0c;旨在保障宠物在乘车过程中的安全性与舒适性。它通常由高强度材料制成&#xff0c;具备良好的缓冲性能&#xff0c;并可通过安全带或ISOFIX接口固定于车内。 近年来&…...