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
代码
packagecom.youjia.found.manager; importcom.youjia.found.common.util.Check; importmicrosoft.exchange.webservices.data.core.ExchangeService; importmicrosoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion; importmicrosoft.exchange.webservices.data.core.enumeration.property.BodyType; importmicrosoft.exchange.webservices.data.core.service.item.EmailMessage; importmicrosoft.exchange.webservices.data.credential.ExchangeCredentials; importmicrosoft.exchange.webservices.data.credential.WebCredentials; importmicrosoft.exchange.webservices.data.property.complex.MessageBody; importorg.slf4j.Logger; importorg.slf4j.LoggerFactory; importorg.springframework.beans.factory.annotation.Value; importorg.springframework.stereotype.Component; importjavax.mail.internet.InternetAddress; importjava.net.URI; /** *exchange邮件处理类
* *@authoreric *@date2020/2/611:08AM *@since */ @Component publicclassMailExchangeManager{ privatefinalLoggerlogger=LoggerFactory.getLogger(this.getClass()); @Value("${youjia.exchange.mail.username}") privateStringusername; @Value("${youjia.exchange.mail.password}") privateStringpassword; @Value("${youjia.exchange.mail.host}") privateStringhost; /** *使用Exchange协议发送 *@paramto收件人 *@paramsubject邮件主题 *@paramcontent正文 *@paramfilePath附件 * *@throwsException */ publicbooleansendMail(Stringto,Stringsubject,Stringcontent,StringfilePath){ logger.info("exchange邮件发送to:{},subject:{},content:{},filePath:{}",to,subject,content,filePath); booleanisOK=false; ExchangeServiceservice=newExchangeService(ExchangeVersion.Exchange2007_SP1); ExchangeCredentialscredentials=newWebCredentials(username,password); service.setCredentials(credentials); try{ service.setUrl(newURI(host)); EmailMessagemsg=newEmailMessage(service); msg.setSubject(subject); MessageBodybody=MessageBody.getMessageBodyFromText(content); body.setBodyType(BodyType.HTML); msg.setBody(body); //支持多个收件人 InternetAddress[]addresses=InternetAddress.parse(to); for(InternetAddressaddress:addresses){ msg.getToRecipients().add(address.getAddress()); } if(Check.notEmpty(filePath)){ msg.getAttachments().addFileAttachment(filePath); } msg.send(); isOK=true; }catch(Exceptione){ logger.error(e.getMessage(),e); isOK=false; } returnisOK; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。