1、服务接口
2、服务接口实现
3、服务配置
- package com.*.email.service;
- /**
- * Description : 邮件相关服务接口.
- * Create Time : 2013年12月2日 上午10:08:42
- * Copyright : Copyright (c) 2010 - 2013 All rights reserved.
- *
- * @author Mr Yan
- * @version 1.0
- */
- public interface EmailService {
- /**
- * 发送邮件
- * @param mailto 收件邮箱
- * @param fromEmail 发送邮箱
- * @param text 内容
- * @param title 标题
- * @throws Exception
- */
- public void sendMail(String mailto, String fromEmail, String text, String title) throws Exception;
- /**
- * 批量下发邮件
- * @param mailto 收件邮箱
- * @param fromEmail 发送邮箱
- * @param text 内容
- * @param title 标题
- * @throws Exception
- */
- public void sendBatchMail(String[] mailto, String fromEmail, String text, String title) throws Exception;
- /**
- * 批量下发邮件待附件
- * @param mailto 收件邮箱
- * @param fromEmail 发送邮箱
- * @param text 内容
- * @param title 标题
- * @param filePath 附件
- * @throws Exception
- */
- public void sendBatchMailWithFile(String[] mailto, String fromEmail, String text, String title, String[] filePath) throws Exception;
- }
package com.*.email.service; /** * Description : 邮件相关服务接口. * Create Time : 2013年12月2日 上午10:08:42 * Copyright : Copyright (c) 2010 - 2013 All rights reserved. * * @author Mr Yan * @version 1.0 */ public interface EmailService { /** * 发送邮件 * @param mailto 收件邮箱 * @param fromEmail 发送邮箱 * @param text 内容 * @param title 标题 * @throws Exception */ public void sendMail(String mailto, String fromEmail, String text, String title) throws Exception; /** * 批量下发邮件 * @param mailto 收件邮箱 * @param fromEmail 发送邮箱 * @param text 内容 * @param title 标题 * @throws Exception */ public void sendBatchMail(String[] mailto, String fromEmail, String text, String title) throws Exception; /** * 批量下发邮件待附件 * @param mailto 收件邮箱 * @param fromEmail 发送邮箱 * @param text 内容 * @param title 标题 * @param filePath 附件 * @throws Exception */ public void sendBatchMailWithFile(String[] mailto, String fromEmail, String text, String title, String[] filePath) throws Exception; }
2、服务接口实现
- package com.*.email.service.impl;
- import java.util.ArrayList;
- import java.util.List;
- import javax.activation.DataHandler;
- import javax.activation.FileDataSource;
- import javax.annotation.Resource;
- import javax.mail.BodyPart;
- import javax.mail.Message;
- import javax.mail.Multipart;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeBodyPart;
- import javax.mail.internet.MimeMessage;
- import javax.mail.internet.MimeMultipart;
- import javax.mail.internet.MimeUtility;
- import org.springframework.mail.javamail.JavaMailSenderImpl;
- import org.springframework.mail.javamail.MimeMessageHelper;
- import org.springframework.stereotype.Service;
- import com.*.email.service.EmailService;
- /**
- * Description : 邮件相关服务接口实现.
- * Create Time : 2013年12月2日 上午10:10:25
- * Copyright : Copyright (c) 2010 - 2013 All rights reserved.
- *
- * @author Mr Yan
- * @version 1.0
- */
- @Service
- public class EmailServiceImpl implements EmailService{
- @Resource(name = "javaMailSender")
- private JavaMailSenderImpl javaMailSender;
- class EmailThread extends Thread {
- private final MimeMessage mimeMessage;
- public EmailThread(MimeMessage mimeMessage) {
- this.mimeMessage = mimeMessage;
- }
- @Override
- public void run() {
- javaMailSender.send(mimeMessage);
- }
- }
- @Override
- public void sendMail(String mailto, String fromEmail, String text, String title) throws Exception {
- fromEmail = javaMailSender.getUsername();
- MimeMessage mimeMessage = javaMailSender.createMimeMessage();
- MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
- messageHelper.setFrom(new InternetAddress(fromEmail));
- messageHelper.setSubject(title);
- messageHelper.setText(text, true);
- messageHelper.setTo(new InternetAddress(mailto));
- mimeMessage = messageHelper.getMimeMessage();
- EmailThread et = new EmailThread(mimeMessage);
- et.start();
- }
- @Override
- public void sendBatchMail(String[] mailto, String fromEmail, String text, String title) throws Exception {
- MimeMessage mimeMessage = javaMailSender.createMimeMessage();
- MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
- messageHelper.setFrom(new InternetAddress(fromEmail));
- messageHelper.setSubject(title);
- messageHelper.setText(text, true);
- List<InternetAddress> list = new ArrayList<InternetAddress>();
- for (int i = 0; i < mailto.length; i++) {
- list.add(new InternetAddress(mailto[i]));
- }
- InternetAddress[] address = list.toArray(new InternetAddress[list.size()]);
- mimeMessage.setRecipients(Message.RecipientType.TO, address);
- mimeMessage = messageHelper.getMimeMessage();
- EmailThread et = new EmailThread(mimeMessage);
- et.start();
- }
- @Override
- public void sendBatchMailWithFile(String[] mailto, String fromEmail, String text, String title, String[] filePath) throws Exception {
- MimeMessage mimeMessage = javaMailSender.createMimeMessage();
- MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
- messageHelper.setFrom(new InternetAddress(MimeUtility.encodeText(fromEmail)));
- messageHelper.setSubject(title);
- if (filePath != null) {
- BodyPart mdp = new MimeBodyPart();// 新建一个存放信件内容的BodyPart对象
- mdp.setContent(text, "text/html;charset=UTF-8");// 给BodyPart对象设置内容和格式/编码方式
- Multipart mm = new MimeMultipart();// 新建一个MimeMultipart对象用来存放BodyPart对象
- mm.addBodyPart(mdp);// 将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)
- // 把mm作为消息对象的内容
- MimeBodyPart filePart;
- FileDataSource filedatasource;
- // 逐个加入附件
- for (int j = 0; j < filePath.length; j++) {
- filePart = new MimeBodyPart();
- filedatasource = new FileDataSource(filePath[j]);
- filePart.setDataHandler(new DataHandler(filedatasource));
- try {
- filePart.setFileName(MimeUtility.encodeText(filedatasource.getName()));
- } catch (Exception e) {
- e.printStackTrace();
- }
- mm.addBodyPart(filePart);
- }
- mimeMessage.setContent(mm);
- } else {
- messageHelper.setText(text, true);
- }
- List<InternetAddress> list = new ArrayList<InternetAddress>();
- for (int i = 0; i < mailto.length; i++) {
- list.add(new InternetAddress(mailto[i]));
- }
- InternetAddress[] address = list.toArray(new InternetAddress[list.size()]);
- mimeMessage.setRecipients(Message.RecipientType.TO, address);
- mimeMessage = messageHelper.getMimeMessage();
- EmailThread et = new EmailThread(mimeMessage);
- et.start();
- }
- }
package com.*.email.service.impl; import java.util.ArrayList; import java.util.List; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.annotation.Resource; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.Multipart; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeUtility; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Service; import com.*.email.service.EmailService; /** * Description : 邮件相关服务接口实现. * Create Time : 2013年12月2日 上午10:10:25 * Copyright : Copyright (c) 2010 - 2013 All rights reserved. * * @author Mr Yan * @version 1.0 */ @Service public class EmailServiceImpl implements EmailService{ @Resource(name = "javaMailSender") private JavaMailSenderImpl javaMailSender; class EmailThread extends Thread { private final MimeMessage mimeMessage; public EmailThread(MimeMessage mimeMessage) { this.mimeMessage = mimeMessage; } @Override public void run() { javaMailSender.send(mimeMessage); } } @Override public void sendMail(String mailto, String fromEmail, String text, String title) throws Exception { fromEmail = javaMailSender.getUsername(); MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8"); messageHelper.setFrom(new InternetAddress(fromEmail)); messageHelper.setSubject(title); messageHelper.setText(text, true); messageHelper.setTo(new InternetAddress(mailto)); mimeMessage = messageHelper.getMimeMessage(); EmailThread et = new EmailThread(mimeMessage); et.start(); } @Override public void sendBatchMail(String[] mailto, String fromEmail, String text, String title) throws Exception { MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8"); messageHelper.setFrom(new InternetAddress(fromEmail)); messageHelper.setSubject(title); messageHelper.setText(text, true); List<InternetAddress> list = new ArrayList<InternetAddress>(); for (int i = 0; i < mailto.length; i++) { list.add(new InternetAddress(mailto[i])); } InternetAddress[] address = list.toArray(new InternetAddress[list.size()]); mimeMessage.setRecipients(Message.RecipientType.TO, address); mimeMessage = messageHelper.getMimeMessage(); EmailThread et = new EmailThread(mimeMessage); et.start(); } @Override public void sendBatchMailWithFile(String[] mailto, String fromEmail, String text, String title, String[] filePath) throws Exception { MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8"); messageHelper.setFrom(new InternetAddress(MimeUtility.encodeText(fromEmail))); messageHelper.setSubject(title); if (filePath != null) { BodyPart mdp = new MimeBodyPart();// 新建一个存放信件内容的BodyPart对象 mdp.setContent(text, "text/html;charset=UTF-8");// 给BodyPart对象设置内容和格式/编码方式 Multipart mm = new MimeMultipart();// 新建一个MimeMultipart对象用来存放BodyPart对象 mm.addBodyPart(mdp);// 将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart) // 把mm作为消息对象的内容 MimeBodyPart filePart; FileDataSource filedatasource; // 逐个加入附件 for (int j = 0; j < filePath.length; j++) { filePart = new MimeBodyPart(); filedatasource = new FileDataSource(filePath[j]); filePart.setDataHandler(new DataHandler(filedatasource)); try { filePart.setFileName(MimeUtility.encodeText(filedatasource.getName())); } catch (Exception e) { e.printStackTrace(); } mm.addBodyPart(filePart); } mimeMessage.setContent(mm); } else { messageHelper.setText(text, true); } List<InternetAddress> list = new ArrayList<InternetAddress>(); for (int i = 0; i < mailto.length; i++) { list.add(new InternetAddress(mailto[i])); } InternetAddress[] address = list.toArray(new InternetAddress[list.size()]); mimeMessage.setRecipients(Message.RecipientType.TO, address); mimeMessage = messageHelper.getMimeMessage(); EmailThread et = new EmailThread(mimeMessage); et.start(); } }
3、服务配置
- <bean name="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
- <property name="host" value="smtp.163.com" />
- <property name="defaultEncoding" value="UTF-8"></property>
- <property name="username" value="[email protected]"></property>
- <property name="password" value="password"></property>
- <property name="javaMailProperties">
- <map>
- <entry key="mail.smtp.auth" value="true" />
- </map>
- </property>
- </bean>
<bean name="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="smtp.163.com" /> <property name="defaultEncoding" value="UTF-8"></property> <property name="username" value="[email protected]"></property> <property name="password" value="password"></property> <property name="javaMailProperties"> <map> <entry key="mail.smtp.auth" value="true" /> </map> </property> </bean>