千家信息网

Java怎么通过exchange协议发送邮件

发表于:2025-01-30 作者:千家信息网编辑
千家信息网最后更新 2025年01月30日,这篇文章主要讲解了"Java怎么通过exchange协议发送邮件",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Java怎么通过exchange协议发送
千家信息网最后更新 2025年01月30日Java怎么通过exchange协议发送邮件

这篇文章主要讲解了"Java怎么通过exchange协议发送邮件",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Java怎么通过exchange协议发送邮件"吧!

pom.xml 导入包

com.microsoft.ews-java-api ews-java-api 2.0

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;   /** * 

exchange邮件处理类

* * @author eric * @date 2020/2/6 11:08 AM * @since */@Componentpublic 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; } }

感谢各位的阅读,以上就是"Java怎么通过exchange协议发送邮件"的内容了,经过本文的学习后,相信大家对Java怎么通过exchange协议发送邮件这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!

0