1. Email模型实体类
package com.spring.email; import java.io.File; import java.util.Date; public class MailBean { //发送邮件账号 private String from; //接收邮件账号集合 private String[] tos = {}; //密送邮件账号集合 private String[] bcc = {}; //抄送邮件账号集合 private String[] cc = {}; //回复地址 private String relpyTO; //文本或HTML private String text; //主题 private String subject; //日期 private Date date; //附件 private File[] files = {}; public String getFrom() { return from; } public void setFrom(String from) { this.from = from; } public String[] getTos() { return tos; } public void setTos(String[] tos) { this.tos = tos; } public String[] getBcc() { return bcc; } public void setBcc(String[] bcc) { this.bcc = bcc; } public String[] getCc() { return cc; } public void setCc(String[] cc) { this.cc = cc; } public String getRelpyTO() { return relpyTO; } public void setRelpyTO(String relpyTO) { this.relpyTO = relpyTO; } public String getText() { return text; } public void setText(String text) { this.text = text; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public Date getDate() { if(date==null){date = new Date();} return date; } public void setDate(Date date) { this.date = date; } public File[] getFiles() { return files; } public void setFiles(File[] files) { this.files = files; } }
2. 邮件处理类
package com.spring.email; import java.io.File; import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Map; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeUtility; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.ui.freemarker.FreeMarkerTemplateUtils; import freemarker.template.Configuration; import freemarker.template.Template; public class Mail { /** * java 发送邮件 * org.springframework.mail.javamail.JavaMailSender 接口 */ private JavaMailSender mailSender; /** * FreeMarker 配置 */ private Configuration config; /** * 获取 * 使用模板发邮件 * @param fileName 模板名称 * @param molde 动态对象 * @return 模板 文本、HTML * @throws Exception */ public String getTemplateText(String fileName,Object... molde) throws Exception{ Map<String,Object> map = new HashMap<String,Object>(); for (Object object : molde) { String name = object.getClass().getSimpleName(); name = name.replaceFirst(name.substring(0, 1), name.substring(0, 1).toLowerCase()); map.put(name,object); } Template template = config.getTemplate(fileName); return FreeMarkerTemplateUtils.processTemplateIntoString(template, map); } /** * 发送邮件 * @param bean 邮件属性 * @throws MessagingException * @throws UnsupportedEncodingException */ public void send(MailBean bean) throws MessagingException, UnsupportedEncodingException{ MimeMessage msg = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(msg,true,"UTF-8"); msg.setSubject(bean.getSubject()); helper.setSentDate(bean.getDate()); helper.setText(bean.getText()); helper.setReplyTo(bean.getRelpyTO()); helper.setFrom(bean.getFrom()); for (File file : bean.getFiles()) { helper.addAttachment(MimeUtility.encodeWord(file.getName()), file); } for (String bcc : bean.getBcc()) { helper.addBcc(bcc); } for (String cc : bean.getCc()) { helper.addBcc(cc); } for (String to : bean.getTos()) { helper.addTo(to); } mailSender.send(msg); } /** * 发送邮件 HTML 格式 * @param bean 邮件属性 * @throws MessagingException * @throws UnsupportedEncodingException */ public void sendHTML(MailBean bean) throws MessagingException, UnsupportedEncodingException{ MimeMessage msg = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(msg,true,"UTF-8"); msg.setSubject(bean.getSubject()); helper.setSentDate(bean.getDate()); helper.setText(bean.getText(),true); helper.setReplyTo(bean.getRelpyTO()); helper.setFrom(bean.getFrom()); for (File file : bean.getFiles()) { helper.addAttachment(MimeUtility.encodeWord(file.getName()), file); } for (String bcc : bean.getBcc()) { helper.addBcc(bcc); } for (String cc : bean.getCc()) { helper.addBcc(cc); } for (String to : bean.getTos()) { helper.addTo(to); } mailSender.send(msg); } /** * 发送邮件 HTML模板 格式 * @param bean 邮件属性 * @throws MessagingException * @throws UnsupportedEncodingException */ public void sendTemplate(MailBean bean,String fileName,Object... molder) throws Exception{ MimeMessage msg = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(msg,true,"UTF-8"); msg.setSubject(bean.getSubject()); helper.setSentDate(bean.getDate()); helper.setText(getTemplateText(fileName,molder),true); helper.setReplyTo(bean.getRelpyTO()); helper.setFrom(bean.getFrom()); for (File file : bean.getFiles()) { helper.addAttachment(MimeUtility.encodeWord(file.getName()), file); } for (String bcc : bean.getBcc()) { helper.addBcc(bcc); } for (String cc : bean.getCc()) { helper.addBcc(cc); } for (String to : bean.getTos()) { helper.addTo(to); } mailSender.send(msg); } public JavaMailSender getMailSender() { return mailSender; } public void setMailSender(JavaMailSender mailSender) { this.mailSender = mailSender; } public Configuration getConfig() { return config; } public void setConfig(Configuration config) { this.config = config; } }
3. Spring 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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="port" value="25"/> <property name="protocol" value="smtp"/> <property name="host" value="smtp.qq.com"/> <property name="username" value="[email protected]"/> <property name="password" value="**********"/> <property name="defaultEncoding" value="utf-8"/> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">true</prop> </props> </property> </bean> <bean id="freeMarkerConfiguration" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean"> <property name="freemarkerSettings"> <props> <prop key="default_encoding">UTF-8</prop> <prop key="template_update_delay">600000</prop> </props> </property> <property name="templateLoaderPath" value="com/spring/freemaker"/> </bean> <bean class="com.spring.email.Mail"> <property name="mailSender" ref="javaMailSender"/> <property name="config" ref="freeMarkerConfiguration"/> </bean> <bean id="dateFormat" class="java.text.SimpleDateFormat"> <constructor-arg value="yyyy-MM-dd"/> </bean> </beans>
4. 发送邮件
package com.spring.email; import javax.mail.MessagingException; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringSendEmail { public static void main(String[] args) throws BeansException, MessagingException { try { BeanFactory factory = new ClassPathXmlApplicationContext("app*.xml"); Mail mail = factory.getBean(Mail.class); MailBean bean = factory.getBean(MailBean.class); mail.sendTemplate(bean, "registerTemplate.html"); System.out.println("success"); } catch (Exception e) { System.out.println(e.getMessage()); } } }