如何使用groovy将html模板作为邮件发送 [英] How to send html template as mail using groovy
本文介绍了如何使用groovy将html模板作为邮件发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用JavaMail API 1.4.4发送邮件。到目前为止,我可以发送邮件,但实际上我需要发送HTML内容,以便在收到邮件时处理html标签。
示例:如果我在我的信息中有一个表格代码,它应该处理html代码并将它显示在邮件中
我的代码
import java.io.File;
import java.util。*
import javax.mail。*
import javax.mail.internet。*
import javax.activation。*
class Mail {
static void sendMail(mailProp){
//获取系统属性
属性properties = System.getProperties()
//安装邮件服务器
properties.setProperty(mail.smtp.host,mailProp.host)
//获取默认的Session对象。
Session session = Session.getDefaultInstance(properties)
try {
//创建一个默认的MimeMessage对象。
MimeMessage消息=新的MimeMessage(会话)
//设置自:头部的头部字段。
message.setFrom(新InternetAddress(mailProp.from))
//设置为:标头的标题字段。
message.addRecipient(Message.RecipientType.TO,new InternetAddress(mailProp.to))
//设置主题:标题栏
message.setSubject(My Subject! )
//现在设置实际的消息
message.setText(createMessage())
//发送消息
Transport.send(message)
System.out.println(发送消息成功....)
}
catch(MessagingException mex){
mex.printStackTrace()
}
static def createMessage(){
def message =< h1>这是实际的消息< / h1>
}
static main(args){
AppProperties.load()
def mailProp = [:]
mailProp.host = AppProperties.get(host )
mailProp.from = AppProperties.get(sender)
mailProp.to = AppProperties.get(receiver)
mailProp.server = AppProperties.get(mailserver)
sendMail(mailProp)
}
}
try {
//创建一个默认的MimeMessage对象。
new MimeMessage(session).with {message - >
// From,Subject and Content
from = new InternetAddress(mailProp.from)
subject =My Subject!
setContent createMessage(),'text / html'
//添加收件人
addRecipient(Message.RecipientType.TO,new InternetAddress(mailProp.to))
//发送消息
Transport.send(消息)
println发送成功
}
}
catch(MessagingException mex ){
mex.printStackTrace()
}
I am using JavaMail API 1.4.4 to send mails. So far I am able to send the mail, but actually I need to send HTML content so that when the mail is received it processes the html tags.
Example: if I have a table code in my message it should process the html code and present it in the mail
My Code
import java.io.File; import java.util.* import javax.mail.* import javax.mail.internet.* import javax.activation.* class Mail { static void sendMail(mailProp) { // Get system properties Properties properties = System.getProperties() // Setup mail server properties.setProperty("mail.smtp.host", mailProp.host) // Get the default Session object. Session session = Session.getDefaultInstance(properties) try { // Create a default MimeMessage object. MimeMessage message = new MimeMessage(session) // Set From: header field of the header. message.setFrom(new InternetAddress(mailProp.from)) // Set To: header field of the header. message.addRecipient(Message.RecipientType.TO,new InternetAddress(mailProp.to)) // Set Subject: header field message.setSubject("My Subject!") // Now set the actual message message.setText(createMessage()) // Send message Transport.send(message) System.out.println("Sent message successfully....") } catch( MessagingException mex ) { mex.printStackTrace() } } static def createMessage() { def message="""<h1>This is actual message</h1>""" } static main(args) { AppProperties.load() def mailProp=[:] mailProp.host=AppProperties.get("host") mailProp.from=AppProperties.get("sender") mailProp.to=AppProperties.get("receiver") mailProp.server=AppProperties.get("mailserver") sendMail(mailProp) } }
解决方案A Groovier way of sending might be:
try { // Create a default MimeMessage object. new MimeMessage(session).with { message -> // From, Subject and Content from = new InternetAddress( mailProp.from ) subject = "My Subject!" setContent createMessage(), 'text/html' // Add recipients addRecipient( Message.RecipientType.TO, new InternetAddress( mailProp.to ) ) // Send the message Transport.send( message ) println "Sent successfully" } } catch( MessagingException mex ) { mex.printStackTrace() }
这篇关于如何使用groovy将html模板作为邮件发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文