最近项目中使用到了spring javamail进行定时邮件发送,网上的例子质量参差不齐,说的不是很清楚,这里简单的介绍自己写的程序,一是与大家分享,另外做为笔记供自己以后参考。
- <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
- <property name="host" value="${metrix.smtp.server}" />
- <property name="port" value="${metrix.smtp.port}" />
- <property name="username" value="${metrix.smtp.user}" />
- <property name="password" value="${metrix.smtp.password}" />
- <property name="javaMailProperties">
- <props>
- <prop key="mail.smtp.auth">${metrix.smtp.auth}</prop>
- <prop key="mail.smtp.timeout">${metrix.smtp.timeout}</prop>
- </props>
- </property>
- </bean>
- <bean id="mimeMessageHelperAdapter" class="com.alibaba.corp.metrix.admin.biz.dataprocess.utils.MimeMessageHelperAdapter">
- <property name="from" value="${metrix.smtp.from.address}"/>
- <property name="tos" value="${metrix.smtp.to.address}"/>
- <property name="personal" value="${metrix.smtp.from.personal}"/>
- </bean>
如果你的smtp邮件服务器不需要认证,可以将properties中mail.smtp.auth设置为false.
- package com.alibaba.corp.metrix.admin.biz.dataprocess.utils;
- /**
- * Copyright 1999-2100 Alibaba.com All right reserved. This software is the
- * confidential and proprietary information of Alibaba.com
- * ("Confidential Information"). You shall not disclose such Confidential
- * Information and shall use it only in accordance with the terms of the license
- * agreement you entered into with Alibaba.com.
- *
- * @Author: leon Mao
- * @Date: 07/13/2012
- * @Time: 11:49
- * @Description:
- */
- import org.apache.commons.lang.StringUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- public class MimeMessageHelperAdapter {
- Logger logger = LoggerFactory.getLogger(MimeMessageHelperAdapter.class);
- private String from;
- private String tos;
- private String personal;
- public String getFrom() {
- return from;
- }
- public void setFrom(String from) {
- this.from = from;
- }
- public String getTos() {
- return tos;
- }
- public String[] getTosArray() {
- if (this.tos != null) {
- return StringUtils.split(this.tos, ',');
- } else {
- logger.warn("email receiver(s) not configured yet!");
- return null;
- }
- }
- public void setTos(String tos) {
- this.tos = tos;
- }
- public String getPersonal() {
- return personal;
- }
- public void setPersonal(String personal) {
- this.personal = personal;
- }
- }
- package com.alibaba.corp.metrix.admin.biz.dataprocess.utils;
- import java.io.UnsupportedEncodingException;
- import javax.mail.MessagingException;
- import javax.mail.internet.MimeMessage;
- import org.springframework.mail.javamail.JavaMailSenderImpl;
- import org.springframework.mail.javamail.MimeMessageHelper;
- import com.alibaba.corp.metrix.admin.biz.dataprocess.job.base.BeanFactoryUtil;
- /**
- * Copyright 1999-2100 Alibaba.com All right reserved. This software is the
- * confidential and proprietary information of Alibaba.com
- * ("Confidential Information"). You shall not disclose such Confidential
- * Information and shall use it only in accordance with the terms of the license
- * agreement you entered into with Alibaba.com.
- *
- * @Author: leon Mao
- * @Date: 07/13/2012
- * @Time: 12:01
- * @Description:
- */
- public class MailSenderUtil {
- public static void sendMail(String subject, String content, Boolean html, String[] tos)
- throws MessagingException, UnsupportedEncodingException {
- JavaMailSenderImpl javaMailSender = (JavaMailSenderImpl) BeanFactoryUtil
- .getBean("mailSender");
- MimeMessageHelperAdapter mimeMessageHelperAdapter = (MimeMessageHelperAdapter) BeanFactoryUtil
- .getBean("mimeMessageHelperAdapter");
- MimeMessage message = javaMailSender.createMimeMessage();
- MimeMessageHelper helper = new MimeMessageHelper(message, true, "GBK");
- helper.setFrom(mimeMessageHelperAdapter.getFrom(), mimeMessageHelperAdapter.getPersonal());
- helper.setSubject(subject);
- if (tos == null || tos.length == 0) {
- helper.setTo(mimeMessageHelperAdapter.getTosArray());
- } else {
- helper.setTo(tos);//推送Job中meta-info中设定的邮件接收者
- }
- helper.setText(content, html);
- javaMailSender.send(message);
- }
- }
这里sendMail方法中的html参数定义mime类型,这样outlook等邮件客户端会将收到的内容自动以html解析。
- public class MailContentGenerator {
- public static final String PREFIX = "classpath:result/";
- public static String generateMailContent(List results, String vmName) throws IOException {
- VelocityContext context = new VelocityContext();
- context.put("results", results);
- Reader reader = new InputStreamReader(Resources.getResourceAsStream(PREFIX + vmName),
- "GBK");
- StringWriter writer = new StringWriter(1024 * 512);
- Velocity.evaluate(context, writer, "", reader);
- return writer.toString();
- }
- }
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=GBK"/>
- <style type="text/css">
- thead tr {
- background-color: ActiveCaption;
- color: CaptionText;
- }
- th, td {
- vertical-align: top;
- font-family: "Tahoma";
- font-size: 10pt;
- padding: 3px;
- }
- table, th, td {
- border: 1px solid silver;
- }
- table {
- border-collapse: collapse;
- }
- </style>
- </head>
- <body>
- <div style="font-size:12pt;">
- <table>
- <tbody>
- <tr>
- <th>序号</th>
- <th>标题</th>
- <th>产品线</th>
- <th>团队</th>
- <th>开发</th>
- <th>QA</th>
- <th>PE</th>
- <th>发布包</th>
- <th>上线手册</th>
- <th>上线时间</th>
- </tr>
- #foreach ($result in $results)
- <tr>
- <td>$velocityCount</td>
- #foreach ($element in $result)
- <td>$!element</td>
- #end
- </tr>
- #end
- </tbody>
- </table>
- </div>
- </body>
- </html>
spring、misc.mail、java.servlet、jakarta.velocity