关闭

Spring邮件服务

标签: springemail
283人阅读 评论(0) 收藏 举报
分类:
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("发送成功");

	}
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:151540次
    • 积分:1376
    • 等级:
    • 排名:千里之外
    • 原创:35篇
    • 转载:2篇
    • 译文:0篇
    • 评论:12条
    文章分类
    最新评论