springboot 发送邮件

邮件的依赖:

        <!--mail-->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
        </dependency>

定义邮件的服务类:

public class MailService {
    @Value("${mail.from}")
    private String from;

    @Resource(name = "javaMailSender")
    private JavaMailSender javaMailSender;

    public void sendMail(String subject,String to, String text) {
        sendMail(subject, text, Collections.singletonList(to));
    }

    public void sendMail(String subject, String text, Collection<String> toMails) {
        if (CollectionUtils.isEmpty(toMails)) {
            return;
        }

        for (String toMail : toMails) {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(from);
            message.setTo(toMail);
            message.setSubject(subject);
            message.setText(text);

            javaMailSender.send(message);
        }
    }
}

这里我们实例化MailService

public class MailConfig {

    @Value("${mail.host}")
    private String host;
    @Value("${mail.port}")
    private int port;
    @Value("${mail.username}")
    private String username;
    @Value("${mail.password}")
    private String password;
    @Value("${mail.protocol:smtp}")
    private String protocol;
    @Value("${mail.defaultEncoding:UTF-8}")
    private String defaultEncoding;

    @Bean(name = "javaMailSender")
    public JavaMailSender javaMailSender() {
        System.setProperty("https.protocols", "TLSv1.1");

        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setHost(host);
        sender.setPort(port);
        sender.setUsername(username);
        sender.setPassword(password);
        sender.setProtocol(protocol);
        sender.setDefaultEncoding(defaultEncoding);

        Properties properties = new Properties();
        properties.setProperty("mail.smtp.auth", "true");//开启认证
        properties.setProperty("mail.debug", "true");//启用调试
        properties.setProperty("mail.smtp.timeout", "10000");//设置链接超时
        properties.setProperty("mail.smtp.port", Integer.toString(port));//设置端口
        properties.setProperty("mail.smtp.socketFactory.port", Integer.toString(port));//设置ssl端口
        properties.setProperty("mail.smtp.socketFactory.fallback", "false");
        properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

        sender.setJavaMailProperties(properties);
        return sender;
    }
}

测试一下:

public class MailTest {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:/test-applicationContext-web.xml");
        applicationContext.start();

        String subject = "test-出现异常-subject";
        String text = "test-出现异常";
        String to = "xxx@xxx.com";

        MailService mailService = applicationContext.getBean(MailService.class);
        mailService.sendMail(subject,to,text);

        try {
            //等待一下,防止邮件还没发送出程序就结束了
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

test-applicationContext-web.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: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">

    <context:component-scan base-package="com.navercorp.pinpoint.web.config.mail,com.navercorp.pinpoint.web.mail"/>

    <bean id="mailConfig" class="com.navercorp.pinpoint.web.config.mail.MailConfig"/>
    <bean id="mailService" class="com.navercorp.pinpoint.web.mail.MailService"/>

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:mail.properties</value>
            </list>
        </property>
    </bean>
</beans>

mail.properties
这里需要注意一下下,mail.host,mail.port不同的厂商(qq,163等)不一样.

mail.host=smtp.exmail.qq.com
mail.port=465
mail.username=xxx@xxx.cn
mail.password=xxx
mail.protocol=smtp
mail.defaultEncoding=UTF-8
mail.from=xxx@xx.cn
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 简述 在日常工作开发中,发送邮件功能有时需要我们去开发使用,这里首先介绍以下与发送接受邮件相关的一些协议: 发送邮...
    BestbpF阅读 15,208评论 2 19
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,491评论 19 139
  • 前言 在项目中可能会碰到发送邮件的需求,这时,我们就可以使用SpringBoot的mail模块进行快速的开发。 配...
    sT丶阅读 4,032评论 0 0
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,081评论 6 342
  • 卡片一: 苦瓜 飞机 卡片2: 雪,键盘 下午三四点的时候,开始飘雪了。 “诶诶?怎么会下雪!不会一直下吧?天气预...
    豆藏阅读 1,644评论 1 1