当前位置: 首页 > 图灵资讯 > 技术篇> spring和quartz进行定时邮件发送 (转)

spring和quartz进行定时邮件发送 (转)

来源:图灵教育
时间:2023-04-20 16:56:50

  一 发送邮件的基类:package com.spring.mail; import org.springframework.mail.MailSender;//import org.springframework.mail.javamail.JavaMailSender; public abstract class BaseMailSender { protected String to; protected String from; protected String subject; protected MailSender sender; ///当发送的邮件是简单邮件时,使用MailSender; //protected JavaMailSender sender; ///当发送的邮件为MIMI时,使用JavamailSender public void setTo(String to) { this.to = to; } public void setFrom(String from) { this.from = from; } public void setSubject(String subject) { this.subject = subject; } public void setJavaMailSender (MailSender sender) {//(JavaMailSender sender) { this.sender = sender; }}

  发送电子邮件调用类: package com.spring.mail; import javax.mail.MessagingException; import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.mail.SimpleMailMessage; public class SimpleHtmlMailSender extends BaseMailSender{ public void sendMessage() throws MessagingException { String textStr = "Helo!!!!!!!!!!!!";////发送电子邮件的消息主体 SimpleMailMessage msg = new SimpleMailMessage(); msg.setTo(to); msg.setFrom(from); msg.setSubject(subject); msg.setText(textStr); sender.send(msg); } public void doIt() throws Exception { ClassPathXmlApplicationContext ctx1 = new ClassPathXmlApplicationContext( new String[] { "ApplicationContext.xml" }); SimpleHtmlMailSender sender = (SimpleHtmlMailSender) ctx1.getBean("messageSender"); sender.sendMessage(); System.out.println(“发送成功”); }}

  applicationtiontextetion配置文件: smtp.163.com tomcat0506 wes true wes830506@126.com tomcat0506@163.com test doIt 3000 6000 0 0 6,12,20 * * ?

  二 发送mimi邮件 在编写和发送邮件程序时,有几个类别需要使用: org.springframework.mail.javamail.JavaMailSender; ////这种类型继承了spring ///MailSender接口-定义了两种简单的Sender方法,用于发送简单的邮件信息,参数类型为:SimpleMailMessage([]) 继承上述方法后,//JavamailSender类支持MIMI邮件,MIMI消息可以发送 org.springframework.context.support.ClassPathXmlApplicationContext; ///在spring中读取配置文件时需要使用的类别 org.springframework.mail.javamail.MimeMessageHelper; ////发送MIMI消息时需要使用的辅助类 javax.mail.MessagingException; javax.mail.internet.MimeMessage; ///J2EE支持发送MIME信息所需的类例:发送电子邮件的基本BaseMailSender .java: import org.springframework.mail.javamail.JavaMailSender;public abstract class BaseMailSender { protected String to; protected String from; protected String subject; protected JavaMailSender sender;////发送MIMI消息时使用的类别 public void setTo(String to) { this.to = to; } public void setFrom(String from) { this.from = from; } public void setSubject(String subject) { this.subject = subject; } public void setJavaMailSender(JavaMailSender sender) { this.sender = sender; }}

  邮件发送类: package com.spring.mail;import javax.mail.MessagingException;import javax.mail.internet.MimeMessage;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.mail.SimpleMailMessage;import org.springframework.mail.javamail.MimeMessageHelper;public class SimpleHtmlMailSender extends BaseMailSender{ public void sendMessage() throws MessagingException { //String htmlHead = "

"; //String htmlBody = ""; //String htmlEnd = ""; // StringBuffer mailMessage = new StringBuffer(); //mailMessage.append(htmlHead); //mailMessage.append(htmlBody); //mailMessage.append(htmlEnd); ///当发送的电子邮件是简单的电子邮件时,注销这句话 String textStr = "

^_^!

"; //String textStr = "Helo could you see this email?";////发送电子邮件的消息主体 //SimpleMailMessage msg = new SimpleMailMessage(); ///当邮件发送为简单邮件时,注销此句话 MimeMessage msg = sender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(msg, true, "GB2312"); helper.setTo(to); helper.setFrom(from); helper.setSubject(subject); helper.setText(textStr, true); /* msg.setTo(to); msg.setFrom(from); msg.setSubject(subject); msg.setText(textStr); */ sender.send(msg); } public void doIt() throws Exception{//static void main(String[] args) throws Exception {// //ApplicationContext ctx = new FileSystemXmlApplicationContext(new String[] { "springexample-creditaccount.xml" }); ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( new String[] { "ApplicationContext.xml" }); SimpleHtmlMailSender sender = (SimpleHtmlMailSender) ctx .getBean("messageSender"); sender.sendMessage(); System.out.println(“发送成功”); }}

 

  配置文件同上!