I'm using mail 0.9 and it seems that the attachment feature is still not in? Was this item still not included until now?
If that's the case, please tell me how to extend a Grails plugin without hacking the code directly.
Thanks.
I'm using mail 0.9 and it seems that the attachment feature is still not in? Was this item still not included until now?
If that's the case, please tell me how to extend a Grails plugin without hacking the code directly.
Thanks.
It seems that I missed the Attachment section in the documentation. What I saw was the TODO section (that should be updated btw). Anyway, here's a much clearer example than the one mentioned there.
String path = "./web-app/images/grails_logo.jpg"
sendMail {
multipart true
to '[email protected]'
subject "Welcome to Grails!"
body '''
Greetings Earthlings!
'''
attachBytes path,'image/jpg', new File(path).readBytes()
}
With this, you can attach any kind of files as long as you properly specify the content type I guess.
Grails plugin ('grails install-plugin mail') works perfectly well even over TLS - see mac.com sending requirements.
However, for those using Outlook or other corporate email systems, I found a slightly different Grails solution using resources.xml and the Spring JavaMail helper classes:
1) Add the following to myapp/grails-app/conf/spring/resources.xml (see below)
2) Define the service in your business service as needed.
3) Add a few imports- done! import javax.mail.internet.MimeMessage import org.springframework.core.io.FileSystemResource import org.springframework.mail.javamail.MimeMessageHelper
def mailSender
<?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>
Java code to add attachment :
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" )