• spring发送邮件


    Spring邮件抽象层的主要包为org.springframework.mail。它包括了发送电子邮件的主要接口MailSender,和值对象SimpleMailMessage,它封装了简单邮件的属性如fromto,ccsubject,text。 包里还包含一棵以MailException为根的checked Exception继承树,它们提供了对底层邮件系统异常的高级别抽象。 要获得关于邮件异常层次的更丰富的信息,请参考Javadocs。

    为了使用JavaMail中的一些特色, 比如MIME类型的信件, Spring提供了MailSender的一个子接口, 即org.springframework.mail.javamail.JavaMailSender。Spring还提供了一个回调接口org.springframework.mail.javamail.MimeMessagePreparator,  用于准备JavaMail的MIME信件。

    这里简单的介绍了如何使用spring发送各种形式的邮件以及配置。

    一,Spring配置发送邮件

    1,配置邮件发送信息

    #sys.mail.protocol=smtp
    sys.mail.transport.protocol=smtp
    sys.mail.host=smtp.exmail.qq.com
    sys.mail.sysEmailAddress=***@***.com
    sys.mail.password=***
    sys.mail.debug=true
    sys.mail.smtp.auth=true
    sys.mail.smtp.timeout=25000
    sys.mail.smtp.port=465
    sys.mail.smtp.ssl.enable=true
    sys.mail.toSet=****,***,***
    

      2,spring配置信息

    <?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:util="http://www.springframework.org/schema/util"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    
    
        <!--	邮件发票配置		-->
        <util:properties id="mailProp" location="classpath:config/mail.properties"/>
    
        <bean class="org.springframework.beans.factory.config.PropertiesFactoryBean" id="configProperties">
            <property name="locations">
                <list>
                    <value>classpath:config/mail.properties</value>
                </list>
            </property>
        </bean>
    
        <!-- 邮件发送   -->
        <bean class="org.springframework.mail.javamail.JavaMailSenderImpl" id="mailSender">
            <property name="host" value="#{mailProp['sys.mail.host']}"></property>
            <property name="username" value="#{mailProp['sys.mail.sysEmailAddress']}"></property>
            <property name="password" value="#{mailProp['sys.mail.password']}"></property>
            <property name="protocol" value="#{mailProp['sys.mail.transport.protocol']}"></property>
            <property name="defaultEncoding" value="UTF-8"></property>
            <property name="javaMailProperties">
                <props>
                    <prop key="mail.debug">#{mailProp['sys.mail.debug']}</prop>
                    <prop key="mail.smtp.auth">#{mailProp['sys.mail.smtp.auth']}</prop>
                    <prop key="mail.smtp.timeout">#{mailProp['sys.mail.smtp.timeout']}</prop>
                    <prop key="mail.smtp.port">#{mailProp['sys.mail.smtp.port']}</prop>
                    <prop key="mail.smtp.ssl.enable">#{mailProp['sys.mail.smtp.ssl.enable']}</prop>
                    <prop key="mail.transport.protocol">#{mailProp['sys.mail.transport.protocol']}</prop>
                </props>
            </property>
        </bean>
    </beans>
    

      3,注入发送配置信息

    4,发送邮件

    /**
         * 发送邮件
         */
        public void sendMail(JavaMailSender mailSender, String from, Set<String> set, String subject, String content, Boolean isHtml, Boolean isAttachment, Map<String, File> attachments) throws Exception {
    
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, isAttachment);
    
            //添加发送者
            helper.setFrom(from);
    
    //                Set<String> toSet = simpleEmail.getToSet();
            //添加接收者
            helper.setTo(set.toArray(new String[set.size()]));
    
            //添加主题
            helper.setSubject(subject);
            //添加正文
            helper.setText(content, isHtml);
    
            //添加附件
            if (isAttachment && attachments != null) {
                for (Map.Entry<String, File> attach : attachments.entrySet()) {
                    helper.addAttachment(attach.getKey(), attach.getValue());
                }
            }
    
            JavaMailSenderImpl javaMailSender = (JavaMailSenderImpl) mailSender;
        ///避开认证问题 MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); javaMailSender.getJavaMailProperties().put("mail.smtp.ssl.socketFactory", sf); javaMailSender.send(message); // 发送 }

      二,javaMail发送工具类

    package com.ehaoyao.jobcenter.order.utils;
    
    import com.sun.mail.util.MailSSLSocketFactory;
    
    import javax.mail.Address;
    import javax.mail.Message;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    import java.util.Date;
    import java.util.Properties;
    
    /**
     * com.ehaoyao.utils
     * 发送邮件工具类
     *
     * @author PF
     * @create 2017-10-12 17:34
     **/
    
    public class EmailUtil {
        public static void sendMail(String fromMail, String user, String password, String toMail, String mailTitle, String mailContent) throws Exception {
            Properties props = new Properties();
            // 开启debug调试
            props.setProperty("mail.debug", "true");
            // 发送服务器需要身份验证
            props.setProperty("mail.smtp.auth", "true");
            // 设置邮件服务器主机名
            props.setProperty("mail.host", "smtp.qq.com");
            // 发送邮件协议名称
            props.setProperty("mail.transport.protocol", "smtp");
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            props.put("mail.smtp.ssl.enable", "true");
            props.put("mail.smtp.ssl.socketFactory", sf);
            Session session = Session.getInstance(props);
            Message msg = new MimeMessage(session);
            msg.setSubject(mailTitle);
            msg.setText(mailContent);
            msg.setFrom(new InternetAddress(fromMail));
            Transport transport = session.getTransport();
            transport.connect("smtp.exmail.qq.com", fromMail, password);
    
            transport.sendMessage(msg, new Address[]{new InternetAddress(toMail)});
            transport.close();
        }
    
        public static void main(String[] args) throws Exception {
            EmailUtil.sendMail("***@***.com","ces","***","***@qq.com","测试","测试");
        }
    }
    

      

  • 相关阅读:
    如何用纯 CSS 创作一个跳动的字母 i
    如何用纯 CSS 创作一个变色旋转动画
    如何用纯 CSS 创作气泡填色的按钮特效
    如何用纯 CSS 创作一个跳 8 字型舞的 loader
    如何用纯 CSS 创作一只徘徊的果冻怪兽
    如何用纯 CSS 创作一个单元素抛盒子的 loader
    如何用纯 CSS 创作单元素点阵 loader
    如何用纯 CSS 创作一个摇摇晃晃的 loader
    [Monkey King]
    473. 核电站问题
  • 原文地址:https://www.cnblogs.com/wlsblog/p/7778891.html
Copyright © 2020-2023  润新知