zoukankan      html  css  js  c++  java
  • 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="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;
    @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("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();
    		}
    	}
    	
    }
    


     

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

  • 相关阅读:
    Webpack 学习笔记总结
    Ctrl+C和Ctrl+V无法使用
    mysql默认字符集问题
    Makefile 简述
    Shell编程学习之重定向
    Shell编程学习之Shell编程基础(一)
    Linux系统目录
    关于Linux部分版本无法安装Chrome的问题
    整数算术溢出问题的分析
    Linux中的/etc/nologin问题
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/3782456.html
Copyright © 2011-2022 走看看