Grails 2.4.3下使用Asynchronous Mail Plugin

第一步:安装

在buildconfig中添加

compile ":asynchronous-mail:1.1"

然后ide执行compile命令安装插件

第二步:设置

执行

grails install-asynchronous-mail-config

会在conf目录生成asynchronousMailConfig文件内容如下:

asynchronous.mail.default.attempt.interval=300000l        //5分钟,发送尝试的间隔时间,单位毫秒
asynchronous.mail.default.max.attempts.count=1   //每个消息发送尝试的最大次数
asynchronous.mail.send.repeat.interval=60000l          //1分钟,重复启动发送任务的时间间隔,单位毫秒
asynchronous.mail.expired.collector.repeat.interval=607000l //重复启动收集过期任务(如果发送时间过期了,就给消息打上过期的标记)
asynchronous.mail.messages.at.once=100 //一次能够发送的最大消息个数
asynchronous.mail.send.immediately=true //如果为true,会在消息创建后立刻运行发送任务。
asynchronous.mail.override=false//是否允许重写
asynchronous.mail.clear.after.sent=false //发送后清除内容记录
asynchronous.mail.disable=false //如果为true,则邮件发送系统停止工作
asynchronous.mail.useFlushOnSave=trueasynchronous.mail.persistence.provider='hibernate4'      // Possible values are 'hibernate', 'hibernate4', 'mongodb' //选择是使用数据对象关系框架
asynchronous.mail.gparsPoolSize=1 //邮件池大小
asynchronous.mail.newSessionOnImmediateSend=false
创建异步发送邮件的视图和控制器
create-asynchronous-mail-controller --package=com.example
通过它可以查看邮件的发送情况
第三步:配置

在conf/Config.groovy中增加邮件服务

grails {
    mail {
      host = "smtp.gmail.com"
      port = 465
      username = "live8139ddl@gmail.com"
      password = "yst3330800"
      props = ["mail.smtp.auth":"true",
               "mail.smtp.socketFactory.port":"465",
               "mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory",
               "mail.smtp.socketFactory.fallback":"false"]
 
 } }

修改DataSource.groovy文件配置数据库,完成后在数据库中新建数据库.(这一步必须做,用默认数据库会报错)

第四步:开始使用

新建控制器

grails create-controller com.xtcanyin.Mail 然后修改控制内容如下:

package com.xtcanyin

class MailController {
    def successService //所使用的服务类,类名第一个字母要小写
    

    def index() { }
    def send ={
        successService.serviceMethod(params.name,params.email) //服务类名.方法名
    render "email sent!"
        }

建立Grails服务类

grails create-service com.xtcanyin.success 并修改如下:

package com.xtcanyin

import grails.transaction.Transactional
import grails.plugin.asyncmail.AsynchronousMailService //引入异步发送插件
@Transactional //这个一定要有,否则一样报错
class SuccessService {
    boolean transactional = false
    AsynchronousMailService asynchronousMailService
    //AsynchronousMailService asyncMailService
    def serviceMethod(name,eamil) {
        asynchronousMailService.sendAsynchronousMail {
            // Mail parameters
            from 'admin<admin@xtcanyin.com>'
            to eamil
            subject 'Test';
            body "thank you ${name} for grails-mail"
            html '<body><u>Testup</u></body>';
            //attachBytes 'test.txt', 'text/plain', byteBuffer; 
            // Additional asynchronous parameters (optional)
            beginDate new Date(System.currentTimeMillis()+60000)    // Starts after one minute, default current date
            endDate new Date(System.currentTimeMillis()+3600000)   // Must be sent in one hour, default infinity
            maxAttemptsCount 3;   // Max 3 attempts to send, default 1
            attemptInterval 300000;    // Minimum five minutes between attempts, default 300000 ms
            delete true;    // Marks the message for deleting after sent
            immediate true;    // Run the send job after the message was created
            priority 10;   // If priority is greater then message will be sent faster
        }
    }
}

增加index.gsp和send.gsp两个视图

index.gsp

1
2
3
4
5
6
7
8
9
< 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

1
2
3
4
<%@ 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/xtcanyin/blog/323654

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值