微软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://xiahunao.cn/news/3019015.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

报表控件Stimulsoft在JavaScript报告工具中的事件:查看器事件(下)

Stimulsoft Ultimate &#xff08;原Stimulsoft Reports.Ultimate&#xff09;是用于创建报表和仪表板的通用工具集。该产品包括用于WinForms、ASP.NET、.NET Core、JavaScript、WPF、PHP、Java和其他环境的完整工具集。无需比较产品功能&#xff0c;Stimulsoft Ultimate包含了…

解决ModuleNotFoundError: No module named ‘skfuzzy‘,这个库全名可不叫skfuzzy哦,否则直接报错!!

ModuleNotFoundError: No module named skfuzzy 在这里插入图片描述在这里插入图片描述如何解决 ModuleNotFoundError: No module named skfuzzy 的问题&#xff1f;skfuzzy 模块介绍什么是模糊C均值聚类&#xff1f;skfuzzy 的应用如何使用 skfuzzy 进行模糊聚类 结论 如何解决…

想要品质飞跃?找六西格玛培训公司就对了!

在当今复杂多变的市场环境中&#xff0c;企业的竞争早已不再是单一的价格或产品竞争&#xff0c;而是转向了对品质、效率和创新的全面追求。六西格玛&#xff0c;作为一种全球公认的质量管理方法论&#xff0c;正成为越来越多企业追求品质革命的重要工具。在这其中&#xff0c;…

不盖CNAS的证书就是无效的?证书哪些信息是“非必要”?

做设备校准的企业&#xff0c;大多数都是为了拿到仪器校准证书&#xff0c;而说起校准证书&#xff0c;很多人优先就是想到CNAS&#xff0c;CNAS作为校准行业重要的核心资质&#xff0c;无论是校准机构实力的证明&#xff0c;还是满足企业年审的需要&#xff0c;基本上都是关键…

谷歌推出10门免费AI课程,无需教科书及费用

谷歌面向小白以及开发者分别推出了不同的AI课程~ 包含初级、中级和高级。课程章节大致包括&#xff1a;&#xff08;含教学视频、参考材料、测验&#xff09; 基础入门&#xff1a;45分钟深入了解生成式AI 简单实操&#xff1a;30分钟掌握大语言模型 了解如何释放生成式 AI S…

武汉星起航:跨境电商平台拓展全球市场,打造国际品牌的更优选择

随着全球化的加速和互联网的普及&#xff0c;跨境电商平台与国内电商平台成为了现代商业领域的两大重要支柱。它们在商业模式、运营策略、市场覆盖等方面均呈现出显著的区别&#xff0c;为商家提供了多样化的销售渠道和市场拓展机会。武汉星起航旨在深入探讨跨境电商平台与国内…

零门槛副业兼职!10种长期赚钱好方法!

想要实现财务自由&#xff0c;不能仅停留在梦想层面&#xff0c;更需要付诸实践。 以下是我从网络上精心整理的十大可靠的兼职副业建议&#xff0c;旨在助你一臂之力。 这些项目已根据推荐程度、难度水平、目标人群以及预期收入进行了细致分类。 我要强调的是&#xff0c;任…

神秘的Python-docx,自动化你的Word文档处理

在Python里&#xff0c;有一个非常实用的库叫做python-docx。它允许我们像操作文本文件一样&#xff0c;轻松地创建和修改Word文档。如果你经常需要处理Word文件&#xff0c;比如生成报告或自动填充数据&#xff0c;学习如何使用python-docx库将大大提升你的工作效率。 python…

苹果删除的短信怎么恢复?这里有4个恢复技巧

手机短信已成为我们日常沟通中不可或缺的一部分&#xff0c;其中包含了与家人、朋友的温馨对话&#xff0c;以及与工作伙伴的重要信息。然而&#xff0c;有时我们可能会因为误操作或其他原因不小心删除了重要的短信。请别担心&#xff0c;本文将为您详细介绍删除的短信怎么恢复…

平滑 3d 坐标

3d平滑 import torch import torch.nn.functional as F import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3Dclass SmoothOperator:def smooth(self, vertices):# 使用一维平均池化进行平滑vertices_smooth F.avg_pool1d(vertices.p…

自动控制原理MATLAB:系统分析

