Spring发送带附件邮件

下面是一个例子使用Spring通过Gmail SMTP服务器来发送电子邮件附件。为了包含附件的电子邮件,你必须使用 Spring的JavaMailSender及MimeMessage 来代替 MailSender&SimpleMailMessage。

2.Spring的邮件发件人

必须使用 JavaMailSender 代替 MailSender 发送附件,并用 MimeMessageHelper 附加的资源。在这个例子中,它会得到 “c:\\log.txt” 从文件系统(FileSystemResource)作为电子邮件附件的文本文件。

除了文件系统,您还可以从URL路径(UrlResource对象),类路径(使用ClassPathResource),InputStream(InputStreamResource)的任何资源......请参考 Spring 的 AbstractResource 类的实现。

File : MailMail.java

package com.zyiz.netmon;

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

3. Bean配置文件

配置 mailSender bean,电子邮件模板,并指定Gmail的SMTP服务器电子邮件的详细信息。

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="zyiz.net@gmail.com" />
	<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.zyiz.netmon.MailMail">
	<property name="mailSender" ref="mailSender" />
	<property name="simpleMailMessage" ref="customeMailMessage" />
</bean>
	
<bean id="customeMailMessage"
	class="org.springframework.mail.SimpleMailMessage">

	<property name="from" value="from@no-spam.com" />
	<property name="to" value="to@no-spam.com" />
	<property name="subject" value="Testing Subject" />
	<property name="text">
	<value>
		<![CDATA[
			Dear %s,
			Mail Content : %s
		]]>
	</value>
    </property>
</bean>

</beans>

4. 运行它

package com.zyiz.netmon;

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("zyiz", "This is text content");
        
    }
}

输出结果

Dear zyiz,
 Mail Content : This is text content
 Attachment : log.txt

下载代码 –  http://pan.baidu.com/s/1jHn9VLW

上一篇:Spring在bean配置文件中定义电子邮件模板

下一篇:Spring依赖注入servlet会话监听器

关注微信小程序
程序员编程王-随时随地学编程

扫描二维码
程序员编程王

扫一扫关注最新编程教程