yml文件
mail: default-encoding: UTF-8 host: smtp.xx.com//邮箱服务器 username: //用户名 password: //密码 port: 465//邮件端口号 properties: mail: smtp: starttls: enable: false required: false auth: true ssl: enable: true protocol: smtp test-connection: false
//读取配置文件 import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import lombok.Data; @Data @ConfigurationProperties(prefix = "youle.platform") public class SysProperties { @Value("${spring.mail.username}") private String emailFrom; }
//注入到
import org.springframework.beans.BeanUtils; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.cloudjet.coupon.sdk.CouponClient; import com.cloudjet.emsoauth.core.EmsOAuthClient; import com.cloudjet.point.sdk.PointClient; @Configuration public class CustomConfiguration { @Bean public YouLeConfig youLeConfig(SysProperties prop) { YouLeConfig youLeConfig = new YouLeConfig(); BeanUtils.copyProperties(prop, youLeConfig); return youLeConfig; } }
//调接口
public interface EmailService { /** * 发送thymeleaf模板邮件 * @param mail * @throws MessagingException * @throws IOException */ public void sendTemplateMail(Mail mail); }
//邮件实现
package com.ziku.ms.youle.service.sms.impl; import java.io.IOException; import java.nio.charset.StandardCharsets; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import org.apache.commons.lang.exception.ExceptionUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import org.thymeleaf.context.Context; import org.thymeleaf.spring4.SpringTemplateEngine; import lombok.extern.slf4j.Slf4j; @Slf4j @Service public class EmailServiceImpl implements EmailService { @Autowired private JavaMailSender emailSender; @Autowired private SpringTemplateEngine templateEngine; /** * <p> * 发送thymeleaf模板邮件 * </p> * * @param mail * @throws MessagingException * @throws IOException */ @Async @Override public void sendTemplateMail(Mail mail) { try { log.info("发送邮件To邮箱:{},From:{}",mail.getTo(),mail.getFrom()); MimeMessage message = emailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED, StandardCharsets.UTF_8.name()); Context context = new Context(); context.setVariables(mail.getModel()); String html = templateEngine.process("email-template", context); helper.setTo(mail.getTo()); helper.setText(html, true); helper.setSubject(mail.getSubject()); helper.setFrom(mail.getFrom()); emailSender.send(message); } catch (MessagingException e) { log.error("发送邮件失败,收件人邮箱:{},异常信息:{}",mail.getTo(),ExceptionUtils.getFullStackTrace(e)); } } }
实现调用
Mail mail = new Mail();
mail.setFrom(youLeConfig.getEmailFrom()); mail.setTo(youleMebExtends.getCompanyEmail()); mail.setSubject("激活邮件"); Map<String,Object> map =new HashMap<>(); map.put("name", youleMebExtends.getCompanyName()); map.put("activeUrl", activeUrl + "/" + getVersionNO); mail.setModel(map); log.info("MemberEmailServiceImpl>>>getCompanyByEmail>>>>邮件参数mail{}",mail); emailService.sendTemplateMail(mail); log.info("激活邮件已发送,url:{}",activeUrl + "/" + getVersionNO);
//发送模板
<!DOCTYPE html> <html lang="en" style="width: 100%; height: auto;"> <head> <meta charset="UTF-8"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <meta http-equiv="X-UA-Compatible" content="ie=edge"/> <title>邮件</title> <!-- <link rel="stylesheet" href="../static/css/index.css"/> <link rel="stylesheet" href="../static/iconfont/iconfont.css"/> --> </head> <body style="width: 100%; height: auto;padding: 0 20px 20px 20px;box-sizing:border-box;"> <div style="width: 100%; height: 500px; background: #fff; background: url(http://img.izhuan100.com/youle/uat/images/1524448743955-.jpg) no-repeat 72px 272px; background-size: 184px 234px; position: relative;"> <div style="width: 100%; height: 46px; border-bottom: 1px solid #ccc;box-sizing: border-box;"> <img src="http://img.izhuan100.com/youle/uat/images/1524448587831-.png" alt="" style="width: 90px; height: 31px; float: left;"/> <span style="font-size: 24px; float: right; color:#666;">激活邮件</span> </div> <!-- 通过添加render类样式来控制页面三种内容的隐藏 --> <!-- 第一种 --> <div class="content"> <h3 style="font-size: 18px;">尊敬的 <span style="color: #95673D;display:inline-block;"><p th:text="${'Dear ' + name}"></p></span>的员工,你好!</h3> <p style=" font-size: 12px; color: #999999; margin-top: 15px;">点击一下链接,立即激活。</p> <a th:href="${activeUrl}">点此立即激活</a> </div> <div style="width: 100%; position: absolute; bottom: 20px;"> <h4 style="font-size: 14px; color: #3F3F3F; line-height: 18px; display: inline-block; width: 54%; margin-bottom: 20px;"> <span style="color: #666;"></span>,致力于为全球家庭客户智造极致的消费体验,实现家庭积分打通、家庭权益整合和家庭消费定制。</h4> <div style=" width: 130px; position: absolute; right: 0px; bottom: 20px;"> <img src="http://img.izhuan100.com/youle/uat/images/1524448674026-.jpg" alt="" style=" width: 130px; height: 130px; margin: 0 auto;"/> <p style="text-align: center; font-size: 12px;">请关公众号</p> </div> <div style="position: absolute; bottom: 0px; width: 100%; height: 10px; background: #94673D;"></div> </div> </div> </body> </html>