锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
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;
聽//褰撳彂閫佺殑閭歡涓虹畝鍗曢偖浠舵椂鐢∕ailSender鍗沖彲;
聽
聽//protected JavaMailSender sender;
聽//褰撳彂閫佺殑閭歡涓篗IMI鏃剁敤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閭歡鍙戦?/strong>
渚嬪瓙:
鍙戦侀偖浠剁殑鍩虹被
BaseMailSender .java:
import org.springframework.mail.javamail.JavaMailSender;
public abstract class BaseMailSender {
聽protected String to;
聽protected String from;
聽protected String subject;
聽protected JavaMailSender sender;//褰撳彂閫丮IMI娑堟伅鏃剁敤鍒扮殑綾?/p>
聽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("鍙戦佹垚鍔?);
聽}
}
閰嶇疆鏂囦歡鍚屼笂!