本文共 3911 字,大约阅读时间需要 13 分钟。
compile group: 'org.springframework.boot', name: 'spring-boot-starter-mail'
spring: # 发邮件 mail: host: smtp.qq.com port: 587 username: 1185172056@qq.com password: 不是qq邮箱的密码,是授权码 properties: smtp: auth: true starttls: enable: true required: true# mail:# smtp:# ssl:# enable: true
package com.futao.springmvcdemo.service.implimport com.alibaba.fastjson.JSONimport com.futao.springmvcdemo.model.system.MailMimport com.futao.springmvcdemo.model.system.SystemConfigimport com.futao.springmvcdemo.service.MailServiceimport org.apache.rocketmq.client.producer.DefaultMQProducerimport org.apache.rocketmq.common.message.Messageimport org.slf4j.LoggerFactoryimport org.springframework.beans.factory.annotation.Valueimport org.springframework.mail.SimpleMailMessageimport org.springframework.mail.javamail.JavaMailSenderimport org.springframework.mail.javamail.MimeMessageHelperimport org.springframework.stereotype.Serviceimport org.thymeleaf.TemplateEngineimport org.thymeleaf.context.Contextimport java.nio.charset.Charsetimport javax.annotation.Resource/** * @author futao * Created on 2018/10/17. */@Serviceopen class MailServiceImpl : MailService { private val logger = LoggerFactory.getLogger(MailServiceImpl::class.java) @Value("\${spring.mail.username}") lateinit var username: String @Resource lateinit var sender: JavaMailSender /** * 发送简单邮件 */ override fun sendSimpleEmail(to: Array, cc: Array , subject: String, content: String): Boolean { return try { val mailMessage = SimpleMailMessage().apply { from = username setTo(*to) setCc(*cc) setSubject(subject) text = content } sender.send(mailMessage) true } catch (e: Exception) { logger.error(e.message, e) false } }
/** * 发送html邮件 */ override fun sendHtmlEmail(to: Array, cc: Array , subject: String, content: String, isHtml: Boolean): Boolean { return try { val message = sender.createMimeMessage() MimeMessageHelper(message).apply { setFrom(username) setTo(to) setCc(cc) setSubject(subject) setText(content, isHtml) } sender.send(message) true } catch (e: Exception) { logger.error(e.message, e) false } }
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
spring: thymeleaf: cache: true prefix:classpath: /templates/ mode: HTML
Hi, Developer
There is an exception occurred in method
methodName
, the error message is :occurredTimeError Message
/** * 使用邮件模板发送邮件 */ override fun sendHtmlEmailWithTemplate(to: Array, cc: Array , subject: String, templatePath: String, context: Context): Boolean { return try { val message = sender.createMimeMessage() MimeMessageHelper(message).apply { setFrom(username) setTo(to) setCc(cc) setSubject(subject) setText(template.process(templatePath, context), true) } sender.send(message) true } catch (e: Exception) { logger.error(e.message, e) false } }
下一篇文章将介绍使用消息队列发送邮件
转载地址:http://fiezo.baihongyu.com/