程序入口:
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 = "1215186706@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("1215186706@qq.com", "password");
javax.mail.Session sendMailSession = javax.mail.Session.getDefaultInstance(properties, authenticator);
MimeMessage mailMessage = new MimeMessage(sendMailSession);
mailMessage.setFrom(new InternetAddress("1215186706@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=1215186706@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,到此为止。

java mail qq邮箱配置 实例的更多相关文章

  1. JAVA 实现 QQ 邮箱发送验证码功能(不局限于框架)

    JAVA 实现 QQ 邮箱发送验证码功能(不局限于框架) 本来想实现 QQ 登录,有域名一直没用过,还得备案,好麻烦,只能过几天再更新啦. 先把实现的发送邮箱验证码更能更新了. 老规矩,更多内容在注释 ...

  2. java实现qq邮箱每天定时发送邮件

    本周四的时候去学校的某机构值班,主要工作是帮老师送文件,干一些杂活.那天没有什么活儿,于是想起用Java实现发送邮件和接收邮件的功能.前几天接触过一点quartz框架,用来实现定时开始任务的功能.于是 ...

  3. JAVA 使用qq邮箱发送邮件

    引入一个架包: gradle( "com.sun.mail:javax.mail:1.5.6", ) 代码如下: private static final String QQ_EM ...

  4. yii2 qq邮箱配置发送

    'mailer' => [ 'class' => 'yii\swiftmailer\Mailer', 'useFileTransport' =>false,//这句一定有,false ...

  5. Jakarta Java Mail属性参数配置

    前言 Jakarta Mail网址:https://eclipse-ee4j.github.io/mail SMTP协议可匹配的属性:https://eclipse-ee4j.github.io/ma ...

  6. Spring Boot Mail通过QQ邮箱发送邮件

    本文将介绍如何在Spring Boot工程完成QQ邮箱配置,实现邮件发送功能. 一.在pom文件中添加依赖 <dependency> <groupId>org.springfr ...

  7. SpringBoot 2.x 集成QQ邮箱、网易系邮箱、Gmail邮箱发送邮件

    在Spring中提供了非常好用的 JavaMailSender接口实现邮件发送,在SpringBoot的Starter模块中也为此提供了自动化配置. 项目源码已托管在Gitee-SpringBoot_ ...

  8. CentOS7安装GitLab、汉化、邮箱配置及使用

    同步首发:http://www.yuanrengu.com/index.php/20171112.html 一.GitLab简介 GitLab是利用Ruby On Rails开发的一个开源版本管理系统 ...

  9. CentOS7安装GitLab、汉化、邮箱配置及使用(转载)

    同步首发: https://www.cnblogs.com/heyonggang/p/7778203.html http://www.yuanrengu.com/index.php/20171112. ...

随机推荐

  1. js中各种跨域问题实战小结

    什么是跨域?为什么要实现跨域呢?   这是因为JavaScript出于安全方面的考虑,不允许跨域调用其他页面的对象.也就是说只能访问同一个域中的资源.我觉得这就有必要了解下javascript中的同源 ...

  2. PHP文件操作[总结]

    1.前言 工作中涉及到数据处理,后台需要用到PHP处理数据,之前没有接触过PHP,借此机会了解了一下PHP,PHP很方便,很灵活,编码很舒服,很喜欢用PHP处理后台数据.今天总结一下php文件操作,主 ...

  3. Arrow functions and the ‘this’ keyword

    原文:https://medium.freecodecamp.org/learn-es6-the-dope-way-part-ii-arrow-functions-and-the-this-keywo ...

  4. Kafka:ZK+Kafka+Spark Streaming集群环境搭建(十五)Spark编写UDF、UDAF、Agg函数

    Spark Sql提供了丰富的内置函数让开发者来使用,但实际开发业务场景可能很复杂,内置函数不能够满足业务需求,因此spark sql提供了可扩展的内置函数. UDF:是普通函数,输入一个或多个参数, ...

  5. OpenGL中的原子操作需要注意的地方

    OpenGL中的原子操作需要注意的地方 仔细阅读看画红线的部分

  6. JavaScript代码不执行

    一天先后有两个同事问我为啥他的js代码出现了莫名其妙的问题 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "h ...

  7. Jlink烧写出错 : Unable to halt arm core

    环境:TQ2440开发板,J-link 通过J-link向TQ2440开发板的Nor Flash烧写程序,执行烧写时出错:Unable to halt arm core-详情如下图所示: 解决办法: ...

  8. Java多线程-BlockingQueue-ArrayBlockingQueue-LinkedBlockingQueue

    前言: BlockingQueue很好的解决了多线程中,如何高效安全“传输”数据的问题.通过这些高效并且线程安全的队列类,为我们快速搭建高质量的多线程程序带来极大的便利.本文详细介绍了Blocking ...

  9. Linux(CentOS6.X)安装mysql

    首先执行rpm -qa|grep -i mysql检查一下是否已经安装过mysql mysql-libs-5.1.73-5.el6_6.x86_64 可见已经安装了库文件,应该先卸载,不然会出现覆盖错 ...

  10. 使用SpringBoot创建Web项目

    1.pom.xml 引入 <parent> <groupId>org.springframework.boot</groupId> <artifactId&g ...