Java实现邮件发送(QQ邮箱例)

Java邮件发送(QQ邮箱例)

准备工作:电脑登陆QQ邮箱,设置–>账户–>开启SMTP服务,获得邮箱授权码,导入mail.jar

Java实现邮件发送(QQ邮箱例)

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.3</version>
</dependency>

SendMail.java

public class SendMail {

    private static final Properties PROPERTIES;
    private static final Session SESSION;

    static {
        //链接前准备,初始化参
        PROPERTIES = new Properties();
        //发送邮箱服务器
        PROPERTIES.setProperty("mail.smtp.host", "smtp.qq.com");
        //发送端口号
        // properties.setProperty("mail.smtp.port","1465");
        //打印信息到控制台
        PROPERTIES.setProperty("mail.debug", "true");
        //是否开启权限设置
        PROPERTIES.setProperty("mail.smtp.auth", "true");
        //发送协议为简单邮件传输协议
        PROPERTIES.setProperty("mail.transport", "smtp");
        //SSL加密
        PROPERTIES.setProperty("mail.smtp.ssl.enable", "true");
        //建立两点之间的连接
        SESSION = Session.getDefaultInstance(PROPERTIES, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                //设置发件人邮箱,通过密码认证
                return new PasswordAuthentication("发件人邮箱", "授权码");
            }
        });
    }

    //创建邮箱对象
    Message message = new MimeMessage(SESSION);
    public  void sendMail(String data){

        try {
            //设置发件人
            message.setFrom(new InternetAddress("发件人邮箱"));
            //设置收件人
           // message.setRecipient(Message.RecipientType.TO,new InternetAddress(name));
            //发送多人邮件
            //设置主题
            message.setSubject("验证码");
            //发送邮件正文及编码
            message.setContent(data,"text/html;charset=UTF-8");
            //时间
            message.setSentDate(new Date());
            //发送一封邮件
            Transport.send(message);

        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
    //单发
   public void setRecipient(String name){
        try {
            message.setRecipient(Message.RecipientType.TO,new InternetAddress(name));
        } catch (MessagingException e) {
            e.printStackTrace();
        }

   }
   //多发
   public void setRecipient(List<String > names){
       try {
           for (String name :names){
               //addRecipient
               message.addRecipient(Message.RecipientType.TO,new InternetAddress(name));
           }

       } catch (MessagingException e) {
           e.printStackTrace();
       }
   }
}

Test .java

public class Test {
    public static void main(String[] args) {
        int ym =(int) (1000+Math.random()*(9999-1+1));
        Random random = new Random();
        int r = random.nextInt(9000)+1000;
        SendMail sendMail = new SendMail();
        List<String> list = new ArrayList<String>();
        list.add("收件人1邮箱");
        list.add("收件人2邮箱");
       // sendMail.setRecipient("收件人邮箱");
        sendMail.setRecipient(list);//添加收件人
        sendMail.sendMail("<h1>来自悠米的验证码邮件,请接收你的验证码:" +
              "</h1><p>你的验证码是:"+ym+",请妥善保管你的验证码!</p>");
    }
}
上一篇:基于Nonebot2搭建QQ机器人(一)机器人环境配置


下一篇:制作QQ登录界面,vue数据双向绑定