美文网首页
springboot 发送邮件

springboot 发送邮件

作者: CaptainZ0713 | 来源:发表于2018-10-31 19:44 被阅读0次

    邮件的依赖:

            <!--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
    

    相关文章

      网友评论

          本文标题:springboot 发送邮件

          本文链接:https://www.haomeiwen.com/subject/vghztqtx.html