首页 > 代码库 > Spring Quartz结合Spring mail定期发送邮件
Spring Quartz结合Spring mail定期发送邮件
文件配置如下:
<?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" > <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value=http://www.mamicode.com/"classpath:mail.properties" />>
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{ @Autowired private 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(); } } }
邮件发送带附件存在问题。
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。