控制系统时域分析 &#xff08;1&#xff09;稳定性分析 稳定是控制系统的重要性能&#xff0c;也是系统设计过程中的首要问题。线性系统稳定的充分必要条件是&#xff1a;闭环系统特征方程的所有根均具有负实部。在MATLAB中可以调用roots命令求取特征根&#xff0c;进而判别…

C# 和 Qt 相比的一些优势

C# 和 Qt 都是流行的软件开发工具&#xff0c;它们各自具有不同的优势&#xff0c;适用于不同的开发场景。以下是 C# 和 Qt 相比的一些优势。相比之下&#xff0c;Qt 也有其独特的优势&#xff0c;特别是在跨平台 GUI 应用程序开发方面。然而&#xff0c;C# 的这些优势使得它在…

【编码利器 —— BaiduComate】

目录 1. 智能编码助手介绍 2. 场景需求 3. 功能体验 3.1指令功能 3.2插件用法 3.3知识用法 3.4自定义配置 4. 试用感受 5. AI编程应用 6.总结 智能编码助手是当下人工智能技术在编程领域的一项重要应用。Baidu Comate智能编码助手作为一款具有强大功能和智能特性的工…

APScheduler定时器使用【简易版】:django中使用apscheduler,使用mysql做存储后端

一、基本环境 python版本&#xff1a;3.8.5 APScheduler3.10.4 Django3.2.7 djangorestframework3.15.1 SQLAlchemy2.0.29 PyMySQL1.1.0二、django基本设置 2.1、新增一个app 该app用来写apscheduler相关的代码 python manage.py startapp gs_scheduler 2.2、修改配置文件s…

20232810 2023-2024-2 《网络攻防实践》实验八

一、实践内容 1.1 恶意代码 1.1.1 简介 定义&#xff1a;恶意代码&#xff08;Malware,或Malicious Code&#xff09;指的是使计算机按照攻击者的意图执行以达到恶意目标的指令集。 指令集合&#xff1a;二进制执行文件、脚本语言代码、宏代码、寄生在文件或者启动扇区的指令…

想要买到心仪的旋转式孔板流量计吗?

选择旋转式孔板流量计可不能云里雾里的乱选择呀&#xff0c;煤矿对产品质量要求很严格的。所以我们要先了解产品的再决定才是对的选择。 旋转式孔板流量计技术参数【1--5--9】 规格&#xff1a;DN15&#xff5e;DN1000 孔径比(βd/D)&#xff1a;β0&#xff0e;2—0&#xff…

深圳车间厂房降温用什么设备好?

环保水空调&#xff08;也被称为水冷空调或蒸发式降温换气机组&#xff09;的特点主要体现在以下几个方面&#xff1a; 节能环保&#xff1a;环保水空调使用水作为冷媒介&#xff0c;相比传统空调的制冷方式&#xff0c;它能在制冷过程中节约更多的能源&#xff0c;减少碳排放…

常用的外贸软件有哪些

常用的外贸软件涵盖了多个方面&#xff0c;包括客户开发、订单管理、库存控制、客户关系管理(CRM)、财务管理以及跨境电商平台等。以下是一些代表性的外贸软件和平台&#xff1a; 客户开发与营销软件: 大镜山谷歌搜索大师易谷歌地图数据采集大师米贸搜 外贸管理软件 (ERP): 神卓…

美国硅谷裸机云大宽带服务器在哪些行业中应用最广泛?

美国硅谷裸机云大宽带服务器在视频流媒体、实时数据分析和金融交易等行业中应用最广泛。关于美国硅谷裸机云大带宽服务器的各行业应用&#xff0c;rak部落小编为您做出详细的阐述。 美国硅谷裸机云大宽带服务器因其结合了高性能物理服务器和大带宽网络连接的特点&#xff0c;成…

H7-TOOL的双硬件串口同时运行Modbus主机和从机方法,方便大家Modbus测试验证(2024-05-06)

H7-TOOL的双硬件串口同时运行Modbus主机和从机方法&#xff0c;方便大家Modbus测试验证&#xff08;2024-05-06&#xff09; 使用这种方法&#xff0c;仅使用一个TOOL就可以方便同时运行Modbus主机和从机。 【Modbus专题视频】 可以用来熟悉Modbus协议 BSP视频教程第23期…