Java使用QQ邮箱发送纯文本格式信息

1.导包

<dependency>
   <groupId>com.sun.mail</groupId>
   <artifactId>javax.mail</artifactId>
   <version>1.6.2</version>
 </dependency>

2.开启POP3/SMTP服务,然后获取16位授权码

在这里插入图片描述

3.拷贝修改代码

    @Test
    void  testSendMail() throws Exception {
                //创建一个配置文件并保存
                Properties properties = new Properties();

                properties.setProperty("mail.host","smtp.qq.com");

                properties.setProperty("mail.transport.protocol","smtp");

                properties.setProperty("mail.smtp.auth","true");


                //QQ存在一个特性设置SSL加密
                MailSSLSocketFactory sf = new MailSSLSocketFactory();
                sf.setTrustAllHosts(true);
                properties.put("mail.smtp.ssl.enable", "true");
                properties.put("mail.smtp.ssl.socketFactory", sf);

                //创建一个session对象
                Session session = Session.getDefaultInstance(properties, new Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication("2223503679@qq.com","16位授权码");
                    }
                });

                //开启debug模式
                session.setDebug(true);

                //获取连接对象
                Transport transport = session.getTransport();

                //连接服务器
                transport.connect("smtp.qq.com","2223503679@qq.com","16位授权码");

                //创建邮件对象
                MimeMessage mimeMessage = new MimeMessage(session);

                //邮件发送人
                mimeMessage.setFrom(new InternetAddress("2223503679@qq.com"));

                //邮件接收人
                mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress("ntxz123@aliyun.com"));

                //邮件标题
                mimeMessage.setSubject("液位警告!!");

                //邮件内容
                mimeMessage.setContent("当前系统液位值2.0cm低于设定的警戒值2.1cm,请注意及时补充灌溉用水!!!","text/html;charset=UTF-8");

                //发送邮件
                transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());

                //关闭连接
                transport.close();
            }

4.想要发送其他格式请参照下方博客

https://blog.csdn.net/baolingye/article/details/96598222

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值