Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 如何添加胡须电子邮件模板并发送_Spring Boot_Email_Kotlin_Mustache - Fatal编程技术网

Spring boot 如何添加胡须电子邮件模板并发送

Spring boot 如何添加胡须电子邮件模板并发送,spring-boot,email,kotlin,mustache,Spring Boot,Email,Kotlin,Mustache,胡须模板:email.Mustache {{subject}} Hello {{userName}}! 我将发送的对象 {{subject}} Hello {{userName}}! data class Email( @Email val emailAddress: String, val subject: String, val userName: String, val message: String

胡须模板:
email.Mustache

{{subject}}

Hello {{userName}}!
我将发送的对象

{{subject}}

Hello {{userName}}!
data class Email(
        @Email
        val emailAddress: String,
        val subject: String,
        val userName: String,
        val message: String
)
我的服务:

{{subject}}

Hello {{userName}}!
@Service
class EmailService {

    @Autowired
    private lateinit var javaMailSender: JavaMailSender

    override fun sendEmail(email: Email): Boolean {
        val msg = SimpleMailMessage()
        msg.setTo(email.emailAddress)
        msg.setSubject(email.subject)
        msg.setText(email.message)
        try {
            javaMailSender.send(msg)
        } catch (e: Exception) {
            return false
        }
        return true
    }
}
但是我怎样才能在这里加载我的胡子模板呢?我想我需要解析胡须模板,然后使用它作为
msg.setText(parsedTemplate)
——类似的东西。

它看起来可以做你需要的事情:

{{subject}}

Hello {{userName}}!
import com.github.mustachejava.DefaultMustacheFactory
import com.github.mustachejava.Mustache
import java.io.StringWriter

data class Email(
    val subject: String,
    val userName: String
)

fun main() {
    val scopes = mapOf(
        "email" to Email(subject = "Welcome", userName = "J_Strauton")
    )

    val writer = StringWriter()
    val mf = DefaultMustacheFactory()
    val mustache: Mustache = mf.compile("email.mustache")
    mustache.execute(writer, scopes)
    writer.flush()

    val email = writer.toString()
}
您的
email.mustache
文件可能如下所示:

{{subject}}

Hello {{userName}}!
{{email.subject}}

Hello {{email.userName}}!