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

 更新时间:2014年01月03日 09:33:38   作者:  
本文为你介绍了java mail使用qq邮箱发邮件的方法,大家参考使用吧

程序入口:
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,到此为止。

相关文章

  • Spring Boot深入分析讲解日期时间处理

    Spring Boot深入分析讲解日期时间处理

    项目中使用LocalDateTime系列作为DTO中时间的数据类型,但是SpringMVC收到参数后总报错,为了配置全局时间类型转换,尝试了如下处理方式
    2022-06-06
  • SpringMVC HttpMessageConverter消息转换器

    SpringMVC HttpMessageConverter消息转换器

    ​​HttpMessageConverter​​​,报文信息转换器,将请求报文转换为Java对象,或将Java对象转换为响应报文。​​​HttpMessageConverter​​​提供了两个注解和两个类型:​​@RequestBody,@ResponseBody​​​,​​RequestEntity,ResponseEntity​​
    2023-04-04
  • MyBatis标签之Select resultType和resultMap详解

    MyBatis标签之Select resultType和resultMap详解

    这篇文章主要介绍了MyBatis标签之Select resultType和resultMap,在MyBatis中有一个ResultMap标签,它是为了映射select标签查询出来的结果集,下面使用一个简单的例子,来介绍 resultMap 的使用方法,需要的朋友可以参考下
    2022-09-09
  • SpringBoot2 整合 ClickHouse数据库案例解析

    SpringBoot2 整合 ClickHouse数据库案例解析

    这篇文章主要介绍了SpringBoot2 整合 ClickHouse数据库案例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-10-10
  • jmeter压力测试工具简介_动力节点Java学院整理

    jmeter压力测试工具简介_动力节点Java学院整理

    这篇文章主要为大家详细介绍了jmeter压力测试工具相关介绍资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08
  • Java基础教程之包(package)

    Java基础教程之包(package)

    这篇文章主要介绍了Java基础教程之包(package),本文详细讲解了包的创建、使用等方法,需要的朋友可以参考下
    2014-08-08
  • 解决运行jar包出错:ClassNotFoundException问题

    解决运行jar包出错:ClassNotFoundException问题

    这篇文章主要介绍了解决运行jar包出错:ClassNotFoundException问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • Spring security实现对账户进行加密

    Spring security实现对账户进行加密

    这篇文章主要介绍了Spring security实现对账户进行加密,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • selenium-java实现自动登录跳转页面方式

    selenium-java实现自动登录跳转页面方式

    利用Selenium和Java语言可以编写一个脚本自动刷新网页,首先,需要确保Google浏览器和Chrome-Driver驱动的版本一致,通过指定网站下载对应版本的浏览器和驱动,在Maven项目中添加依赖,编写脚本实现网页的自动刷新,此方法适用于需要频繁刷新网页的场景,简化了操作,提高了效率
    2024-11-11
  • Java中的Cookie和Session详细解析

    Java中的Cookie和Session详细解析

    这篇文章主要介绍了Java中的Cookie和Session详细解析,客户端会话技术,服务端给客户端的数据,存储于客户端(浏览器),由于是保存在客户端上的,所以存在安全问题,需要的朋友可以参考下
    2024-01-01

最新评论