发送邮件基于QQ邮箱

1.pom依赖

<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.7</version>
</dependency>



<!-- https://mvnrepository.com/artifact/javax.activation/activation -->
<dependency>
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1.1</version>
</dependency>


2.打开QQ POP3/SMTP服务并得到授权码


3 代码

1设置

 Properties properties = new Properties();
        properties.setProperty("mail.host","smtp.qq.com");//设置QQ服务器
        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);

2步骤

1
         //使用JavaMail发送邮件需以下五步骤
        //1.创建所需Session对象
        Session session =Session.getDefaultInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("自己的邮箱","生成授权码");
            }
        });
        //是否开启调试 有信息输出,不写没有信息输出
        session.setDebug(true);

2
 //2.通过session对象得到得到transport对象
        Transport ts = session.getTransport();
3
       //3.使用邮箱的用户名和密码和授权码连上服务器
        ts.connect("smtp.qq.com","自己的邮箱","授权码");
4
 //4.创建邮件
        MimeMessage message = new MimeMessage(session);
        //指明发件人
        message.setFrom(new InternetAddress("1355847796@qq.com"));
        //指明收件人
        message.setRecipient(Message.RecipientType.TO,new InternetAddress("2150399417@qq.com"));
        //邮件的标
        message.setSubject("你好");
        //邮件的文本内容
        //准备图片数据
        MimeBodyPart image = new MimeBodyPart();
        //处理图片
        DataHandler dataHandler = new DataHandler(new FileDataSource("D:\\IDEA\\projects\\file_upload\\mail-java\\src\\1.PNG"));
        image.setDataHandler(dataHandler);
        image.setContentID("wsx");
        //准备正文数据
        MimeBodyPart text = new MimeBodyPart();
        text.setContent("<h1>你好,你的验证码为</h1>"+"<h1 style='color:red'>"+s+"</h1><p>"+"<img src='cid:wsx'>"+"</p>","text/html;charset=UTF-8");

        //描述数据关系
        MimeMultipart mm = new MimeMultipart();
        mm.addBodyPart(text);
        mm.addBodyPart(image);
        //有附件就设置成Mixed
        mm.setSubType("related");
        message.setContent(mm);
        message.saveChanges();
         //简单发送
        // message.setContent("<h1>你好,你的验证码为</h1>"+"<h1 style='color:red'>"+s+"</h1>","text/html;charset=UTF-8");
5
 //5.发送邮件
        ts.sendMessage(message,message.getAllRecipients());
6
  //6.关闭连接
        ts.close();
7
//生成验证码
private static String verifCode(){

        Random r = new Random();
        String s = r.nextInt(9999) + "";
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i <4-s.length() ; i++) {

            stringBuffer.append("0");
        }
        String s1 = s + stringBuffer;

        return s1;
    }