java mail使用qq邮箱发邮件的配置方法

时间:2023-07-02 07:58:56 

程序入口:
Test_Email_N.java


import java.io.IOException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class Test_Email_N {
    public static void  main(String args[]){
        try {
            send_email();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void send_email() throws IOException, AddressException, MessagingException{

        String to = "1219999@qq.com";
        String subject = "subject";
        String content = "content";
        Properties properties = new Properties();
        properties.put("mail.smtp.host", "smtp.qq.com");
        properties.put("mail.smtp.port", "25");
        properties.put("mail.smtp.auth", "true");
        Authenticator authenticator = new Email_Authenticator("1219999@qq.com", "password");
        javax.mail.Session sendMailSession = javax.mail.Session.getDefaultInstance(properties, authenticator);
        MimeMessage mailMessage = new MimeMessage(sendMailSession);
        mailMessage.setFrom(new InternetAddress("1219999@qq.com"));
        // Message.RecipientType.TO属性表示接收者的类型为TO
        mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
        mailMessage.setSubject(subject, "UTF-8");
        mailMessage.setSentDate(new Date());
        // MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
        Multipart mainPart = new MimeMultipart();
        // 创建一个包含HTML内容的MimeBodyPart
        BodyPart html = new MimeBodyPart();
        html.setContent(content.trim(), "text/html; charset=utf-8");
        mainPart.addBodyPart(html);
        mailMessage.setContent(mainPart);
        Transport.send(mailMessage);
    }
}

其中依赖的jar包为javax.mail,我这里是maven管理的,直接用maven去下载jar包,也可以到https://java.net/projects/javamail/pages/Home直接下载jar包.


<dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.5.0-b01</version>
        </dependency>


Email_Authenticator.java,这里继承了Authenticator 类,用来封装name,和password的:


package com.infomorrow.webtest.JuxinliTest.restdetect;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class Email_Authenticator extends Authenticator {
    String userName = null;
    String password = null;
    public Email_Authenticator() {
    }
    public Email_Authenticator(String username, String password) {
        this.userName = username;
        this.password = password;
    }
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(userName, password);
    }
}

配置就这么多,把邮箱密码改成自己的就可以了,否则会报错。程序到这就可以运行了!

下面介绍的是配置properties文件来管理账号密码:

新建一个email.propertis文件。

email.propertis:


mail.smtp.host=smtp.qq.com
mail.smtp.port=25
username=1219999@qq.com
password=password

Test_Email.java 代码改为如下:


package com.infomorrow.webtest.JuxinliTest.restdetect;


import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class Test_Email {

public static void main(String args[]){
        try {
            send_email();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void send_email() throws IOException, AddressException, MessagingException{

        String to = "1215186706@qq.com";
        String subject = "subject";//邮件主题
        String content = "content";//邮件内容
        Properties properties = new Properties();
        InputStream resourceAsStream = null;
        try {
             resourceAsStream = Object.class.getResourceAsStream("/email.properties");
            properties.load(resourceAsStream);
        } finally{
            if (resourceAsStream!=null) {
                resourceAsStream.close();
            }
        }
        System.err.println("properties:"+properties);
        properties.put("mail.smtp.host", properties.get("mail.smtp.host"));
        properties.put("mail.smtp.port", properties.get("mail.smtp.port"));
        properties.put("mail.smtp.auth", "true");
        Authenticator authenticator = new Email_Authenticator(properties.get("username").toString(), properties.get("password").toString());
        javax.mail.Session sendMailSession = javax.mail.Session.getDefaultInstance(properties, authenticator);
        MimeMessage mailMessage = new MimeMessage(sendMailSession);
        mailMessage.setFrom(new InternetAddress(properties.get("username").toString()));
        // Message.RecipientType.TO属性表示接收者的类型为TO
        mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
        mailMessage.setSubject(subject, "UTF-8");
        mailMessage.setSentDate(new Date());
        // MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
        Multipart mainPart = new MimeMultipart();
        // 创建一个包含HTML内容的MimeBodyPart
        BodyPart html = new MimeBodyPart();
        html.setContent(content.trim(), "text/html; charset=utf-8");
        mainPart.addBodyPart(html);
        mailMessage.setContent(mainPart);
        Transport.send(mailMessage);
    }
}

ok,到此为止。

标签:qq邮箱发邮件
0
投稿

猜你喜欢

  • Java 蒙特卡洛算法求圆周率近似值实例详解

    2023-10-19 23:32:10
  • springboot前端传参date类型后台处理的方式

    2023-04-12 16:47:35
  • android底层去掉虚拟按键的实例讲解

    2022-01-29 17:53:14
  • iOS实现从背景图中取色的代码

    2023-07-06 15:18:32
  • java面向对象之人机猜拳小游戏

    2021-12-20 18:12:34
  • 全面解析Hibernate关联操作、查询操作、高级特性、并发处理机制

    2021-06-25 08:48:48
  • c# 开机启动项的小例子

    2022-11-30 02:16:43
  • 关于各种排列组合java算法实现方法

    2023-11-15 05:46:55
  • 基于Rxjava实现轮询定时器

    2021-12-27 07:22:10
  • java数字转汉字工具类详解

    2023-04-28 02:00:26
  • unity shader实现较完整光照效果

    2023-04-27 07:43:29
  • Android中LinearLayout布局的常用属性总结

    2023-11-23 17:09:37
  • java.text.DecimalFormat用法详解

    2022-09-30 03:02:27
  • C#使用第三方组件生成二维码汇总

    2023-10-03 22:15:21
  • C#中Convert.ToDecimal()报错问题的解决

    2022-05-09 06:05:56
  • 详解C#中的System.Timers.Timer定时器的使用和定时自动清理内存应用

    2022-11-07 14:09:54
  • Mybatis Lombok使用方法与复杂查询介绍

    2023-03-30 00:30:13
  • 谈C# using的用法与好处

    2022-02-10 08:20:01
  • Mybatis打印替换占位符后的完整Sql教程

    2023-11-08 22:56:13
  • java代理模式(静态代理、动态代理、cglib代理)

    2022-11-22 16:12:49
  • asp之家 软件编程 m.aspxhome.com