zoukankan      html  css  js  c++  java
  • spring邮件发送

    1,Emaill类:

    package com.learn.jsp.pojo;

    /**
     * 邮件基本信息
     * @author kevin
     *
     */
    public class Email {
     private String emailFrom ;
     private String emailTo;
     private String emailSubject;
     private String emailContent;
     
     public String getEmailContent() {
      return emailContent;
     }
     public String getEmailFrom() {
      return emailFrom;
     }
     public String getEmailSubject() {
      return emailSubject;
     }
     public String getEmailTo() {
      return emailTo;
     }
     public void setEmailContent(String emailContent) {
      this.emailContent = emailContent;
     }
     public void setEmailFrom(String emailFrom) {
      this.emailFrom = emailFrom;
     }
     public void setEmailSubject(String emailSubject) {
      this.emailSubject = emailSubject;
     }
     public void setEmailTo(String emailTo) {
      this.emailTo = emailTo;
     }

    }

    2,EmailThread:

    package com.learn.jsp.pojo;

    import org.apache.commons.lang3.StringUtils;
    import org.springframework.mail.SimpleMailMessage;
    import org.springframework.mail.javamail.JavaMailSender;

    public class EmailThread implements Runnable {
     private Email email;
     private JavaMailSender emailSender;

     public EmailThread(Email email, JavaMailSender emailSender) {
      super();
      this.email = email;
      this.emailSender = emailSender;
     }

     public EmailThread() {
      super();
     }

     public void run() {
      //发送条件不足
      if (email == null || emailSender == null) {
       System.out.println("发送条件不足发送失败~~~");
       return;
      }

      SimpleMailMessage mail = new SimpleMailMessage();
      boolean isOk = true;
      try {
       if (email != null) {
        if (StringUtils.isNotEmpty(email.getEmailFrom())) {
         mail.setFrom(email.getEmailFrom());// 发送者
        } else {
         isOk = false;
        }
        if (StringUtils.isNotEmpty(email.getEmailTo())) {
         mail.setTo(email.getEmailTo());// 接受者
        } else {
         isOk = false;
        }
        if (StringUtils.isNotEmpty(email.getEmailSubject())) {
         mail.setSubject(email.getEmailSubject());// 主题
        } else {
         isOk = false;
        }
        if (StringUtils.isNotEmpty(email.getEmailContent())) {
         mail.setText(email.getEmailContent());// 邮件内容
        } else {
         isOk = false;
        }
       } else {
        isOk = false;
       }
       if (!isOk) {
        throw new Exception("内容为空~~-发送失败");
       }
       // 发送邮件
       emailSender.send(mail);
      } catch (Exception e) {
       System.out.println(e.getMessage());
      }
     }
    }

    3基础配置:

    email.properties

    mail.smtp.host=smtp.qq.com
    mail.smtp.auth=true
    mail.smtp.timeout=50000
    mail.smtp.username=xxxxxxx@qq.com
    mail.smtp.password=独立密码
    mail.smtp.port=465
    mail.smtp.starttls.enable=true

    4,配置spring bean

    <!-- 加载配置文件 -->
     <context:property-placeholder location="classpath:email.properties" />

    <bean id="emailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
      <property name="host" value="${mail.smtp.host}" />
      <property name="port" value="${mail.smtp.port}" />
      <property name="username" value="${mail.smtp.username}" />
      <property name="password" value="${mail.smtp.password}" />
      <property name="javaMailProperties">
       <props>
        <prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop>
        <prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
        <prop key="mail.smtp.starttls.enable">${mail.smtp.starttls.enable}</prop>
        <prop key="mail.smtp.socketFactory.port">${mail.smtp.port}</prop>
        <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
        <prop key="mail.smtp.socketFactory.fallback">false</prop>
       </props>
      </property>
     </bean>

    5,controller中注入发送器:

    @Autowired
     @Qualifier("emailSender")
     private JavaMailSender emailSender;

    6,实现邮件发送

  • 相关阅读:
    jmeter调试-webservise服务直接HTTP请求--方式一
    jmeter-webservise服务HTTP信息头管理器方式--方式二
    使用SoupUI工具获得webservise服务的请求格式内容
    SOUPUI安装破解-小白看
    Jmeter-插件扩展及性能监控插件的安装
    jmeter-命令行执行及测试报告导出
    类加载过程
    老生常谈:String s1 = new String("abc") 创建了几个字符串对象及8 种基本类型的包装类和常量池
    mysql的日期时间类型格式
    leetCode 您正在爬楼梯。它需要n步才能到达顶部。每次您可以爬1或2步。您可以通过几种不同的方式登顶?
  • 原文地址:https://www.cnblogs.com/sunshinekevin/p/7356142.html
Copyright © 2011-2022 走看看