Grails邮件插件:附上org.springframework.web.multipart.commons.CommonsMultipartFile

时间:2011-05-19 04:29:14

标签: spring email grails attachment

我无法弄清楚如何做附件。已安装的插件“mail”和相关文件是从表单上传(成功)的.csv。

这有效:

def f = request.getFile('uploadedFile')
//do a bunch of stuff with f and make bodyString
MailService.sendMail {
  to "myemail@secretplace.com"
  subject "subject"
  body bodyString
}

这不是:

def f = request.getFile('uploadedFile')
//do a bunch of stuff with f and make bodyString
MailService.sendMail {
  multipart true
  to "myemail@secretplace.com"
  subject "subject"
  body bodyString
  attach "myfile.csv", f
}

哦,是的错误说明了这一点:

groovy.lang.MissingMethodException: No signature of method: my_project.MySweetController.attach() is applicable for argument types: (org.codehaus.groovy.runtime.GStringImpl, org.springframework.web.multipart.commons.CommonsMultipartFile) values: [Demo myfile.csv, org.springframework.web.multipart.commons.CommonsMultipartFile@73e2d16b]
Possible solutions: each(groovy.lang.Closure)

1 个答案:

答案 0 :(得分:2)

如果您使用的是0.9或更高版本的邮件插件,则以下内容应该可以正常工作

def f = request.getFile('uploadedFile')

//do a bunch of stuff with f and make bodyString
MailService.sendMail {
  multipart true
  to "myemail@secretplace.com"
  subject "subject"
  body bodyString
  attachBytes "Some-File-Name.csv", "text/csv", f.bytes
}

顺便提一下,您对邮件服务MailService的引用名称很奇怪。如果您通过自动装配Spring bean来获取对此服务的引用,我希望它是mailService