包括: 一个配置文件, 一个继承了java.util.TimerTask 的类:TimerMail.java
一 . 配置文件中使用了两个类(还可以使用更多的 类,如:org.springframework.mail.MailMessage 邮件模板.
1. org.springframework.scheduling.timer.TimerFactoryBean
2. org.springframework.scheduling.timer.ScheduledTimerTask
<beans>
<bean id="springSendMail" class="com.yidwo.app.springMail.SpringSendMail">
<property name="mailSender">
<ref bean="mailSender" />
</property>
</bean>
<!-- 定时任务 -->
<bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<ref bean="springTimerTask" />
</list>
</property>
</bean>
<bean id="springTimerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="timerTask">
<ref bean="springSendMail" />
</property>
<property name="delay">
<value>10000</value>
</property>
<property name="period">
<value>3600000</value> <!-- 区间要定在一小时 3600000-->
</property>
</bean>
<!-- 邮件发送模板 -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp解析地址" />
<property name="port" value="25" />
<property name="username" value="username" />
<property name="password" value="password" />
<!-- 如果要使用用户名和密码验证,这一步需要 -->
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
</props>
</property>
</bean>
</beans>
实现的类代码如下:
package com.yidwo.app.springMail;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimerTask;
import java.util.Locale;
import java.io.ByteArrayOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import org.apache.log4j.Logger;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import freemarker.template.Template;
import freemarker.template.Configuration;
/**
* 报表邮件发送类
*
* @author Yinghe Lai
*
*/
public class SpringSendMail extends TimerTask {
private static final Logger logger = Logger.getLogger(SpringSendMail.class);
public SpringSendMail() {}
private JavaMailSenderImpl mailSender;
public JavaMailSenderImpl getMailSender() {
return mailSender;
}
public void setMailSender(JavaMailSenderImpl mailSender) {
this.mailSender = mailSender;
}
public void run() {
this.doEmail();
}
// 进行发送处理
private void doEmail() {
MimeMessage mimeMessage = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true,"utf-8");
helper.setTo("接收邮箱"); // 设置接收邮箱 邮箱必须符合邮箱格式
// 设置发送方信息,邮箱必须符合邮箱格式
helper.setFrom("置发送邮箱", "发送人别名");
helper.setSubject("设置标题"); // 设置标题
// list 为其它程序传入, 用于动态填充模板.
String fmStr = this.doFreemarkerMail(this.doFreemarkerMail(List list));
helper.setText("这是您设置接收的报表邮件,请查收.",fmStr);
mailSender.send(mimeMessage);
helper = null;
System.out.println("*** The required mail has been successfully sended ***");
} catch (Exception e) {
logger.debug("sendException: "+e.getMessage());
e.printStackTrace();
}
}
// 构造freemarker动态邮件
// 只要由其它方法传入一个List结构的数据就可.返回邮件内容
private String doFreemarkerMail(List list) throws Exception{
Map maplist = new HashMap();
maplist.put("mapList", list);
Configuration freemarker_cfg = new Configuration();
freemarker_cfg.setEncoding(Locale.getDefault(), "utf-8");
// 查找并加载freemarker模板文件.
freemarker_cfg.setClassForTemplateLoading(this.getClass(), "\\com\\yidwo\\app\\springMail");
Template freetemplate = freemarker_cfg.getTemplate("mailTemplate.ftl");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Writer out = new OutputStreamWriter(bos);
freetemplate.process(mapList, out);
String content = bos.toString();
out.close();
return content;
}
// 返回List 用于动态更新模板文件
private List returnList(){
List list = new ArrayList();
return list;
}
邮件开发中的异常:
1. javax.mail.MessagingException: Could not connect to SMTP host: www.d1888.com, port: 25, response: -1
都是配置host时出错.要不没有此host的.要不服务解析时没有对应到邮件服务器.
2. org.springframework.mail.MailAuthenticationException: Authentication failed; nested exception is javax.mail.AuthenticationFailedException
是邮箱用户名或密码出错导致.
3. com.sun.mail.smtp.SMTPSendFailedException: 503 input error.
是setFrom时设置的邮箱出错,这里的设置要符合邮箱格式.(user@websit.com).