2

我正在使用mail:1.0.1插件进行邮件发送

但是在发送邮件时它给了我一个错误..

资源 :-

def serviceMethod(EmailModel mailObj) {
        PageRenderer groovyPageRenderer;
        try{
            sendMail {
                to "abc@gmail.com"
                subject mailObj.subject;
                  html groovyPageRenderer.render(template:"myMailTemplate", model: [mailObj: mailObj])


            }
        } catch (Throwable th) {
                    th.printStackTrace();
        }
    }
4

2 回答 2

1

如果您想将 gsp 页面作为电子邮件正文发送,则可以这样发送:

def mailService

def serviceMethod(EmailModel mailObj) {
...
    mailService.sendMail {
        to email
        subject "subject"
        body(view: "/_template", model: [mailObj: mailObj])
    }
...
}

编辑................................................. .....................................

只需PageRenderer groovyPageRenderer全局注入,例如

import grails.gsp.PageRenderer
class TestService {
    PageRenderer groovyPageRenderer

    def getText() {
        String s = groovyPageRenderer.render(template: "../first/temp", model: [name: 'user1690588'])
        println "Content = ${s}"
    }
}
于 2013-08-29T08:05:32.373 回答
1

我认为您正在从 java 类调用 Service Class(.groovy) 方法。通过使用 EmailService 类的对象。所以你不能得到 PageRenderer 类的对象。为此在 src/java 中创建SpringsUtil类并定义 EmailService 的常量对象。像这样

public class SpringsUtil {

    public static ApplicationContext getCtx() {
        return getApplicationContext();
    }

    public static ApplicationContext getApplicationContext() {
        return (ApplicationContext) ServletContextHolder.getServletContext().getAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT);
    }

    @SuppressWarnings("unchecked")
    public static <T> T getBean(String beanName) {
        return (T) getApplicationContext().getBean(beanName);
    }


    public static final String EMAIL_SERVICE = "emailService";
//  public static final String INVENTORY_REORDER_SERVICE =  "InventoryReorderService";

}

创建Service类的对象并调用方法

EmailService emailService = SpringsUtil.getBean(SpringsUtil.EMAIL_SERVICE);

于 2016-05-17T06:46:06.630 回答