欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring实现邮件发送功能

程序员文章站 2024-04-01 20:35:58
前言:以前都是直接用java自带的邮件工具发送邮件,现在spring帮我们做了封装,提供了更好用更简单的发送邮件工具javamailsender,关于邮件服务器的设置就不在...

前言:以前都是直接用java自带的邮件工具发送邮件,现在spring帮我们做了封装,提供了更好用更简单的发送邮件工具javamailsender,关于邮件服务器的设置就不在这里说了,直接去qq邮箱设置下就好,下面看下主要的步骤: 

步骤一、添加发送邮件相关maven依赖

<!-- spring 邮件发送 -->
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-context-support</artifactid>
      <version>4.3.2.release</version>
    </dependency>
    <dependency>
      <groupid>javax.mail</groupid>
      <artifactid>mail</artifactid>
      <version>1.4.7</version>
    </dependency>    

步骤二、添加相关spring配置,创建一个spring-mail.xml(如果是spring-boot请无视),内容如下:

<?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:context="http://www.springframework.org/schema/context"
  xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">

  <!-- spring javamailsender -->
  <bean class="org.springframework.mail.javamail.javamailsenderimpl">
    <property name="host" value="smtp.qq.com"></property>
    <!-- 或许你可以用465端口,默认的25不适合 -->
    <property name="port" value="587"/>
    <property name="protocol" value="smtp"/>
    <property name="username" value="******@qq.com"></property>
    <!-- 这里的是你在qq邮箱发送开通smtp短信后,获取的授权码 -->
    <property name="password" value="******"></property>
    <property name="defaultencoding" value="utf-8"/>
    <property name="javamailproperties">
      <props>
        <prop key="mail.smtp.auth">true</prop>
        <prop key="mail.smtp.timeout">25000</prop>
      </props>
    </property>
  </bean>
</beans>

步骤三、创建具体的邮件发送工具类

/**
 * 基于spring的javamailsender的邮件发送工具类
 * @author simon
 *
 */
@component
public class emailsender {
  
  @autowired
  private javamailsender javamailsender;
  
  private static emailsender emailsender;
  
  @postconstruct
  public void init() {
    emailsender = this;
    emailsender.javamailsender= this.javamailsender;
  }
  
  /**
   * 发送简单的文本邮件
   * @param sendto    收件人组
   * @param subject    主题
   * @param content    文本内容
   */
  public static void sendsimplemessage(string sendfrom, string[] sendto, string subject, string textcontent) {
    simplemailmessage mail = new simplemailmessage();
    mail.setfrom(sendfrom);
    mail.setto(sendto);
    mail.setsubject(subject);
    mail.settext(textcontent);
    //发送
    emailsender.javamailsender.send(mail);
  }
  
  /**
   * 发送html内容格式的邮件
   * @param sendfrom
   * @param sendto      收件人组
   * @param subject      主题
   * @param htmlcontent    html内容
   * @throws exception
   */
  public static void sendhtmlmessage(string sendfrom, string[] sendto, string subject, string htmlcontent) throws exception {
    mimemessage mimemessage = emailsender.javamailsender.createmimemessage(); 
    
    mimemessagehelper mimemessagehelper = new mimemessagehelper(mimemessage);
    mimemessagehelper.setfrom(sendfrom);
    mimemessagehelper.setto(sendto);
    mimemessagehelper.setsubject(subject);
    // true 表示启动html格式的邮件 
    mimemessagehelper.settext(htmlcontent, true); 

    // 发送邮件
    emailsender.javamailsender.send(mimemessage);
  }
  
}

步骤四、创建单元测试测试发送邮件

/**
 * 邮件发送测试类
 * 
 * @author simon
 *
 */
@runwith(springjunit4classrunner.class)
@contextconfiguration("classpath:applicationcontext.xml")
public class emailsendertest {

  @test
  public void testsend() throws exception {
    string sendfrom = "1317492210@qq.com";
    string[] sendto = {"zhaosheng@hitencent.com"};
    string subject = "spring自带javamailsender发送的html邮件";
    stringbuilder htmlcontent = new stringbuilder()
        .append("<html>")
        .append("<head>")
        .append("<title>")
        .append("spring自带javamailsender发送的html邮件")
        .append("</title>")
        .append("</head>")
        .append("<body>")
        .append("您好!陌生人<p/>")
        .append("</body>")
        .append("</html>");
    
    emailsender.sendhtmlmessage(sendfrom, sendto, subject, htmlcontent.tostring());
    system.out.println("邮件发送成功.");
  }


ok,到这里全部的步骤就结束了,是不是比java mail简单方便很多哉,赶紧自己动手试试吧!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。