程序入口:
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. android Logger 一二三

    我们在开发Android应用的过程中可以很方便地使用Log信息来调试程序,这都归功于Android的Logger驱动为用户层提供的Log支持.无论是底层的源代码还是上层的应用,我们都可以使用Logge ...

  2. Structured Streaming编程向导

    简介 Structured Streaming is a scalable and fault-tolerant stream processing engine built on the Spark ...

  3. 如何对 GIT 分支进行规划?

    项目背景: 该项目是在2011年11月份使用Asp.net三层帮荷兰某个客户开发的机票预定系统 该客户主要是做中国与欧洲的旅行社业务,特别是最近两年由于中国的发展因此客户也越来越重视机票业务 于是他们 ...

  4. How to Build a Search Page with Elasticsearch and .NET

    Although SQL Server's Full-Text search is good for searching text that is within a database, there a ...

  5. Windows 环境 cygwin 安装 SSH

    本文内容 安装环境 安装 cygwin 安装 SSH 服务 启动 sshd 服务 SSH 免密码登录 验证 SSH 是否已安装成功 验证 SSH 是否可以免密码登录本机 安装环境 Windows 20 ...

  6. Elasticsearch - 理解字段分析过程(_analyze与_explain)

    我们经常会遇到问题.为什么指定的文档没有被搜索到.许多情况下, 这都归因于映射的定义和分析例程配置存在问题. 针对分析过程的调试,ElasticSearch提供了专用的REST API. _analy ...

  7. 牛客网-《剑指offer》-斐波那契数列

    题目:http://www.nowcoder.com/practice/c6c7742f5ba7442aada113136ddea0c3 C++ class Solution { public: in ...

  8. Want to write a book? Use word count to stay on track

    http://paloalto.patch.com/groups/maria-murnanes-blog/p/bp--want-to-write-a-book-use-word-count-to-st ...

  9. LevelDB初体验测试

    最近工作需要找一个能使用磁盘存储数据,对写要求比较苛刻,需要每秒达100000TPS,读的时候需要能10000TPS左右,不能占用太多内存.单节点满足这个要求的常见有Redis.Memcached等, ...

  10. Linux下验证码无法显示,Could not initialize class sun.awt.X1 解决方案

    环境:Oracle Linux 6.4,JDK1.6,Weblogic11g 在通过java.awt生成图片验证码时,提示: Could not initialize class sun.awt.X1 ...