基于SSM框架实现邮件发送工具类

要实现qq邮箱发送邮件,前提需要开启POP3/SMTP服务。
如图:

这里写图片描述

整体思路:

  • 实现发送邮件工具类(MailUtil.class)
  • 配置账户密码属性文件(mail.properties)
  • spring容器管理工具类(application-mail.xml)

一、实现发送邮件工具类
代码如下:

import java.util.List;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.multipart.MultipartFile;

public class MailUtil {
    private JavaMailSenderImpl mailSender; 

    /**
    *   JavaMailSenderImpl支持MimeMessages和SimpleMailMessages。
    * MimeMessages为复杂邮件模板,支持文本、附件、html、图片等。
    * SimpleMailMessages实现了MimeMessageHelper,为普通邮件模板,支持文本
    */
    private SimpleMailMessage simpleMailMessage;

    /**
     * 描述:Spring 依赖注入
     * @author wanghaoyu
     * @date 
     * @version 1.0
     * @param mailSender
     * @since 1.8
     *
     */
    public void setMailSender(JavaMailSenderImpl mailSender) {
        this.mailSender = mailSender;
    }

    /**
     * 描述:Spring 依赖注入
     * @author wanghaoyu
     * @date 
     * @version 1.0
     * @param simpleMailMessage void
     * @since 1.8
     *
     */
    public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) {
        this.simpleMailMessage = simpleMailMessage;
    }

    /**
     * 单发
     *
     * @param recipient 收件人
     * @param subject 主题
     * @param content 内容
     */
    public void send(String recipient,String subject,String content){
        System.out.println(simpleMailMessage);
        simpleMailMessage.setTo(recipient);
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(content);
        mailSender.send(simpleMailMessage);
    }

    /**
     * 群发
     *
     * @param recipients 收件人
     * @param subject 主题
     * @param content 内容
     */
    public void send(List<String> recipients,String subject,String content){
        simpleMailMessage.setTo(recipients.toArray(new String[recipients.size()]));
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(content);
        mailSender.send(simpleMailMessage);
    }

    /**
     * 发送带附件的邮件
     * @author malizhi
     * @param recipient
     * @param subject
     * @param content
     * @param file void
     * @version 1.0
     * @exception Nothing
     */
    public void sendWithFile(String recipient,String subject,String content,MultipartFile file){
        //使用JavaMail的MimeMessage,支付更加复杂的邮件格式和内容  
        MimeMessage msg = mailSender.createMimeMessage();
        try {
            //创建MimeMessageHelper对象,处理MimeMessage的辅助类  
            MimeMessageHelper helper = new MimeMessageHelper(msg, true);  
            //使用辅助类MimeMessage设定参数  
            helper.setFrom(mailSender.getUsername());  
            helper.setTo(recipient);  
            helper.setSubject(subject);  
            helper.setText(content);
            //加入附件  
            helper.addAttachment(file.getOriginalFilename(), file);
        } catch (MessagingException e) {
            e.printStackTrace();
        }  
        // 发送邮件  
        mailSender.send(msg);
    }
}

二、配置账户密码属性文件(mail.properties)

mail.host=smtp.qq.com
mail.username=yourusername@qq.com
mail.password=yourpassword
mail.smtp.auth=true
mail.smtp.timeout=25000
mail.smtp.port=25

三、spring容器管理工具类(application-mail.xml)

<?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:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    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/aop  
                        http://www.springframework.org/schema/aop/spring-aop.xsd  
                        http://www.springframework.org/schema/tx  
                        http://www.springframework.org/schema/tx/spring-tx.xsd  
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd  
                        ">  
    <!-- 引入属性文件 -->
    <context:property-placeholder ignore-unresolvable="true" location="classpath:properties/mail.properties" />

    <!-- 邮件发送器   -->  
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">  
        <property name="host" value="${mail.host}" /> 
        <property name="username" value="${mail.username}" />  
        <property name="password" value="${mail.password}" />  
        <property name="defaultEncoding" value="UTF-8"></property>  
        <property name="javaMailProperties">  
            <props>  
                <prop key="mail.smtp.auth">${mail.smtp.auth}</prop>  
                <prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop>  
                <prop key="mail.smtp.port">${mail.smtp.port}</prop>  
            </props>  
        </property>  
    </bean>  

    <bean id="simpleMailMessage" class="org.springframework.mail.SimpleMailMessage">
        <!-- 发件人email -->
        <property name="from" value="${mail.username}" />
    </bean>

    <bean id="mailUtil" class="com.neuedu.crm.utils.MailUtil">
        <property name="mailSender" ref="mailSender"></property>
        <property name="simpleMailMessage" ref="simpleMailMessage"></property>
    </bean>
</beans>

教程完毕~

©️2020 CSDN 皮肤主题: 程序猿惹谁了 设计师:上身试试 返回首页