开发中经常会遇到发送邮件进行用户验证,或者其它推送信息的情况,本文基于spring,完成邮件的发送,主要支持普通文本邮件的发送,html文本邮件的发送,带附件的邮件发送,没有实现群发、多个附件发送等需求。如果需要可以参照如下源代码进行修改完成。
  1. POM文件配置

     <dependencies>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.1.6.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>4.1.6.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.7</version>
    </dependency>
    </dependencies>

    POM dependencies

  2. spring 配置
     <!-- Production implementation of the JavaMailSender interface, supporting
    both JavaMail MimeMessages and Spring SimpleMailMessages -->
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="smtp.gmail.com" />
    <property name="port" value="587" />
    <property name="username" value="<!-- Provide your Gmail ID -->" />
    <property name="password" value="<!-- Provide your Gmail Password -->" /> <!-- The name of the property, following JavaBean naming conventions -->
    <property name="javaMailProperties">
    <props>
    <prop key="mail.transport.protocol">smtp</prop>
    <prop key="mail.smtp.auth">true</prop>
    <prop key="mail.smtp.starttls.enable">true</prop>
    <prop key="mail.debug">true</prop>
    </props>
    </property>
    </bean>

    spring 配置

  3. 邮件发送接口
     /**
    * Copyright (c) 2015, www.wisdombud.com
    * All Rights Reserved.
    */
    package com.wisdombud.wisdomhr.common.srv; import java.io.FileNotFoundException; import javax.mail.MessagingException; /**
    * 功能: 邮件发送接口.<br/>
    * date: 2015年8月24日 上午9:53:05 <br/>
    *
    * @author joseph
    * @version
    */
    public interface MailSenderSrv {
    /**
    * 功能: 发普通邮件,msgBody是普通的文本.<br/>
    * date: 2015年8月24日 上午9:57:19 <br/>
    *
    * @author joseph
    * @param toAddress
    * @param fromAddress
    * @param subject
    * @param msgBody
    */
    void sendEmail(String toAddress, String fromAddress, String subject, String msgBody); /**
    * 功能: 发html邮件或者普通邮件,msgBody是html文本或者普通文本.<br/>
    * MimeMessage 消息发送.<br/>
    * date: 2015年8月24日 上午9:57:19 <br/>
    *
    * @author joseph
    * @param toAddress
    * @param fromAddress
    * @param subject
    * @param msgBody
    * @throws MessagingException
    */
    void sendHtmlEmail(String toAddress, String fromAddress, String subject, String htmlBody) throws MessagingException; /**
    * 功能: 发html邮件或者普通邮件,msgBody是html文本或者普通文本,带附件.<br/>
    * MimeMessage 消息发送.<br/>
    * date: 2015年8月24日 上午9:57:19 <br/>
    *
    * @author joseph
    * @param toAddress
    * @param fromAddress
    * @param subject
    * @param msgBody
    * @throws MessagingException
    * @throws FileNotFoundException
    */
    void sendHtmlEmail(String toAddress, String fromAddress, String subject, String htmlBody, String filePath)
    throws MessagingException, FileNotFoundException; /**
    * 功能: 发html邮件或者普通邮件,msgBody是html文本或者普通文本,带附件.<br/>
    * MimeMessage 消息发送.<br/>
    * date: 2015年8月24日 上午9:57:19 <br/>
    *
    * @author joseph
    * @param toAddress
    * @param fromAddress
    * @param subject
    * @param htmlBody
    * @param filePath
    * @param fileName
    * @throws MessagingException
    * @throws FileNotFoundException
    */
    void sendHtmlEmail(String toAddress, String fromAddress, String subject, String htmlBody, String filePath,
    String fileName) throws MessagingException, FileNotFoundException;
    }

    邮件发送接口代码

  4. 邮件发送实现
     /**
    * Copyright (c) 2015, www.wisdombud.com
    * All Rights Reserved.
    */
    package com.wisdombud.wisdomhr.common.srv; import java.io.File;
    import java.io.FileNotFoundException; import javax.mail.MessagingException;
    import javax.mail.internet.MimeMessage; import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.mail.SimpleMailMessage;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.mail.javamail.MimeMessageHelper;
    import org.springframework.stereotype.Service; /**
    * 功能: 邮件发送实现.<br/>
    * date: 2015年8月24日 上午10:07:01 <br/>
    *
    * @author joseph
    * @version
    */
    @Service("mailSenderSrv")
    public class MailSenderSrvImpl implements MailSenderSrv { @Autowired
    private JavaMailSender mailSender; /**
    * @see com.wisdombud.wisdomhr.common.srv.MailSenderSrv#sendEmail(java.lang.String,
    * java.lang.String, java.lang.String, java.lang.String)
    */ @Override
    public void sendEmail(String toAddress, String fromAddress, String subject, String msgBody) {
    SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
    simpleMailMessage.setFrom(fromAddress);
    simpleMailMessage.setTo(toAddress);
    simpleMailMessage.setSubject(subject);
    simpleMailMessage.setText(msgBody);
    mailSender.send(simpleMailMessage);
    } /**
    * @throws MessagingException
    * @see com.wisdombud.wisdomhr.common.srv.MailSenderSrv#sendHtmlEmail(java.lang.String,
    * java.lang.String, java.lang.String, java.lang.String)
    */ @Override
    public void sendHtmlEmail(String toAddress, String fromAddress, String subject, String htmlBody)
    throws MessagingException {
    MimeMessage message = mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, "UTF-8");
    helper.setTo(toAddress);
    helper.setFrom(fromAddress);
    helper.setText(htmlBody, true);
    helper.setSubject(subject); mailSender.send(message); } /**
    * @throws MessagingException
    * @throws FileNotFoundException
    * @see com.wisdombud.wisdomhr.common.srv.MailSenderSrv#sendHtmlEmail(java.lang.String,
    * java.lang.String, java.lang.String, java.lang.String,
    * java.lang.String)
    */ @Override
    public void sendHtmlEmail(String toAddress, String fromAddress, String subject, String htmlBody, String filePath)
    throws MessagingException, FileNotFoundException {
    MimeMessage message = mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
    helper.setTo(toAddress);
    helper.setFrom(fromAddress);
    helper.setText(htmlBody, true);
    helper.setSubject(subject);
    File file = new File(filePath);
    if (!file.exists()) {
    throw new FileNotFoundException("找不到附件:" + filePath);
    }
    helper.addAttachment(file.getName(), file);
    mailSender.send(message);
    } /**
    * @throws MessagingException
    * @throws FileNotFoundException
    * @see com.wisdombud.wisdomhr.common.srv.MailSenderSrv#sendHtmlEmail(java.lang.String,
    * java.lang.String, java.lang.String, java.lang.String,
    * java.lang.String)
    */ @Override
    public void sendHtmlEmail(String toAddress, String fromAddress, String subject, String htmlBody, String filePath,
    String fileName) throws MessagingException, FileNotFoundException {
    MimeMessage message = mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
    helper.setTo(toAddress);
    helper.setFrom(fromAddress);
    helper.setText(htmlBody, true);
    helper.setSubject(subject);
    File file = new File(filePath);
    if (!file.exists()) {
    throw new FileNotFoundException("找不到附件:" + filePath);
    }
    helper.addAttachment(fileName, file);
    mailSender.send(message);
    }
    }

    邮件发送实现代码

  5. 邮件发送测试代码(需要自己替换邮箱)
     package com.wisdombud.wisdomhr.test.srv;
    
     import java.io.FileNotFoundException;
    
     import javax.mail.MessagingException;
    
     import org.junit.Test;
    import org.springframework.beans.factory.annotation.Autowired; import com.wisdombud.wisdomhr.common.srv.MailSenderSrv;
    import com.wisdombud.wisdomhr.test.base.AbstractTransactionContextTest; public class MailSrvTest extends AbstractTransactionContextTest {
    @Autowired
    private MailSenderSrv srv; @Test
    public void testTextMail() {
    String from = "test@163.com";
    String to = "test@163.com";
    String subject = "测试主题";
    String text = "测试内容";
    this.srv.sendEmail(to, from, subject, text);
    } @Test
    public void testHtmlMail() {
    String from = "test@163.com";
    String to = "test@163.com";
    String subject = "测试主题";
    String text = "<html><a href=\"www.baidu.com\">百度</a></html>";
    try {
    this.srv.sendHtmlEmail(to, from, subject, text);
    } catch (MessagingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    } @Test
    public void testHtmlMailWithAttach() {
    String from = "test@163.com";
    String to = "test@163.com";
    String subject = "测试主题";
    String text = "<html><a href=\"www.baidu.com\">百度</a></html>";
    String filePath = "d://1.sql";
    try {
    this.srv.sendHtmlEmail(to, from, subject, text, filePath);
    } catch (MessagingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    } @Test
    public void testHtmlMailWithAttachAndName() {
    String from = "test@163.com";
    String to = "test@163.com";
    String subject = "测试主题";
    String text = "<html><a href=\"www.baidu.com\">百度</a></html>";
    String filePath = "d://1.sql";
    String fileName = "haha.sql";
    try {
    this.srv.sendHtmlEmail(to, from, subject, text, filePath, fileName);
    } catch (MessagingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    } }

    邮件发送测试代码

 

java spring 邮件发送的更多相关文章

  1. Spring 邮件发送

      前言:以前都是直接用Java自带的邮件工具发送邮件,现在Spring帮我们做了封装,提供了更好用更简单的发送邮件工具JavaMailSender 关于邮件服务器的设置就不在这里说了,直接去QQ邮箱 ...

  2. 用java实现邮件发送验证码

    java实现邮件发送验证码 建议不要用qq邮箱,我使用qq邮箱直接一直给我报530错误,我一直认为我代码写的有错误或者POP3/SMTP服务没弄好.所以建议注册个别的邮箱,我就申请了个网易163邮箱瞬 ...

  3. java mail邮件发送(带附件) 支持SSL

    java mail邮件发送(带附件)有三个类 MailSenderInfo.java package mail; import java.util.Properties; import java.ut ...

  4. Spring邮件发送1

    注意:邮件发送code中,邮件服务器的申请和配置是比较主要的一个环节,博主这里用的是QQ的邮件服务器.有需要的可以谷歌.百度查下如何开通. 今天看了下Spring的官方文档的邮件发送这一章节.在这里记 ...

  5. Java实现邮件发送

      概述 Spring Boot下面整合了邮件服务器,使用Spring Boot能够轻松实现邮件发送:整理下最近使用Spring Boot发送邮件和注意事项: Maven包依赖 <depende ...

  6. 【Java EE 学习 21 下】【使用java实现邮件发送、邮件验证】

    一.邮件发送 1.邮件发送使用SMTP协议或者IMAP协议,这里使用SMTP协议演示. SMTP协议使用的端口号:25 rfc821详细记载了该协议的相关信息 (1)使用telnet发送邮件(使用12 ...

  7. JAVA实现邮件发送功能(账号注册验证码、账号激活等)

    第一步,导入JAR包,JAR包下载地址[http://pan.baidu.com/s/1kVRvGyF] 如果是Maven,请直接在Pom文件中加入 <dependency> <gr ...

  8. spring邮件发送

    1,Emaill类: package com.learn.jsp.pojo; /** * 邮件基本信息 * @author kevin * */public class Email { private ...

  9. Java Mail 邮件发送简单封装

    上一篇文章我们用写了一个Java Mail 的Demo,相信你已经可以用那个例子来发送邮件了.但是Demo 有很多的问题. 首先每次发送需要配置的东西很多,包括发件人的邮箱和密码.smtp服务器和SM ...

随机推荐

  1. [已解决]:调用 LoadLibraryEx 失败,在 ISAPI 筛选器 "c:\Windows\Microsoft.NET\Framework\v4.0.30319\\aspnet_filter.

    现象:我的是 win7, iis7, 64bit, 打开网站错误如下: 错误摘要 HTTP 错误 500.0 - Internal Server Error 调用 LoadLibraryEx 失败,在 ...

  2. Android资源站

    用这个帖子记录下看到的好的android资源站 1.各种资源:http://appxcode.com/ 2.图标 2.1 http://www.easyicon.net 2.2 http://www. ...

  3. 【转】Oracle索引失效问题

    转自:http://www.cnblogs.com/millen/archive/2010/01/18/1650423.html 失效情况分析: <> 单独的>,<,(有时会用 ...

  4. 从零开始山寨Caffe·伍:Protocol Buffer简易指南

    你为Class外访问private对象而苦恼嘛?你为设计序列化格式而头疼嘛? ——欢迎体验Google Protocol Buffer 面向对象之封装性 历史遗留问题 面向对象中最矛盾的一个特性,就是 ...

  5. knockoutjs扩展与使用

    原来考虑使用avalon2.0 经过一周的试验,能力不够,用不起来.最终使用了knockout-3.4.js <!DOCTYPE html> <html> <head&g ...

  6. 将excel数据读入matlab

    1.[NUM,TXT,RAW]=xlsread('example'),其中example是你的excel名,假设所有的数据都在example.xls中. 2.NUM返回的是excel中的数据,TXT输 ...

  7. 还原后缀名为.bak的数据库备份文件

    1.打开SQL Server Management Studio,随便右击击一个数据库选择任务-->还原-->数据库 4.在弹出来的窗口中的源选项中选择设备-->点选择设备--> ...

  8. [ios]关于用FMDB 操作数据库 删除 tableView 后刷新

    刚了解使用fmdb,从数据库获取数据 绑定到一个可变数组classNameItems //从ClassList表取得数据 FMResultSet *classInfo=[db executeQuery ...

  9. linux用户不在sudoers文件中

    *** is not in the sudoers file.  This incident will be reported."  (用户不在sudoers文件中--) 处理这个问题很简单 ...

  10. *HDU 1007 计算几何

    Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...