我正在使用 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();
}
}
答案 0 :(得分: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}"
}
}
答案 1 :(得分:1)
我认为你是从java类调用Service Class(.groovy)方法。 通过使用EmailService类的对象。 所以你不能得到PageRenderer类的Object。 为了这 在src / java中创建 SpringsUtil 类,并定义EmailSerevice的常量对象。像这样
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);