使用邮件插件在Grails中附加文件
我使用的是mail 0.9,附件功能似乎还不在?到现在还没有包括在内吗 如果是这样,请告诉我如何扩展Grails插件而不直接破解代码使用邮件插件在Grails中附加文件,grails,plugins,email,Grails,Plugins,Email,我使用的是mail 0.9,附件功能似乎还不在?到现在还没有包括在内吗 如果是这样,请告诉我如何扩展Grails插件而不直接破解代码 谢谢。我似乎错过了报告中的附件部分。我看到的是待办事项部分(顺便说一句,应该更新)。无论如何,这里有一个比上面提到的更清楚的例子 String path = "./web-app/images/grails_logo.jpg" sendMail { multipart true to 'alfred@fbmsoftware.com' subje
谢谢。我似乎错过了报告中的附件部分。我看到的是待办事项部分(顺便说一句,应该更新)。无论如何,这里有一个比上面提到的更清楚的例子
String path = "./web-app/images/grails_logo.jpg"
sendMail {
multipart true
to 'alfred@fbmsoftware.com'
subject "Welcome to Grails!"
body '''
Greetings Earthlings!
'''
attachBytes path,'image/jpg', new File(path).readBytes()
}
有了它,您可以附加任何类型的文件,只要您正确地指定了我猜的内容类型。Grails插件(“Grails安装插件邮件”)即使在TLS上也能很好地工作-请参阅mac.com发送要求
但是,对于那些使用Outlook或其他公司电子邮件系统的用户,我发现使用resources.xml和Spring JavaMail helper类的Grails解决方案略有不同:
1) 将以下内容添加到myapp/grails-app/conf/spring/resources.xml(见下文)
2) 根据需要在业务服务中定义服务
3) 添加一些导入-完成!
导入javax.mail.internet.mimessage
导入org.springframework.core.io.FileSystemResource
导入org.springframework.mail.javamail.mimessagehelper
def邮件发送者
正确,使用mail 0.9,我已经能够获得附件,如您所示。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- Mail service -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="mail.munger.somecorp.com"/>
<property name="port" value="25"/>
<property name="javaMailProperties">
<props>
<prop key="mail.debug">false</prop>
</props>
</property>
</bean>
<!-- more bean definitions go here... -->
</beans>
MimeMessage message = mailSender.createMimeMessage()
MimeMessageHelper helper = new MimeMessageHelper( message, true )
for ( String recipients : [ customer1, customer2, customer3, customer4 ].findAll { it != null } )
{
helper.addTo( str );
}
helper.setFrom( "" )
helper.setSubject( aSubject )
helper.setText("...")
FileSystemResource fileResource =
new FileSystemResource( new File(tempFile) )
helper.addAttachment( tempFile.substring(tempFile.lastIndexOf( "/" ) + 1), fileResource, "application/pdf" )