邮件发送原理及实现

邮件发送原理及其实现

1.到QQ邮箱中获取授权码

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MPLwpq1y-1632660494369)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210926202505576.png)]开启POP3/SMTP服务,复制授权码,注意不要外泄

2.编写代码

package com.chi;

import com.sun.corba.se.impl.protocol.giopmsgheaders.Message;
import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import java.util.Properties;

public class MailDemo01 {
    public static void main(String[] args) throws Exception {
        Properties prop = new Properties();
        prop.setProperty("mail.host","smtp.qq.com");//设置qq邮箱服务器
        prop.setProperty("mail.transport.protocol","smtp");//邮件发送协议
        prop.setProperty("mail.stmp.auth","ture");//需要验证用户名和密码

        //关于QQ邮箱,还要设置SSL加密,加上以下代码即可,其他邮箱不需要!
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        prop.put("mail.smtp.ssl.enable","ture");
        prop.put("mail.smtp.ssl.socketFactory",sf);

        //使用javaMail发送邮件的5个步骤
        //1.创建定义整个程序需要的环境信息的Session对象

        //QQ才有 其他邮箱不用
        Session session = Session.getDefaultInstance(prop, new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication(){
                //发件人邮件用户名`授权码
                return  new PasswordAuthentication("327658159@qq.com","jjibivrwhmvrbjgb");
            }
        });
        //开启Session的DeBug模式,可以查看Email的运行状态
        session.setDebug(true);

        //2.通过session得到transport对象
        Transport ts = session.getTransport();

        // 3.使用邮箱的用户名和授权码链接上邮箱服务器
        ts.connect("smtp.qq.com","327658159@qq.com","jjibivrwhmvrbjgb");
        //4.创建邮件
        //注意需要传递Session
        MimeMessage message = new MimeMessage(session);

        //指明邮件的发送人
        message.setFrom(new InternetAddress("327658159@qq.com"));
        //指明邮件的收件人,现在收件人和发件人是一样的,那就是给自己发的
        message.setRecipient(javax.mail.Message.RecipientType.TO,new InternetAddress("953750219@qq.com"));
        //邮件的标题
        message.setSubject("欢迎学习java");
        //邮件的文本内容
        message.setContent("<h1 style='color:red'>恭喜您注册成功,您的用户名:404rapper您的密码:123,请妥善保管密码,如有问题请联系网站客服!!<h1>","text/html;charset=UTF-8");

        //5.发送邮件
        for (int i =0; i <100 ; i++) {
            ts.sendMessage(message,message.getAllRecipients());
        }


        //6.关闭连接
        ts.close();


    }
}




      


  

3.注意事项

在狂神的代码最后的ts.sendMessage(message,message.getAllRecipients());我加了一个for循环一百次,想实现一个邮箱轰炸效果.但是QQ邮箱似乎有限制的,发送25条之后就不能再发送了,控制台显示Invalid Addresses

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值