`
JavaCrazyer
  • 浏览: 3009104 次
  • 性别: Icon_minigender_1
  • 来自: 河南
社区版块
存档分类

Spring温习(6)--邮件发送实现

阅读更多

Spring对邮件发送支持的很好,我们只要配置好邮件发送器,写好邮件发送具体操作类的方法,那么实现就不是很难的事,不过这个过程中经常会出现这样或那样的错误,所以待讲完本篇后我会总体总结下spring邮件发送容易发生的错误及解决办法

关于邮件发送,不外乎有这么几种情况,一种是纯文本的发送,一种是HTML形式的发送,一种就是发送时带有附件,一种就是发送时采取的事群发,针对这几个我将一一来讲下,我的代码中将第一中归为一大类,后面三种归为一代类,所以将会出现两种配置文件

第一大类邮件发送【纯文本发送】

必须包:spring.jar,common-logging.jar,mail.jar,servlet.jar,common-collection.jar

首先我们来看spring配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    <!--定义邮件发送器,配置好发送者详细信息-->
	<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
		<property name="host" value="smtp.163.com" />
		<property name="port" value="25" />
		<property name="username" value="emailusername" />
		<property name="password" value="emailpassword" />
		<property name="defaultEncoding" value="utf-8" />
		<property name="javaMailProperties">
			<props>
				<prop key="mail.smtp.auth">true</prop>
			</props>
		</property>
	</bean>

	<bean id="emailService" class="com.javacrazyer.comon.SendOrderConfirmationEmailAdvice">
		<property name="mailSender" ref="mailSender" />
	</bean>

</beans>

用到的实体类Order.java

package com.javacrazyer.comon;

import java.io.Serializable;

public class Order implements Serializable {

	/* Private Fields */

	private int orderId;
	private String username;
	private String useremail;

	public int getOrderId() {
		return orderId;
	}

	public void setOrderId(int orderId) {
		this.orderId = orderId;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getUseremail() {
		return useremail;
	}

	public void setUseremail(String useremail) {
		this.useremail = useremail;
	}

}

 

 邮件发送类的接口类MailService.java

package com.javacrazyer.comon;


/**
 * 使用Spring发送邮件
 * 
 */

public interface MailService {
	
	
	void sendOrderMail(Order order);

}

 

具体实现的邮件发送类SendOrderConfirmationEmailAdvice.java

package com.javacrazyer.comon;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

public class SendOrderConfirmationEmailAdvice implements MailService{

	private String from="emailusername@163.com"; //发送人邮箱地址,必须与spring配置文件中的邮箱地址一样
	private String registeTemplateName = "com/javacrazyer/comon/mail_registe.txt";
	private MailSender mailSender;

	public void setMailSender(MailSender mailSender) {
        this.mailSender = mailSender;
    }


	public void sendOrderMail(Order order) {
		//SimpleMialMessage只能用来发送TEXT格式的邮件
		SimpleMailMessage mail = new SimpleMailMessage();
		mail.setFrom(this.from);
		mail.setTo(order.getUseremail());
		mail.setSubject("恭喜你成功注册成为SOMEDAY商城的会员!");
		mail.setText(loadTemplateContent(registeTemplateName).replaceAll("\\$\\{LOGINNAME\\}", order.getUsername()));
		this.mailSender.send(mail);

	}


	
	private String loadTemplateContent(String templateName){
		StringBuilder sb = new StringBuilder();
		BufferedReader br= null;
		try{
			br = new BufferedReader(
					new InputStreamReader(
						Thread.currentThread()
							  .getContextClassLoader()
							  .getResourceAsStream(templateName), "UTF-8"));
			
			String lineSeparator = System.getProperty("line.separator");
			for(String str = null; (str = br.readLine()) != null;){
				sb.append(str);
				sb.append(lineSeparator);
			}
		}catch(IOException e){
			e.printStackTrace();
		}finally{
			if(br != null){
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		return sb.toString();
	}



}

 用到的mail_registe.txt

亲爱的${LOGINNAME}:您好!
恭喜你成为SOMEDAY商城的会员!

你的登录用户名为:${LOGINNAME}
你的登录口令为:******(隐藏)

本站网址:http://www.yoursitename.cn
联系邮箱:hr@yoursite.cn

 

测试发送

package com.javacrazyer.service.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.javacrazyer.comon.MailService;
import com.javacrazyer.comon.Order;

public class UserServiceTest {

	@Test
	public void test() throws InterruptedException {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		MailService sf = (MailService) context.getBean("emailService");
		Order order = new Order();
		order.setUsername("cheneywu");
		order.setUseremail("xxxxxx@qq.com");
		sf.sendOrderMail(order);
	}
}

 注意查收邮箱中的信息哦


第二大类邮件发送【HTML格式的发送,群发,附件】

  除了上面包外,还需要spring-webmvc.jar,freemarker.jar,为什么需要freemarker呢,因为要发送HTML格式的文件,所以要先写好HTML内容的文件,然后用模板freemarker匹配其中的值,Spring中对freemarker也有很好的支持

 spring配置文件applicatonContext-html.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	<bean id="freeMarker"
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
		<property name="templateLoaderPath" value="classpath:mailTemplate" />
		<!--指定模板文件目录-->
		<property name="freemarkerSettings"><!-- 设置FreeMarker环境属性-->
			<props>
				<prop key="template_update_delay">1800</prop><!--刷新模板的周期,单位为秒-->
				<prop key="default_encoding">UTF-8</prop><!--模板的编码格式 -->
				<prop key="locale">zh_CN</prop><!-- 本地化设置-->
			</props>
		</property>
	</bean>

	<!-- 注意:这里的参数(如用户名、密码)都是针对邮件发送者的 -->
	<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
		<property name="host" value="smtp.163.com" />
		<property name="port" value="25" />
		<property name="username" value="emailusername" />
		<property name="password" value="emailpassword" />
		<property name="defaultEncoding" value="utf-8" />
		<property name="javaMailProperties">
			<props>
				<prop key="mail.smtp.auth">true</prop>
			</props>
		</property>
	</bean>
	<bean id="emailService" class="com.javacrazyer.comon.EmailService">
		<property name="mailSender" ref="mailSender"></property>
		<property name="freeMarkerConfigurer" ref="freeMarker"></property>
	</bean>
</beans>    

 模板文件目mailTemplate下的模板文件registe.ftl 【这个文件无名字、后缀无所谓,只要内容是HTML内容即可】

<html>  
   <head>  
      <meta http-equiv="content-type" content="text/html;charset=utf8">  
   </head>  
   <body>  
       恭喜您成功注册!您的用户名为:<font color='red' size='30'>${LOGINNAME}</font>  
   </body>  
</html>  

 具体邮件发送类EmailService.java

  

package com.javacrazyer.comon;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;

import org.springframework.mail.MailSender;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import freemarker.template.Template;

public class EmailService implements MailService {

	private JavaMailSender mailSender;
	private FreeMarkerConfigurer freeMarkerConfigurer = null; // FreeMarker的技术类

	public void setMailSender(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }



	public void setFreeMarkerConfigurer(
			FreeMarkerConfigurer freeMarkerConfigurer) {
		this.freeMarkerConfigurer = freeMarkerConfigurer;
	}
	/**
	 * 发送带模板的单个html格式邮件
	 */
	public void sendOrderMail(Order order) {
		MimeMessage msg = mailSender.createMimeMessage();
		// 设置utf-8或GBK编码,否则邮件会有乱码,true表示为multipart邮件

		try {
			MimeMessageHelper helper = new MimeMessageHelper(msg, true, "utf-8");
			helper.setTo(order.getUseremail()); // 邮件接收地址
			helper.setFrom("emailusername@163.com"); // 邮件发送地址,这里必须和xml里的邮件地址相同一致
			helper.setSubject("你好,恭喜你注册成功"); // 主题
			String htmlText;
			htmlText = getMailText(order.getUsername());
			helper.setText(htmlText, true); // 邮件内容,注意加参数true,表示启用html格式
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // 使用模板生成html邮件内容

		mailSender.send(msg); // 发送邮件
	}

	/**
	 * 批量发送带附件的html格式邮件,带附件
	 */
	public void sendBatchEmail(Order order, List<String> address) {
		
		MimeMessage msg = mailSender.createMimeMessage();

		try {
			MimeMessageHelper helper = new MimeMessageHelper(msg, true, "utf-8");
			String toList = getMailList(address);
			InternetAddress[] iaToList = new InternetAddress().parse(toList);
			msg.setRecipients(Message.RecipientType.TO, iaToList);
			helper.setFrom("emailusername@163.com");
			helper.setSubject("你好,恭喜你注册成功");
			String htmlText = getMailText(order.getUsername());
			helper.setText(htmlText, true);
			// 添加内嵌文件,第1个参数为cid标识这个文件,第2个参数为资源
			helper.addInline("a", new File("E:/11.jpg")); // 附件内容
			helper.addInline("b", new File("E:/12.jpg"));
			File file = new File("E:/各种框架图介绍.docx");
			// 这里的方法调用和插入图片是不同的,使用MimeUtility.encodeWord()来解决附件名称的中文问题
			helper.addAttachment(MimeUtility.encodeWord(file.getName()), file);
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 如果主题出现乱码,可以使用该函数,也可以使用下面的函数
		// helper.setSubject(MimeUtility.encodeText(String text,String
		// charset,String encoding))
		// 第2个参数表示字符集,第三个为目标编码格式。
		// MimeUtility.encodeWord(String word,String charset,String encoding)
		mailSender.send(msg);
	}

	/**
	 * 集合转换字符串
	 */
	public String getMailList(List<String> to) {
		StringBuffer toList = new StringBuffer();
		int length = to.size();
		if (to != null && length < 2) {
			toList.append(to.get(0));
		} else {
			for (int i = 0; i < length; i++) {
				toList.append(to.get(i));
				if (i != (length - 1)) {
					toList.append(",");
				}
			}
		}
		return toList.toString();
	}

	// 通过模板构造邮件内容,参数content将替换模板文件中的${content}标签。
	private String getMailText(String content) throws Exception {
		String htmlText = "";
		// 通过指定模板名获取FreeMarker模板实例
		Template tpl = freeMarkerConfigurer.getConfiguration().getTemplate(
				"registe.ftl");
		Map map = new HashMap(); // FreeMarker通过Map传递动态数据
		map.put("LOGINNAME", content); // 注意动态数据的key和模板标签中指定的属性相匹配
		// 解析模板并替换动态数据,最终content将替换模板文件中的${content}标签。
		htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(tpl, map);
		return htmlText;
	}

}

  单一发送HTML格式的邮件测试

  

 

 

package com.javacrazyer.service.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.javacrazyer.comon.MailService;
import com.javacrazyer.comon.Order;

public class UserServiceTest {

	@Test
	public void test() throws InterruptedException {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"applicationContext-html.xml");
		MailService sf = (MailService) context.getBean("emailService");
		Order order = new Order();
		order.setUsername("cheneywu");
		order.setUseremail("xxxxxx@qq.com");
		sf.sendOrderMail(order);
	}
}

 收到邮件了


群发,带附件的测试

package com.javacrazyer.service.test;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.javacrazyer.comon.EmailService;
import com.javacrazyer.comon.Order;

public class UserServiceTest {

	@Test
	public void test() throws InterruptedException {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"applicationContext-html.xml");
		EmailService sf = (EmailService) context.getBean("emailService");
		Order order = new Order();
		order.setUsername("cheneywu");
		List<String> maillist = new ArrayList<String>();
		maillist.add("XXXXXX@qq.com");
		maillist.add("YYYYYY@qq.com");
		sf.sendBatchEmail(order, maillist);
	}
}

 收到邮件了哦



 看到了没有,这个上边有两个接收人,下边的是附件

 

 


 
 

 

 

 

  • 大小: 8.6 KB
  • 大小: 7.6 KB
  • 大小: 19.5 KB
18
0
分享到:
评论
4 楼 306085175 2010-11-11  
432432432144
3 楼 JavaLanguageFun 2010-11-10  
一直想写个外部邮件 ,谢谢LZ的分享 。
2 楼 JavaCrazyer 2010-10-30  
yuantong 写道
org.springframework.mail.MailSendException; nested exceptions (0) are:
Caused by: javax.mail.MessagingException: Exception reading response;
  nested exception is:
java.net.SocketException: Connection reset
at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1090)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:986)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:197)
at javax.mail.Service.connect(Service.java:233)
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:373)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:291)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:277)

这个估计是SMTP设置的不对, 比如163邮箱的是smpt.163.com
1 楼 yuantong 2010-10-30  
org.springframework.mail.MailSendException; nested exceptions (0) are:
Caused by: javax.mail.MessagingException: Exception reading response;
  nested exception is:
java.net.SocketException: Connection reset
at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1090)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:986)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:197)
at javax.mail.Service.connect(Service.java:233)
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:373)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:291)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:277)

相关推荐

    springboot 整合spring-boot-starter-mail 发邮件.rar

    springboot 整合spring-boot-starter-mail 发邮件

    spring-boot-邮件发送

    #### 二、邮件发送实现 接下来,我们将详细介绍如何实现邮件发送的功能。 1. **创建邮件发送工具类:** - 可以创建一个名为`EmailSend`的类,该类负责封装邮件发送的逻辑。这个类需要被Spring管理,因此需要添加`...

    jasypt-spring-boot-starter 3.0.5依赖的pom及jar

    jasypt-spring-boot-starter是一个流行的安全库,它允许开发者在Spring Boot应用中轻松地实现加密和解密功能。版本3.0.5的jasypt-spring-boot-starter带来了更强大的功能和改进,本文将深入探讨其背后的依赖结构,...

    spring-cglib-repack-3.2.5.jar和spring-objenesis-repack-2.6.jar

    标题中的"spring-cglib-repack-3.2.5.jar"和"spring-objenesis-repack-2.6.jar"是两个与Spring框架相关的库文件,它们主要用于Spring框架的内部实现,尤其是针对Java对象的创建和代理机制。下面将详细解释这两个库的...

    spring-security-web源码所需jar包

    6. **spring-security-core-3.1.2.RELEASE.jar**:Spring Security的核心模块,包含了安全抽象,如权限表达式,访问决策管理器,以及认证和授权的基础设施。 7. **spring-aop-3.1.2.RELEASE.jar**:Spring的面向切...

    spring-cglib-repack-3.2.5.jar,spring-objenesis-repack-2.6.jar

    标题中的"spring-cglib-repack-3.2.5.jar"和"spring-objenesis-repack-2.6.jar"是Spring框架在运行时依赖的两个关键库,它们主要涉及到动态代理和对象创建的优化。 1. **CGLIB(Code Generation Library)**: ...

    spring导入eclipse时少的spring-cglib-repack-3.2.4.jar和spring-objenesis-repack-2.4.jar

    然而,有时候在导入Spring项目时,可能会遇到一些问题,例如标题中提到的"spring-cglib-repack-3.2.4.jar"和"spring-objenesis-repack-2.4.jar"这两个jar包的缺失。这些jar包对于Spring框架的正常运行至关重要,因为...

    spring-context-support-4.2.2.RELEASE.jar 的jar包

    总结来说,`spring-context-support`是Spring框架中一个不可或缺的部分,它为开发者提供了丰富的工具和API,帮助实现各种企业级服务的集成,如定时任务、邮件服务、消息队列等。通过深入理解和熟练运用这一模块,...

    spring-boot-mail邮件发送服务 v1.0.zip

    本项目"spring-boot-mail邮件发送服务 v1.0"就是基于Spring Boot实现的邮件发送功能,旨在帮助开发者快速构建自己的邮件发送系统。 首先,我们需要理解Spring Boot的核心理念:简化Spring应用的初始搭建以及开发...

    spring-boot-mail

    spring boot 邮件发送,亲测可用 #字符集 spring.mail.default-encoding=UTF-8 #电子邮件地址 spring.mail.host=smtp.126.com #Application spring.application.name=SEND-MAIL #授权密码 spring.mail.password=...

    Spring源码编译缺少的两个包:spring-cglib-repack-3.2.0.jar和spring-objenesis-repack-2.2.jar

    总之,spring-cglib-repack-3.2.0.jar和spring-objenesis-repack-2.2.jar是Spring源码编译的关键组成部分,它们为Spring的AOP代理功能提供了底层支持,特别是对于没有实现接口的类。正确地包含这些库可以确保顺利...

    spring-framework-4.1.6.RELEASE.rar

    spring-aop-4.1.6.RELEASE.jar spring-aspects-4.1.6.RELEASE.jar spring-beans-4.1.6.RELEASE.jar spring-context-4.1.6.RELEASE.jar spring-context-support-4.1.6.RELEASE.jar spring-core-4.1.6.RELEASE.jar ...

    spring-context-3.2.0 spring-core-3.2.0 等齐全的Spring jar包

    这里提到的是一组完整的Spring库,包括`spring-core`、`spring-context`、`spring-webmvc`、`spring-web`、`spring-beans`、`spring-test`、`spring-jdbc`、`spring-orm`、`spring-aop`和`spring-tx`,它们都是3.2.0...

    spring-beans-3.0.xsd

    《Spring框架中的beans配置文件详解——以spring-beans-3.0.xsd和3.1.xsd为例》 在Spring框架中,`spring-beans`是核心组件之一,它负责管理对象的生命周期和依赖关系。`spring-beans`的配置文件通常以`.xsd`为后缀...

    spring-boot-starter-websocket.zip

    通过以上步骤,我们可以利用“spring-boot-starter-websocket.zip”中的内容搭建起一个完整的站内信系统,实现实时的消息推送功能。这个压缩包提供的起步依赖简化了WebSocket在Spring Boot中的集成工作,让开发者...

    spring-context-support-4.2.2和quartz-2.2.3所需Jar包

    标题中的"spring-context-support-4.2.2和quartz-2.2.3所需Jar包"涉及到两个关键的Java库:Spring Context Support 4.2.2版本和Quartz Scheduler 2.2.3版本。这两个库在企业级Java应用开发中扮演着重要角色,特别是...

    spring 3.2.4.RELEASE jar包

    spring-context-3.2.4.RELEASE.jar spring-core-3.2.4.RELEASE.jar spring-beans-3.2.4.RELEASE.jar spring-test-3.2.4.RELEASE.jar spring-web-3.2.4.RELEASE.jar spring-aop-3.2.4.RELEASE.jar spring-webmvc-...

    intellij-spring-assistant-1.0.2.zip

    6. **版本兼容性**:"intellij-spring-assistant-1.0.2"版本表明其对Spring框架的不同版本有较好的兼容性,包括Spring Boot、Spring MVC等。 7. **实时反馈**:在编写代码过程中,插件能够提供实时的类型检查和错误...

    官方spring-framework-5.0.2.RELEASE完整包

    官方spring-framework-5.0.2.RELEASE完整包官方spring-framework-5.0.2.RELEASE完整包官方spring-framework-5.0.2.RELEASE完整包

    spring4源码所需要的spring-objenesis-repack-2.1.jar,spring-cglib-repack-3.1.jar

    在Spring框架中,`spring-objenesis-repack-2.1.jar`和`spring-cglib-repack-3.1.jar`是两个关键的依赖库,它们在Spring的内部工作原理和功能扩展中扮演着重要角色。让我们深入探讨这两个库以及它们与Spring 4的关系...

Global site tag (gtag.js) - Google Analytics