Spring Quartz结合Spring mail定期发送邮件

文件配置如下:



    
		
	
   
   		
   			${host}
   		
   		
   			${username}
   		
   		
   			${password}
   		
   		    
   		   		
   		   			true
   		   			25000
   		   		
   		    
   
    
    


 

spring-quartz2.xml



      

	


 

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;
@Component
public 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("[email protected]");
		smm.setFrom("[email protected]");
		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("[email protected]");//接受者
			 messageHelper.setFrom("[email protected]");//发送者  
			 messageHelper.setSubject("测试邮件");//主题  
			 //邮件内容,注意加参数true,表示启用html格式  
			 messageHelper.setText("

hello!!chao.wang

BaBY",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("[email protected]");//接受者 messageHelper.setFrom("[email protected]");//发送者 messageHelper.setSubject("测试邮件");//主题 messageHelper.setText("

hello!!chao.wang

",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(); } } }


 

邮件发送带附件存在问题。

 

 

 

你可能感兴趣的:(spring)