您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
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; } }
关于Java中怎么通过exchange协议发送邮件问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。