• java之mail发送


      一、邮箱的应用在实际的Java开发过程中,也是非常常见的。我这里就是写了一个简单的邮箱发送功能。

      二、邮箱的基本配置,使用的是JavaMailSender的提供的方式来实现邮件的发送

      1)qq邮箱的配置(说明一点目前qq邮箱的配置方式都是采用ssl的方式来实现的,非ssl的方式没有测试成功)

    package com.troy.jpa.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.mail.javamail.JavaMailSenderImpl;
    
    import java.util.Properties;
    
    @Configuration
    public class MailConfiguration {
    
        @Bean
        public JavaMailSender javaMailSender() {
            //设置JavaMail的基本数据
            JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
            //host官方查询
            javaMailSender.setHost("smtp.qq.com");
            //端口465ssl
            javaMailSender.setPort(465);
            //设置账户,密码
            javaMailSender.setUsername("409546297@qq.com");
            javaMailSender.setPassword("密码");
            //认证数据配置,ssl方式需要加入认证方式
            Properties properties = new Properties();
            properties.setProperty("mail.host", "smtp.qq.com");
            properties.setProperty("mail.transport.protocol", "smtp");
            properties.setProperty("mail.smtp.auth", "true");
            properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            properties.setProperty("mail.smtp.port", "465");
            properties.setProperty("mail.smtp.socketFactory.port", "465");
            javaMailSender.setJavaMailProperties(properties);
            return javaMailSender;
        }
    }

      注意:这里的密码不是我们一般登录的密码,而是授权码

      2)163邮箱有2中配置方式

      a、ssl的的连接方式

    package com.troy.jpa.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.mail.javamail.JavaMailSenderImpl;
    
    import java.util.Properties;
    
    @Configuration
    public class MailConfiguration {
    
        @Bean
        public JavaMailSender javaMailSender() {
            JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
            javaMailSender.setHost("smtp.163.com");
            javaMailSender.setPort(465);
            javaMailSender.setUsername("账号");
            javaMailSender.setPassword("密码");
            Properties properties = new Properties();
            properties.setProperty("mail.host", "smtp.163.com");
            properties.setProperty("mail.transport.protocol", "smtp");
            properties.setProperty("mail.smtp.auth", "true");
            properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            properties.setProperty("mail.smtp.port", "465");
            properties.setProperty("mail.smtp.socketFactory.port", "465");
            javaMailSender.setJavaMailProperties(properties);
            return javaMailSender;
        }
    }

      b、非ssl的连接方式

    package com.troy.jpa.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.mail.javamail.JavaMailSenderImpl;
    
    @Configuration
    public class MailConfiguration {
    
        @Bean
        public JavaMailSender javaMailSender() {
            JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
            javaMailSender.setHost("smtp.163.com");
            javaMailSender.setPort(25);
            javaMailSender.setUsername("账号");
            javaMailSender.setPassword("密码");
            return javaMailSender;
        }
    }

      3)这个是采用注解的方式,自己也可以采用xml的配置方式,自己随意

      4)应用,邮件的发送

    package com.troy.jpa.util;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.mail.SimpleMailMessage;
    import org.springframework.mail.javamail.JavaMailSender;
    
    public class MailUtil {
    
        public static void main(String[] args) {
            sendMail("收件人邮箱","test","test");
        }
    
        public static void sendMail(String mail, String title, String text) {
            //加载容器
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring-mvc.xml");
            //获取bean
            JavaMailSender javaMailSender = (JavaMailSender) context.getBean("javaMailSender");
            //建立邮箱模板
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            //设置发送人的账号
            mailMessage.setFrom("发送人账号(和登录的账号一样)");
            //设置接收人的账号
            mailMessage.setTo(mail);
            //设置主题
            mailMessage.setSubject(title);
            //设置内容
            mailMessage.setText(text);
            //发送邮件
            javaMailSender.send(mailMessage);
        }
    }
  • 相关阅读:
    Azure 中的多个 VM NIC 和网络虚拟设备
    程序猿入职秘籍大分享!分分钟走上人生巅峰
    高考估分查分选志愿一键搞定_支付宝又操办了件人生大事
    三大新兴力量_引领中国消费新经济丨图媒体
    史上最全编程语言列表_你掌握了哪些?
    市值登顶亚洲后_马云对话全球投资者:与未来相比阿里还是个baby
    无法访问ECS云服务器怎么解决?
    快速搭建phpwind论坛系统
    Linux全自动安装面板脚本_支持目前比较流行的国内面板
    为阿里云ECS(Windows 2012)创建IPv6隧道地址
  • 原文地址:https://www.cnblogs.com/ll409546297/p/8205272.html
Copyright © 2020-2023  润新知