Spring Boot - 邮件服务

2018-10-10  本文已影响15人  yuanzicheng

在Java世界,邮件相关功能主要经历了JavaMail > JavaMailSender > spring-boot-starter-mail这几个阶段,目前我们在Spring Boot项目中使用邮件服务已经变得非常简单了。

添加依赖

compile('org.springframework.boot:spring-boot-starter-mail')

application.yml中邮件服务相关配置

spring:
  mail:
    host: 邮件服务域
    username: 账号
    password: 密码

编写一个专门发送邮件的类MailHelper.kt

@Component
class MailHelper {

    private val logger = LoggerFactory.getLogger(this.javaClass)

    @Autowired
    private lateinit var javaMailSender: JavaMailSender

    @Value("\${spring.mail.username}")
    private lateinit var from: String

    /**
     * 发送简单文本邮件
     */
    fun sendSimpleMail(to: String, subject: String, content: String) {
        val message = SimpleMailMessage()
        message.setFrom(from)
        message.setTo(to)
        message.setSubject(subject)
        message.setText(content)
        javaMailSender.send(message)
    }

    /**
     * 发送HTML格式邮件
     */
    fun sendHtml(to: String, subject: String, content: String) {
        val message = javaMailSender.createMimeMessage()
        val helper = MimeMessageHelper(message, true)
        helper.setFrom(from)
        helper.setTo(to)
        helper.setSubject(subject)
        helper.setText(content, true)
        javaMailSender.send(message)
    }

    /**
     * 发送带附件的邮件(使用InputStream从内存中获取文件内容;如果是本地文件,可以考虑使用FileSystemResource)
     */
    fun sendAttachments(to: String, subject: String, content: String, fileName: String, inputStream: InputStream) {
        val message = javaMailSender.createMimeMessage()
        val helper = MimeMessageHelper(message, true)
        helper.setFrom(from)
        helper.setTo(to)
        helper.setSubject(subject)
        helper.setText(content, true)
        helper.addAttachment(fileName, ByteArrayResource(inputStream.readBytes()))
        javaMailSender.send(message)
    }

    /**
     * 发送带静态资源(一般为图片)的邮件
     */
    fun sendInline(to: String, subject: String, content: String,contentId: String, inputStream: InputStream, contentType: String) {
        val message = javaMailSender.createMimeMessage()
        val helper = MimeMessageHelper(message, true)
        helper.setFrom(from)
        helper.setTo(to)
        helper.setSubject(subject)
        helper.setText(content, true)
        helper.addInline(contentId, ByteArrayResource(inputStream.readBytes()), contentType)
        javaMailSender.send(message)
    }
}

在Spring项目中需要使用发送邮件功能的时候,注入上面的MailHelper,就可以使用了。

发送附件及图片的测试代码如下,使用了kotlin中的use函数自动关闭实现了Closeable接口的类对象,类似java中的try-with-resource,不过更为简洁方便。

@RunWith(SpringRunner::class)
@SpringBootTest
class ApplicationTests {

    @Autowired
    private lateinit var mailHelper: MailHelper

    @Test
    fun test1() {
        val inputStream = File("/home/xxx/Desktop/93237012.pdf").inputStream()
        inputStream.use {
            mailHelper.sendAttachments("your.email@xx.xx", "附件邮件", "测试附件邮件", "测试文件.pdf", it)
        }
    }

    @Test
    fun test2() {
        val cid = "123"
        val content = "<html><body>测试图片邮件:<img src=\'cid:$cid\' ></body></html>"
        val inputStream = File("/home/xxx/Desktop/123.png").inputStream()
        inputStream.use {
            mailHelper.sendInline("your.email@xxx@xx.xx", "图片邮件", content, cid, it, "image/png")
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读