Giter Club home page Giter Club logo

qqemail's Introduction

准备工作

开启PO3/SMTP服务 打开qq邮箱>账户 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 ==记住这串授权码 会用到==

java发送qq邮箱实现步骤

  1. 创建一个javase的工程 在这里插入图片描述
  2. 新建一个lib文件夹 放邮件发送所需要的架包 架包链接:链接:https://pan.baidu.com/s/17m0nA7UDjgHoi9mbFDSrag 提取码:2faa

在这里插入图片描述 3. 编写核心代码 我这里封装了 也可以不封装 在这里插入图片描述

/**
 * @author joker_dj
 * @create 2020-04-13日 18:43
 */
public class Email {

    public void qqemai(String QQmail,String head,String body) throws AddressException, MessagingException {
        Properties properties = new Properties();
        properties.put("mail.transport.protocol", "smtp");// 连接协议
        properties.put("mail.smtp.host", "smtp.qq.com");// 主机名
        properties.put("mail.smtp.port", 465);// 端口号
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.ssl.enable", "true");// 设置是否使用ssl安全连接 ---一般都使用
        properties.put("mail.debug", "true");// 设置是否显示debug信息 true 会在控制台显示相关信息
        // 得到回话对象
        Session session = Session.getInstance(properties);
        // 获取邮件对象
        Message message = new MimeMessage(session);
        // 设置发件人邮箱地址
        message.setFrom(new InternetAddress("****@qq.com"));
        // 设置收件人邮箱地址
        message.setRecipients(Message.RecipientType.TO,
                new InternetAddress[] { new InternetAddress(QQmail) });
        //new InternetAddress();设置同时发送多个好友
        // 设置邮件标题
        message.setSubject(head);
        // 设置邮件内容
        message.setText(body);
        // 得到邮差对象
        Transport transport = session.getTransport();
        // 连接自己的邮箱账户
        transport.connect("****@qq.com", "*****授权码");// 密码为QQ邮箱开通的stmp服务后得到的客户端授权码
        // 发送邮件
        int i=0;
        transport.sendMessage(message, message.getAllRecipients());
        System.out.println("成功!");
        transport.close();
    }
}
  1. 编写main方法 在这里插入图片描述
public class qqmain {
    public static void main(String[] args) throws MessagingException {
        Email qq=new Email();
        Scanner input=new Scanner(System.in);
        System.out.println("请输入QQ号");
        String QQmail=input.next()+"@qq.com";
        System.out.println("请输入要发送的标题");
        String head=input.next();
        System.out.println("请输入要发送的文本");
        String body=input.next();
        qq.qqemai(QQmail, head, body);
    }
}
  1. 运行测试 在这里插入图片描述 在这里插入图片描述 测试成功

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.