Spring邮件服务
来源:互联网 发布:软件系统测试报告用途 编辑:程序博客网 时间:2024/06/06 07:18
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
- Spring配置邮件服务
- spring邮件服务
- Spring邮件服务
- Spring boot 集成邮件服务
- Spring最佳实践-9.1 集成邮件服务
- spring中配置邮件发送服务
- Spring boot 集成JavaMail服务发送邮件
- 转载:Spring Boot (十):邮件服务
- Spring 邮件服务:Spring+Javamail+Spring SMTP Mail+Maven
- 邮件服务
- 邮件服务
- 邮件服务
- 邮件服务
- 邮件服务
- 邮件服务
- 邮件服务
- 邮件服务
- java spring 开启SMTP服务发送QQ邮件
- spark shuffle过程分析
- 项目导入后出现The import javax.servlet cannot be resolved报错怎么办
- android edittext限制输入小数位数,限制输入数字大小
- Android5.0(lollipop)新特性介绍(一)
- 集合框架-----LinkedList中的一些特有方法
- Spring邮件服务
- java sftp文件上传核心代码
- swift-使用NSURLConnection进行数据的提交和获取(POST与GET)
- android Activity与Fragment之间的通信
- 一个很详细的web.xml讲解
- Python在线爆破邮箱账号密码测试代码(亲测可用)
- 修改注册表添加IE信任站点及启用Activex控件
- Monkey测试简介
- input file