这个插件实现了异步邮件发送功能,并配合数据库存储发送的消息
compile ":asynchronous-mail:1.0-RC6"
grails install-asynchronous-mail-config
生成AsynchronousMailConfig.groovy文件
//5分钟,发送尝试的间隔时间,单位毫秒
asynchronous.mail.default.attempt.interval=300000l
//每个消息发送尝试的最大次数
asynchronous.mail.default.max.attempts.count=1
//1分钟,重复启动发送任务的时间间隔,单位毫秒
asynchronous.mail.send.repeat.interval=60000l
//重复启动收集过期任务(如果发送时间过期了,就给消息打上过期的标记)
//的时间间隔,单位毫秒
asynchronous.mail.expired.collector.repeat.interval=607000l
//一次能够发送的最大消息个数
asynchronous.mail.messages.at.once=100
//如果为true,会在消息创建后立刻运行发送任务。缺省值为true
asynchronous.mail.send.immediately=true
asynchronous.mail.override=false
//如果设置为true,则邮件发送完成后删除邮件内容
asynchronous.mail.clear.after.sent=false
//如果为true,则邮件发送系统停止工作
asynchronous.mail.disable=false
asynchronous.mail.useFlushOnSave=true
asynchronous.mail.gparsPoolSize=1
在conf/Config.groovy中增加邮件服务
grails {
mail {
host = "smtp.gmail.com"
port = 465
username = "[email protected]"
password = "yourpassword"
props = ["mail.smtp.auth":"true",
"mail.smtp.socketFactory.port":"465",
"mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory",
"mail.smtp.socketFactory.fallback":"false"]
}
}
grails create-controller com.example.Mail
编辑MailController.groovy
import grails.plugin.asyncmail.AsynchronousMailService
class MailController {
def asynchronousMailService
def index = {
asynchronousMailService.sendAsynchronousMail {
to '[email protected]'
subject 'Test'
html 'Test'
attachBytes 'test.txt', 'text/plain',AttachFile ContentBytes
// 附加的异步参数
// 在一分钟后启动,缺省是当前时间启动
beginDate new Date(System.currentTimeMillis()+60000)
// 必须在一小时内发送,缺省是无穷大
endDate new Date(System.currentTimeMillis()+3600000)
//尝试发送的最大次数为3,缺省值为1
maxAttemptsCount 3
//尝试发送的时间间隔,缺省为5分钟
attemptInterval 300000
}
render "Email Sent!"
}
}
新建一个MailService
grails create-servicecom.example.Mail
将Controller中的邮件发送部分移到service,将Service中的body修改成grails链接
package com.example
import grails.plugin.asyncmail.AsynchronousMailService
class MailService {
boolean transactional = false
AsynchronousMailService asynchronousMailService
def serviceSendMail(name,email) {
asynchronousMailService.sendAsynchronousMail {
from 'NEO <[email protected]>'
to email
subject 'Test Asynchronous-Mail plugin'
body (view:"/mail/send",model:[name:name])
//attachBytes 'test.txt', 'text/plain',AttachFile ContentBytes
// 附加的异步参数
// 在一分钟后启动,缺省是当前时间启动
beginDate new Date(System.currentTimeMillis()+6000)
// 必须在一小时内发送,缺省是无穷大
endDate new Date(System.currentTimeMillis()+3600000)
//尝试发送的最大次数为3,缺省值为1
maxAttemptsCount 3
//尝试发送的时间间隔,缺省为5分钟
attemptInterval 300000
}
}
}
修改Controller
package com.example
class MailController {
def mailService
def index() {}
def send = {
mailService.serviceSendMail(params.name, params.email)
render "Email Sent!"
}
}
增加index.gsp和send.gsp两个视图
index.gsp
<html>
<body>
<g:form action="send">
Enter You Email:<input name="email"/><br/>
Enter You Name: <input name="name"/><br/>
<input type="submit"/>
</g:form>
</body>
</html>
send.gsp
<%@ page contentType="text/plain" %>
Thank you ${name} for using grails-asynchronous-mail plugin from a service,
Rendered content from a GSP!