Spring邮件服务

原创 2015年11月19日 15:28:15
1、发送邮件配置文件springmail_config.xml
1、发送邮件配置文件springmail_config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="<a href="http://www.springframework.org/schema/beans" style="font-size: 14px;">http://www.springframework.org/schema/beans</a>"
       xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance" style="font-size: 14px;">http://www.w3.org/2001/XMLSchema-instance</a>"
       xsi:schemaLocation="<a href="http://www.springframework.org/schema/beans" style="font-size: 14px;">http://www.springframework.org/schema/beans</a>
       <a href="http://www.springframework.org/schema/beans/spring-beans.xsd" style="font-size: 14px;">http://www.springframework.org/schema/beans/spring-beans.xsd</a>"
>
   <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="smtp.qq.com" />
        <property name="port" value="25" />
        <property name="username" value="xxxx@qq.com" />
        <property name="password" value="xxxxxxx" />
        <property name="javaMailProperties">
          <props>
           <prop key="mail.smtp.auth">true</prop>
            <!-- 根据情况可进行设置 
            <prop key="mail.smtp.timeout">2500</prop>
           -->
                     </props> 
               </property> 
     </bean> 
</beans>
2、发送邮件类
package com.yihongyu.exec.javamail;

import java.io.File;

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

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

/**
 * SpringMail测试类
 * 
 * @author tzz
 * 
 */
public class SpringMailUtil {
	private ApplicationContext context = null;

	public SpringMailUtil() {
		context = new ClassPathXmlApplicationContext("classpath:META-INF/spring/springmail_config.xml");
	}

	// 简单邮件
	public void simpleSend() {
		JavaMailSender mailSender = (JavaMailSender) context.getBean("mailSender");
		SimpleMailMessage mail = new SimpleMailMessage();
		mail.setFrom("xxx@qq.com");
		mail.setTo("xxx@qq.com");
		mail.setSubject(" 测试spring Mail");
		mail.setText("你好,java");
		mailSender.send(mail);
	}

