开发中经常会遇到发送邮件进行用户验证,或者其它推送信息的情况,本文基于spring,完成邮件的发送,主要支持普通文本邮件的发送,html文本邮件的发送,带附件的邮件发送,没有实现群发、多个附件发送等需求。如果需要可以参照如下源代码进行修改完成。
1 <dependencies> 2 <dependency> 3 <groupId>org.springframework</groupId> 4 <artifactId>spring-context</artifactId> 5 <version>4.1.6.RELEASE</version> 6 </dependency> 7 <dependency> 8 <groupId>org.springframework</groupId> 9 <artifactId>spring-context-support</artifactId> 10 <version>4.1.6.RELEASE</version> 11 </dependency> 12 <dependency> 13 <groupId>javax.mail</groupId> 14 <artifactId>mail</artifactId> 15 <version>1.4.7</version> 16 </dependency> 17 </dependencies>POM dependencies
1 <!-- Production implementation of the JavaMailSender interface, supporting 2 both JavaMail MimeMessages and Spring SimpleMailMessages --> 3 <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> 4 <property name="host" value="smtp.gmail.com" /> 5 <property name="port" value="587" /> 6 <property name="username" value="<!-- Provide your Gmail ID -->" /> 7 <property name="password" value="<!-- Provide your Gmail Password -->" /> 8 9 <!-- The name of the property, following JavaBean naming conventions --> 10 <property name="javaMailProperties"> 11 <props> 12 <prop key="mail.transport.protocol">smtp</prop> 13 <prop key="mail.smtp.auth">true</prop> 14 <prop key="mail.smtp.starttls.enable">true</prop> 15 <prop key="mail.debug">true</prop> 16 </props> 17 </property> 18 </bean>spring 配置
1 /** 2 * Copyright (c) 2015, www.wisdombud.com 3 * All Rights Reserved. 4 */ 5 package com.wisdombud.wisdomhr.common.srv; 6 7 import java.io.FileNotFoundException; 8 9 import javax.mail.MessagingException; 10 11 /** 12 * 功能: 邮件发送接口.<br/> 13 * date: 2015年8月24日 上午9:53:05 <br/> 14 * 15 * @author joseph 16 * @version 17 */ 18 public interface MailSenderSrv { 19 /** 20 * 功能: 发普通邮件,msgBody是普通的文本.<br/> 21 * date: 2015年8月24日 上午9:57:19 <br/> 22 * 23 * @author joseph 24 * @param toAddress 25 * @param fromAddress 26 * @param subject 27 * @param msgBody 28 */ 29 void sendEmail(String toAddress, String fromAddress, String subject, String msgBody); 30 31 /** 32 * 功能: 发html邮件或者普通邮件,msgBody是html文本或者普通文本.<br/> 33 * MimeMessage 消息发送.<br/> 34 * date: 2015年8月24日 上午9:57:19 <br/> 35 * 36 * @author joseph 37 * @param toAddress 38 * @param fromAddress 39 * @param subject 40 * @param msgBody 41 * @throws MessagingException 42 */ 43 void sendHtmlEmail(String toAddress, String fromAddress, String subject, String htmlBody) throws MessagingException; 44 45 /** 46 * 功能: 发html邮件或者普通邮件,msgBody是html文本或者普通文本,带附件.<br/> 47 * MimeMessage 消息发送.<br/> 48 * date: 2015年8月24日 上午9:57:19 <br/> 49 * 50 * @author joseph 51 * @param toAddress 52 * @param fromAddress 53 * @param subject 54 * @param msgBody 55 * @throws MessagingException 56 * @throws FileNotFoundException 57 */ 58 void sendHtmlEmail(String toAddress, String fromAddress, String subject, String htmlBody, String filePath) 59 throws MessagingException, FileNotFoundException; 60 61 /** 62 * 功能: 发html邮件或者普通邮件,msgBody是html文本或者普通文本,带附件.<br/> 63 * MimeMessage 消息发送.<br/> 64 * date: 2015年8月24日 上午9:57:19 <br/> 65 * 66 * @author joseph 67 * @param toAddress 68 * @param fromAddress 69 * @param subject 70 * @param htmlBody 71 * @param filePath 72 * @param fileName 73 * @throws MessagingException 74 * @throws FileNotFoundException 75 */ 76 void sendHtmlEmail(String toAddress, String fromAddress, String subject, String htmlBody, String filePath, 77 String fileName) throws MessagingException, FileNotFoundException; 78 }邮件发送接口代码
1 /** 2 * Copyright (c) 2015, www.wisdombud.com 3 * All Rights Reserved. 4 */ 5 package com.wisdombud.wisdomhr.common.srv; 6 7 import java.io.File; 8 import java.io.FileNotFoundException; 9 10 import javax.mail.MessagingException; 11 import javax.mail.internet.MimeMessage; 12 13 import org.springframework.beans.factory.annotation.Autowired; 14 import org.springframework.mail.SimpleMailMessage; 15 import org.springframework.mail.javamail.JavaMailSender; 16 import org.springframework.mail.javamail.MimeMessageHelper; 17 import org.springframework.stereotype.Service; 18 19 /** 20 * 功能: 邮件发送实现.<br/> 21 * date: 2015年8月24日 上午10:07:01 <br/> 22 * 23 * @author joseph 24 * @version 25 */ 26 @Service("mailSenderSrv") 27 public class MailSenderSrvImpl implements MailSenderSrv { 28 29 @Autowired 30 private JavaMailSender mailSender; 31 32 /** 33 * @see com.wisdombud.wisdomhr.common.srv.MailSenderSrv#sendEmail(java.lang.String, 34 * java.lang.String, java.lang.String, java.lang.String) 35 */ 36 37 @Override 38 public void sendEmail(String toAddress, String fromAddress, String subject, String msgBody) { 39 SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); 40 simpleMailMessage.setFrom(fromAddress); 41 simpleMailMessage.setTo(toAddress); 42 simpleMailMessage.setSubject(subject); 43 simpleMailMessage.setText(msgBody); 44 mailSender.send(simpleMailMessage); 45 } 46 47 /** 48 * @throws MessagingException 49 * @see com.wisdombud.wisdomhr.common.srv.MailSenderSrv#sendHtmlEmail(java.lang.String, 50 * java.lang.String, java.lang.String, java.lang.String) 51 */ 52 53 @Override 54 public void sendHtmlEmail(String toAddress, String fromAddress, String subject, String htmlBody) 55 throws MessagingException { 56 MimeMessage message = mailSender.createMimeMessage(); 57 MimeMessageHelper helper = new MimeMessageHelper(message, "UTF-8"); 58 helper.setTo(toAddress); 59 helper.setFrom(fromAddress); 60 helper.setText(htmlBody, true); 61 helper.setSubject(subject); 62 63 mailSender.send(message); 64 65 } 66 67 /** 68 * @throws MessagingException 69 * @throws FileNotFoundException 70 * @see com.wisdombud.wisdomhr.common.srv.MailSenderSrv#sendHtmlEmail(java.lang.String, 71 * java.lang.String, java.lang.String, java.lang.String, 72 * java.lang.String) 73 */ 74 75 @Override 76 public void sendHtmlEmail(String toAddress, String fromAddress, String subject, String htmlBody, String filePath) 77 throws MessagingException, FileNotFoundException { 78 MimeMessage message = mailSender.createMimeMessage(); 79 MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); 80 helper.setTo(toAddress); 81 helper.setFrom(fromAddress); 82 helper.setText(htmlBody, true); 83 helper.setSubject(subject); 84 File file = new File(filePath); 85 if (!file.exists()) { 86 throw new FileNotFoundException("找不到附件:" + filePath); 87 } 88 helper.addAttachment(file.getName(), file); 89 mailSender.send(message); 90 } 91 92 /** 93 * @throws MessagingException 94 * @throws FileNotFoundException 95 * @see com.wisdombud.wisdomhr.common.srv.MailSenderSrv#sendHtmlEmail(java.lang.String, 96 * java.lang.String, java.lang.String, java.lang.String, 97 * java.lang.String) 98 */ 99 100 @Override 101 public void sendHtmlEmail(String toAddress, String fromAddress, String subject, String htmlBody, String filePath, 102 String fileName) throws MessagingException, FileNotFoundException { 103 MimeMessage message = mailSender.createMimeMessage(); 104 MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); 105 helper.setTo(toAddress); 106 helper.setFrom(fromAddress); 107 helper.setText(htmlBody, true); 108 helper.setSubject(subject); 109 File file = new File(filePath); 110 if (!file.exists()) { 111 throw new FileNotFoundException("找不到附件:" + filePath); 112 } 113 helper.addAttachment(fileName, file); 114 mailSender.send(message); 115 } 116 }邮件发送实现代码
1 package com.wisdombud.wisdomhr.test.srv; 2 3 import java.io.FileNotFoundException; 4 5 import javax.mail.MessagingException; 6 7 import org.junit.Test; 8 import org.springframework.beans.factory.annotation.Autowired; 9 10 import com.wisdombud.wisdomhr.common.srv.MailSenderSrv; 11 import com.wisdombud.wisdomhr.test.base.AbstractTransactionContextTest; 12 13 public class MailSrvTest extends AbstractTransactionContextTest { 14 @Autowired 15 private MailSenderSrv srv; 16 17 @Test 18 public void testTextMail() { 19 String from = "test@163.com"; 20 String to = "test@163.com"; 21 String subject = "测试主题"; 22 String text = "测试内容"; 23 this.srv.sendEmail(to, from, subject, text); 24 } 25 26 @Test 27 public void testHtmlMail() { 28 String from = "test@163.com"; 29 String to = "test@163.com"; 30 String subject = "测试主题"; 31 String text = "<html><a href=/"www.baidu.com/">百度</a></html>"; 32 try { 33 this.srv.sendHtmlEmail(to, from, subject, text); 34 } catch (MessagingException e) { 35 // TODO Auto-generated catch block 36 e.printStackTrace(); 37 } 38 } 39 40 @Test 41 public void testHtmlMailWithAttach() { 42 String from = "test@163.com"; 43 String to = "test@163.com"; 44 String subject = "测试主题"; 45 String text = "<html><a href=/"www.baidu.com/">百度</a></html>"; 46 String filePath = "d://1.sql"; 47 try { 48 this.srv.sendHtmlEmail(to, from, subject, text, filePath); 49 } catch (MessagingException e) { 50 // TODO Auto-generated catch block 51 e.printStackTrace(); 52 } catch (FileNotFoundException e) { 53 // TODO Auto-generated catch block 54 e.printStackTrace(); 55 } 56 } 57 58 @Test 59 public void testHtmlMailWithAttachAndName() { 60 String from = "test@163.com"; 61 String to = "test@163.com"; 62 String subject = "测试主题"; 63 String text = "<html><a href=/"www.baidu.com/">百度</a></html>"; 64 String filePath = "d://1.sql"; 65 String fileName = "haha.sql"; 66 try { 67 this.srv.sendHtmlEmail(to, from, subject, text, filePath, fileName); 68 } catch (MessagingException e) { 69 // TODO Auto-generated catch block 70 e.printStackTrace(); 71 } catch (FileNotFoundException e) { 72 // TODO Auto-generated catch block 73 e.printStackTrace(); 74 } 75 } 76 77 }邮件发送测试代码