Groovy&Grails-插件应用-Asynchronous Mail Plugin

信息

  • 评级:5星
  • 版本:1.0-RC6
  • 更新:2013-10-01
  • 适用:Grails2.0.0 > *

简介

这个插件实现了异步邮件发送功能,并配合数据库存储发送的消息

安装

<!-- lang: groovy -->
compile ":asynchronous-mail:1.0-RC6"

设置

<!-- lang: groovy -->
grails install-asynchronous-mail-config

生成AsynchronousMailConfig.groovy文件

<!-- lang: 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中增加邮件服务

<!-- lang:groovy -->
grails {
    mail {
        host = "smtp.gmail.com"
        port = 465
        username = "youracount@gmail.com"
        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"]
    }
}

使用

<!-- lang: groovy -->
grails create-controller com.example.Mail

编辑MailController.groovy

<!-- lang: groovy -->
import grails.plugin.asyncmail.AsynchronousMailService

class MailController {
    def asynchronousMailService
    def index = { 
        asynchronousMailService.sendAsynchronousMail {        
            to 'groovy.land@hotmail.com'
            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

<!-- lang: groovy -->
grails create-servicecom.example.Mail

将Controller中的邮件发送部分移到service,将Service中的body修改成grails链接

<!-- lang: groovy -->
package com.example

import grails.plugin.asyncmail.AsynchronousMailService

class MailService {

    boolean transactional = false

    AsynchronousMailService asynchronousMailService

    def serviceSendMail(name,email) {
        asynchronousMailService.sendAsynchronousMail {
            from 'NEO <jingzhi.li@gmail.com>'
            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

<!-- lang: groovy -->
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

<!-- lang: html-->
<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

<!-- lang: html-->
<%@ page contentType="text/plain" %>

Thank you ${name} for using grails-asynchronous-mail plugin from a service,
Rendered content from a GSP!

参考

转载于:https://my.oschina.net/65304586/blog/167833

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值