	// 带附件
	public void attachmentSend() {
		JavaMailSender mailSender = (JavaMailSender) context.getBean("mailSender");

		MimeMessage mime = mailSender.createMimeMessage();
		MimeMessageHelper helper;
		try {

			helper = new MimeMessageHelper(mime, true, "utf-8");
			helper.setFrom("xxx@qq.com");
			helper.setTo("xxx@qq.com");
			helper.setSubject("测试spring Mail附件");
			// 需要将附件显示在html中
			helper.setText("你好,java", true);
			FileSystemResource attachment = new FileSystemResource(new File("E:\\Test2.doc"));
			helper.addAttachment(MimeUtility.encodeWord("测试.doc"), attachment);// 解决附件中文编码问题

			mailSender.send(mime);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	// 多附件
	public void moreAttachmentSend() {
		JavaMailSender mailSender = (JavaMailSender) context.getBean("mailSender");

		MimeMessage mime = mailSender.createMimeMessage();
		MimeMessageHelper helper;
		try {

			helper = new MimeMessageHelper(mime, true, "utf-8");
			helper.setFrom("xxxx@qq.com");
			helper.setTo("xxx@qq.com");
			helper.setSubject("测试spring Mail附件");
			// 需要将附件显示在html中
			helper.setText("你好,java", true);
			FileSystemResource attachment = new FileSystemResource(new File("E:\\zqt.sql"));
			helper.addAttachment("zqt.sql", attachment);
			FileSystemResource attachment2 = new FileSystemResource(new File("E:\\Test2.doc"));
			helper.addAttachment(MimeUtility.encodeWord("测试.doc"), attachment2);// 解决附件中文编码问题

			mailSender.send(mime);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	// 抄送
	public void copySend() {
		JavaMailSender mailSender = (JavaMailSender) context.getBean("mailSender");

		MimeMessage mime = mailSender.createMimeMessage();
		MimeMessageHelper helper;
		try {

			helper = new MimeMessageHelper(mime, true, "utf-8");
			helper.setFrom("xxxxx@qq.com");
			helper.setTo("xxxx@qq.com");
			helper.setCc("xxxx@qq.com");
			helper.setSubject("测试spring Mail附件");
			// 需要将附件显示在html中
			helper.setText("你好,java", true);
			FileSystemResource attachment = new FileSystemResource(new File("E:\\zqt.sql"));
			helper.addAttachment("zqt.sql", attachment);
			FileSystemResource attachment2 = new FileSystemResource(new File("E:\\Test2.doc"));
			helper.addAttachment(MimeUtility.encodeWord("测试.doc"), attachment2);// 解决附件中文编码问题

			mailSender.send(mime);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	// 多附件、多人发送/抄送
	public void moreUserSend() {
		JavaMailSender mailSender = (JavaMailSender) context.getBean("mailSender");

		MimeMessage mime = mailSender.createMimeMessage();
		MimeMessageHelper helper;
		try {

			helper = new MimeMessageHelper(mime, true, "utf-8");
			helper.setFrom("xxxxx@qq.com");
			helper.setTo("xxxxx@qq.com");// 发送
			// helper.setCc("xxxxxx@qq.com");//抄送
			// helper.setTo(new InternetAddress[] { new InternetAddress("xxxxx@qq.com"),
			// new InternetAddress("xxxx@qq.com") });
			helper.setCc(new InternetAddress[] { new InternetAddress("xxxxxx@qq.com"),
					new InternetAddress("xxxxxx@qq.com") });
			helper.setSubject("测试spring Mail附件");
			// 需要将附件显示在html中
			helper.setText("你好,java", true);
			FileSystemResource attachment = new FileSystemResource(new File("E:\\zqt.sql"));
			helper.addAttachment("zqt.sql", attachment);
			FileSystemResource attachment2 = new FileSystemResource(new File("E:\\Test2.doc"));
			helper.addAttachment(MimeUtility.encodeWord("测试.doc"), attachment2);// 解决附件中文编码问题

			mailSender.send(mime);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		SpringMailUtil springMailUtil = new SpringMailUtil();
		// 简单邮件
		// springMailUtil.simpleSend();
		// 附件
		// springMailUtil.attachmentSend();
		// 多附件
		// springMailUtil.moreAttachmentSend();
		// 抄送
		// springMailUtil.copySend();
		// 多附件、多人发送/抄送
		springMailUtil.moreUserSend();
		System.out.println("发送成功");

	}
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

spring集成邮件服务

  • 2012年04月20日 20:31
  • 3.18MB
  • 下载

Spring mail 邮件服务及其参数配置(properties文件的读写)

一个Web 系统通常会少不了邮件服务的,比如用于注册,密码找回,订单提醒等应用场景。Spring 封装了一个简单易用的关于邮件发送的工具类JavaMailSenderImpl 。系统要提供邮件服务,那...

Spring boot 集成邮件服务

添加依赖 org.springframework.boot spring-boot-starter-mail 添加配置文件stmp.host=smtp.163.com stmp.acc...

Spring boot 集成JavaMail服务发送邮件

JavaMail是SUN提供给广大Java开发人员的一款邮件发送和接受的一款开源类库,支持常用的邮件协议,如:SMTP、POP3、IMAP,开发人员使用JavaMail编写邮件程序时,不再需要考虑底层...

为你的spring cloud微服务添加宕机邮件通知

在恶劣线上环境中,我们最担心的是服务挂掉,每当这个时候,运维就想吃柠檬(CNM)了,一些运维工具也可以实现这个功能,但是,既然用了spring cloud,就可以享受它带来的福利吧。 此功能基于sp...

java spring 开启SMTP服务发送QQ邮件

开通QQ邮箱的SMTP服务,然后再Spring使用587端口发送邮件,也有javax.mail的例子

转载:Spring Boot (十):邮件服务

springboot仍然在狂速发展,才五个多月没有关注,现在看官网已经到1.5.3.RELEASE版本了。准备慢慢在写写springboot相关的文章,本篇文章使用springboot最新版本1.5....

Spring 邮件服务:Spring+Javamail+Spring SMTP Mail+Maven

继续昨天的学习(详细见javamail实现发送邮件并简单实现观察者模式),昨天只是简单的用javamail来发送邮件,需要自己进行一些封装,实际应用到项目中可能封装的会更多,由于项目中将要采用Spri...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Spring邮件服务
举报原因:
原因补充:

(最多只允许输入30个字)