Java通过exchange协议发送邮件
作者:tomato先生 时间:2022-10-05 01:46:10
本文实例为大家分享了Java通过exchange协议发送邮件的具体代码,供大家参考,具体内容如下
pom.xml 导入包
<dependency>
<groupId>com.microsoft.ews-java-api</groupId>
<artifactId>ews-java-api</artifactId>
<version>2.0</version>
</dependency>
application.properties 配置信息
#邮箱地址
youjia.exchange.mail.username=123@abc.com
#邮箱密码
youjia.exchange.mail.password=123456
#邮箱exchange服务地址,如果不知道找运维
youjia.exchange.mail.host=https://*****/ews/exchange.asmx
代码
package com.youjia.found.manager;
import com.youjia.found.common.util.Check;
import microsoft.exchange.webservices.data.core.ExchangeService;
import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
import microsoft.exchange.webservices.data.core.enumeration.property.BodyType;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.mail.internet.InternetAddress;
import java.net.URI;
/**
* <P>exchange邮件处理类</P>
*
* @author eric
* @date 2020/2/6 11:08 AM
* @since
*/
@Component
public class MailExchangeManager {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Value("${youjia.exchange.mail.username}")
private String username ;
@Value("${youjia.exchange.mail.password}")
private String password;
@Value("${youjia.exchange.mail.host}")
private String host ;
/**
* 使用Exchange协议发送
* @param to 收件人
* @param subject 邮件主题
* @param content 正文
* @param filePath 附件
*
* @throws Exception
*/
public boolean sendMail(String to, String subject, String content, String filePath) {
logger.info("exchange邮件发送 to:{}, subject:{}, content:{},filePath:{}", to, subject, content,filePath);
boolean isOK=false;
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
ExchangeCredentials credentials = new WebCredentials(username,password);
service.setCredentials(credentials);
try {
service.setUrl(new URI(host));
EmailMessage msg = new EmailMessage(service);
msg.setSubject(subject);
MessageBody body = MessageBody.getMessageBodyFromText(content);
body.setBodyType(BodyType.HTML);
msg.setBody(body);
//支持多个收件人
InternetAddress[] addresses = InternetAddress.parse(to);
for (InternetAddress address : addresses) {
msg.getToRecipients().add(address.getAddress());
}
if (Check.notEmpty(filePath)) {
msg.getAttachments().addFileAttachment(filePath);
}
msg.send();
isOK=true;
} catch (Exception e) {
logger.error(e.getMessage(),e);
isOK= false;
}
return isOK;
}
}
来源:https://blog.csdn.net/tomatoxman/article/details/104249121?utm_source=distribute.pc_feed.none-task
标签:java,exchange,发送邮件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
高效C#编码优化原则
2023-06-13 03:16:23
Java实现多线程断点下载
2022-04-27 00:29:16
![](https://img.aspxhome.com/file/2023/9/76319_0s.png)
Android沉浸式状态栏 + actionBar渐变 + scrollView顶部伸缩效果
2021-09-12 01:04:16
![](https://img.aspxhome.com/file/2023/2/125402_0s.gif)
c#调用vc写的ocx控件示例
2021-09-26 19:34:00
SpringBoot四种读取properties文件的方式(小结)
2023-10-10 03:25:14
flutter Container容器实现圆角边框
2021-06-17 23:16:23
![](https://img.aspxhome.com/file/2023/1/110731_0s.gif)
详解android是如何管理内存的
2022-02-08 03:37:22
![](https://img.aspxhome.com/file/2023/8/87308_0s.png)
Spring Boot JPA中使用@Entity和@Table的实现
2023-11-22 11:49:58
C# Socket连接请求超时机制实现代码分享
2021-10-26 06:52:05
深入了解Java File对象的使用
2022-10-12 00:54:11
![](https://img.aspxhome.com/file/2023/6/80926_0s.png)
Spring Boot中的那些条件判断的实现方法
2023-04-26 15:02:07
c# 剔除sql语句'尾巴'的五种方法
2022-09-20 16:32:39
![](https://img.aspxhome.com/file/2023/8/125998_0s.png)
Android实现屏幕锁定源码详解
2022-10-08 00:46:38
![](https://img.aspxhome.com/file/2023/1/123251_0s.png)
举例详解Java中的访问权限修饰符
2023-01-18 08:52:17
基于Java实现收发电子邮件功能
2021-08-23 17:30:14
![](https://img.aspxhome.com/file/2023/7/80307_0s.png)
Java利用POI读取、写入Excel的方法指南
2023-11-23 15:39:23
![](https://img.aspxhome.com/file/2023/9/59899_0s.png)
Android Studio使用Kotlin时,修改代码后运行不生效的解决方法
2022-08-05 11:29:04
![](https://img.aspxhome.com/file/2023/0/137760_0s.jpg)
Spring Security OAuth过期的解决方法
2023-05-26 22:30:01
![](https://img.aspxhome.com/file/2023/0/69330_0s.png)
SpringBoot使用Log4j过程详解
2023-05-03 22:44:12
java中transient关键字用法分析
2022-01-22 04:27:05