Spring Quartz结合Spring mail定期发送邮件
来源:互联网 发布:中华家长 知乎 编辑:程序博客网 时间:2024/06/03 18:49
文件配置如下:
<?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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" > <beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location" value="classpath:mail.properties" /></bean> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" > <value>${host}</value> </property> <property name="username" > <value>${username}</value> </property> <property name="password" > <value>${password}</value> </property> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">true</prop> <prop key="mail.smtp.timeout">25000</prop> </props> </property> </bean> <import resource="spring-quartz2.xml"/> <context:component-scan base-package="com.study"/></beans>
spring-quartz2.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:context="http://www.springframework.org/schema/context"xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd " > <task:annotation-driven/> </beans>
package com.study;import java.io.File;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeUtility;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.mail.SimpleMailMessage;import org.springframework.mail.javamail.JavaMailSender;import org.springframework.mail.javamail.JavaMailSenderImpl;import org.springframework.mail.javamail.MimeMessageHelper;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;import org.springframework.web.context.ServletContextAware;@Componentpublic class QuartzJob{@Autowiredprivate JavaMailSender jms;private SimpleMailMessage smm;private MimeMessage mailMsg;public QuartzJob() throws ServletException{//initSimpleMailMSG();//initHTMLMailMSG();initHTMLWithAttachMailMsg();System.out.println("Quartzjob创建成功");}@Scheduled(cron = "0/1 * * * * ? ")public void run(){System.out.println("Quartz执行的任务调度发送邮件");try {//jms.send(smm);jms.send(mailMsg);} catch (Exception e) {e.printStackTrace();}}private void initSimpleMailMSG(){//发送简单邮件smm = new SimpleMailMessage();smm.setTo("253503125@qq.com");smm.setFrom("hbzhongqian@163.com");smm.setSubject("测试邮件");smm.setText("springMail的简单测试发送邮件");}private void initHTMLMailMSG(){//发送HTML格式的邮件 JavaMailSenderImpl senderImpl = new JavaMailSenderImpl(); mailMsg = senderImpl.createMimeMessage(); try { MimeMessageHelper messageHelper = new MimeMessageHelper(mailMsg,true,"utf-8"); messageHelper.setTo("253503125@qq.com");//接受者 messageHelper.setFrom("hbzhongqian@163.com");//发送者 messageHelper.setSubject("测试邮件");//主题 //邮件内容,注意加参数true,表示启用html格式 messageHelper.setText("<html><head></head><body><h1>hello!!chao.wang</h1><font color='red'>BaBY</font></body></html>",true); } catch (Exception e) {e.printStackTrace();}}private void initHTMLWithAttachMailMsg(){//发送带附件的邮件JavaMailSenderImpl senderImpl = new JavaMailSenderImpl(); mailMsg = senderImpl.createMimeMessage(); try { MimeMessageHelper messageHelper = new MimeMessageHelper(mailMsg,true,"utf-8"); messageHelper.setTo("253503125@qq.com");//接受者 messageHelper.setFrom("hbzhongqian@163.com");//发送者 messageHelper.setSubject("测试邮件");//主题 messageHelper.setText("<html><head></head><body><h1>hello!!chao.wang</h1></body></html>",true); //附件内容 messageHelper.addInline("a", new File("E:/xiezi.png"));// messageHelper.addInline("b", new File("E:/logo.png")); // 这里的方法调用和插入图片是不同的,使用MimeUtility.encodeWord()来解决附件名称的中文问题// messageHelper.addAttachment(MimeUtility.encodeWord(file.getName()), file); } catch (Exception e) {e.printStackTrace();}}}
邮件发送带附件存在问题。
0 0
- Spring Quartz结合Spring mail定期发送邮件
- Spring Mail邮件发送
- spring mail发送邮件
- spring mail 发送邮件
- Spring mail 发送邮件
- spring mail 发送邮件
- Spring mail 发送邮件
- Spring mail 发送邮件
- Spring Mail实现发送邮件
- 使用Spring Mail发送邮件
- Spring mail实现邮件发送
- spring mail简单邮件发送
- spring mail发送TLS邮件
- Spring的Mail邮件发送
- spring-boot mail 邮件发送
- spring+mail+maven(spring 发送邮件)
- 通过Spring Mail Api发送邮件
- 通过Spring Mail Api发送邮件
- 关于单个字符怎么转换成字符串的问题
- 2014百度之星资格赛——1004 Labyrinth(dp)
- Android URL encode 空格处理
- USACO 2007 公开赛(银组)
- 一个函数中返回多个值
- Spring Quartz结合Spring mail定期发送邮件
- 对一次笔试写过小结
- stm32 看门狗
- Java内部类理解
- C++经典开源项目
- 【剑指offer】面试题10:二进制中1的个数
- A4纸的屏幕比列
- linux vimrc配置
- 互信息概念与定理