一 发送邮件的基类:
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("发送成功");
}
}
配置文件ApplicationContext:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "
http://www.springframework.org/dtd/spring-beans.dtd
">
<beans>
<bean id="sender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host">
<value>smtp.163.com</value>
</property>
<property name="username">
<value>tomcat0506</value>
</property>
<property name="password">
<value>wes</value>
</property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
</props>
</property>
</bean>
<bean id="messageSender" class="com.spring.mail.SimpleHtmlMailSender">
<property name="javaMailSender">
<ref bean="sender"/>
</property>
<property name="to">
<value>wes830506@126.com</value>
</property>
<property name="from">
<value>tomcat0506@163.com</value>
</property>
<property name="subject">
<value>test</value>
</property>
</bean>
<bean id="methodInvokingJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- <property name="jobClass">
<value>com.spring.mail.SimpleHtmlMailSender</value>
</property>-->
<property name="targetObject"><ref bean="messageSender"/></property>
<property name="targetMethod"><value>doIt</value></property>
</bean>
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail"><ref bean="methodInvokingJobDetail"/></property>
<property name="startDelay">
<!-- 3 seconds -->
<value>3000</value>
</property>
<property name="repeatInterval">
<!-- repeat every 6 seconds -->
<value>6000</value>
</property>
</bean>
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail"> <ref bean="methodInvokingJobDetail"/></property>
<property name="cronExpression"> <value>0 0 6,12,20 * * ?</value> </property>
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref local="simpleTrigger"/>
</list>
</property>
</bean>
</beans>
二 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 = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\"></head><body>";
//String htmlBody = "";
//String htmlEnd = "";
// StringBuffer mailMessage = new StringBuffer();
//mailMessage.append(htmlHead);
//mailMessage.append(htmlBody);
//mailMessage.append(htmlEnd);
//当发送的邮件为简单邮件时注销此句
String textStr = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\"></head><body><h1><a href='#'>^_^!</a></h1></body></html>";
//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("发送成功");
}
}
配置文件同上!