2010-02-19 31 views

回答

5

看来我错过了附件部分在documentation。我看到的是TODO部分(应该更新btw)。无论如何,这里比这里提到的更清晰。

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() 
} 

有了这个,只要您正确指定了我猜的内容类型,就可以附加任何类型的文件。

+0

正确,使用邮件0.9,我已经能够获得附件正如你所示。 – 2010-02-19 13:33:33

0

即使在TLS之上,Grails插件('grails install-plugin mail')也能很好地工作 - 请参阅mac.com发送要求。

然而,对于使用Outlook或其他企业电子邮件系统的,我找到了一个稍微不同的Grails的解决方案resource.xml的使用和Spring JavaMail的辅助类:

1)添加以下的myapp /在grails-app/conf/spring/resources.xml(见下文)

2)根据需要在您的业务服务中定义服务。

3)添加一些导入 - 完成! 进口的javax.mail.internet.MimeMessage 进口org.springframework.core.io.FileSystemResource 进口org.springframework.mail.javamail.MimeMessageHelper

高清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代码添加附件:

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")