必須使用 JavaMailSender 代替 MailSender 傳送附件,並用 MimeMessageHelper 附加的資源。在這個例子中,它會得到 「c:\\log.txt」 從檔案系統(FileSystemResource)作為電子郵件附件的文字檔案。
File : MailMail.java
package com.tw511.common; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.MailParseException; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; public class MailMail { private JavaMailSender mailSender; private SimpleMailMessage simpleMailMessage; public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) { this.simpleMailMessage = simpleMailMessage; } public void setMailSender(JavaMailSender mailSender) { this.mailSender = mailSender; } public void sendMail(String dear, String content) { MimeMessage message = mailSender.createMimeMessage(); try{ MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(simpleMailMessage.getFrom()); helper.setTo(simpleMailMessage.getTo()); helper.setSubject(simpleMailMessage.getSubject()); helper.setText(String.format( simpleMailMessage.getText(), dear, content)); FileSystemResource file = new FileSystemResource("C:\\log.txt"); helper.addAttachment(file.getFilename(), file); }catch (MessagingException e) { throw new MailParseException(e); } mailSender.send(message); } }
File : Spring-Mail.xml
<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.5.xsd"> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="smtp.gmail.com" /> <property name="port" value="587" /> <property name="username" value="[email protected]" /> <property name="password" value="password" /> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">true</prop> <prop key="mail.smtp.starttls.enable">true</prop> </props> </property> </bean> <bean id="mailMail" class="com.tw511.common.MailMail"> <property name="mailSender" ref="mailSender" /> <property name="simpleMailMessage" ref="customeMailMessage" /> </bean> <bean id="customeMailMessage" class="org.springframework.mail.SimpleMailMessage"> <property name="from" value="[email protected]" /> <property name="to" value="[email protected]" /> <property name="subject" value="Testing Subject" /> <property name="text"> <value> <![CDATA[ Dear %s, Mail Content : %s ]]> </value> </property> </bean> </beans>
package com.tw511.common; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); MailMail mm = (MailMail) context.getBean("mailMail"); mm.sendMail("Yiibai", "This is text content"); } }
輸出結果
Dear Yiibai, Mail Content : This is text content Attachment : log.txt