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

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="yiibai.com@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.yiibai.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="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.yiibai.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
 
下载代码 –   http://pan.baidu.com/s/1jHn9VLW

本文转自左正博客园博客,原文链接:http://www.cnblogs.com/soundcode/p/6367594.html,如需转载请自行联系原作者
相关文章
|
Java Apache 网络架构
【小家Spring】Feign发送Get请求时,采用POJO对象传递参数的最终解决方案 Request method ‘POST‘ not supported (附带其余好几个坑)(上)
【小家Spring】Feign发送Get请求时,采用POJO对象传递参数的最终解决方案 Request method ‘POST‘ not supported (附带其余好几个坑)
|
Java Spring
Spring Boot集成JavaMailSender发送电子邮件
Spring Boot集成JavaMailSender发送电子邮件
219 0
Spring Boot集成JavaMailSender发送电子邮件
|
负载均衡 Java Spring
【小家Spring】Feign发送Get请求时,采用POJO对象传递参数的最终解决方案 Request method ‘POST‘ not supported (附带其余好几个坑)(下)
【小家Spring】Feign发送Get请求时,采用POJO对象传递参数的最终解决方案 Request method ‘POST‘ not supported (附带其余好几个坑)(下)
【小家Spring】Feign发送Get请求时,采用POJO对象传递参数的最终解决方案 Request method ‘POST‘ not supported (附带其余好几个坑)(下)
|
XML Java API
Spring发送邮件总结(附源码)
Spring发送邮件总结(附源码)
164 0
Spring发送邮件总结(附源码)
|
消息中间件 Java 中间件
Rocketmq spring 上手:如何在优雅地Spring 中实现消息的发送和消费
Rocketmq spring 上手:如何在优雅地Spring 中实现消息的发送和消费
|
消息中间件 Java Spring
消息队列AMQP Spring Boot发送延时消息
消息队列AMQP Spring Boot发送延时消息
|
消息中间件 Java RocketMQ
如何在优雅地Spring 中实现消息的发送和消费
本文将对rocktmq-spring-boot的设计实现做一个简单的介绍,读者可以通过本文了解将RocketMQ Client端集成为spring-boot-starter框架的开发细节,然后通过一个简单的示例来一步一步的讲解如何使用这个spring-boot-starter工具包来配置,发送和消费RocketMQ消息。
4966 0
|
Web App开发 Java 测试技术