栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java通过exchange协议发送邮件

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;
 
 
 

@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 ;
 
 
 
 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;
 }
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/135206.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号