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 = "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邮箱配置 实例的更多相关文章
- JAVA 实现 QQ 邮箱发送验证码功能(不局限于框架)
JAVA 实现 QQ 邮箱发送验证码功能(不局限于框架) 本来想实现 QQ 登录,有域名一直没用过,还得备案,好麻烦,只能过几天再更新啦. 先把实现的发送邮箱验证码更能更新了. 老规矩,更多内容在注释 ...
- java实现qq邮箱每天定时发送邮件
本周四的时候去学校的某机构值班,主要工作是帮老师送文件,干一些杂活.那天没有什么活儿,于是想起用Java实现发送邮件和接收邮件的功能.前几天接触过一点quartz框架,用来实现定时开始任务的功能.于是 ...
- JAVA 使用qq邮箱发送邮件
引入一个架包: gradle( "com.sun.mail:javax.mail:1.5.6", ) 代码如下: private static final String QQ_EM ...
- yii2 qq邮箱配置发送
'mailer' => [ 'class' => 'yii\swiftmailer\Mailer', 'useFileTransport' =>false,//这句一定有,false ...
- Jakarta Java Mail属性参数配置
前言 Jakarta Mail网址:https://eclipse-ee4j.github.io/mail SMTP协议可匹配的属性:https://eclipse-ee4j.github.io/ma ...
- Spring Boot Mail通过QQ邮箱发送邮件
本文将介绍如何在Spring Boot工程完成QQ邮箱配置,实现邮件发送功能. 一.在pom文件中添加依赖 <dependency> <groupId>org.springfr ...
- SpringBoot 2.x 集成QQ邮箱、网易系邮箱、Gmail邮箱发送邮件
在Spring中提供了非常好用的 JavaMailSender接口实现邮件发送,在SpringBoot的Starter模块中也为此提供了自动化配置. 项目源码已托管在Gitee-SpringBoot_ ...
- CentOS7安装GitLab、汉化、邮箱配置及使用
同步首发:http://www.yuanrengu.com/index.php/20171112.html 一.GitLab简介 GitLab是利用Ruby On Rails开发的一个开源版本管理系统 ...
- CentOS7安装GitLab、汉化、邮箱配置及使用(转载)
同步首发: https://www.cnblogs.com/heyonggang/p/7778203.html http://www.yuanrengu.com/index.php/20171112. ...
随机推荐
- uva 10712 - Count the Numbers(数位dp)
题目链接:uva 10712 - Count the Numbers 题目大意:给出n,a.b.问说在a到b之间有多少个n. 解题思路:数位dp.dp[i][j][x][y]表示第i位为j的时候.x是 ...
- 【Spark】SparkStreaming-加载外部配置文件
SparkStreaming-加载外部配置文件 spark加载配置文件_百度搜索 Spark加载外部配置文件 - CSDN博客 spark读取配置文件中的配置 - CSDN博客 spark加载prop ...
- 学习世界模型,通向AI的下一步:Yann LeCun在IJCAI 2018上的演讲
https://baijiahao.baidu.com/s?id=1606296521706399213&wfr=spider&for=pc 机器之心整理,机器之心编辑部. 人工智能顶 ...
- Kafka:ZK+Kafka+Spark Streaming集群环境搭建(六)针对spark2.2.1以yarn方式启动spark-shell抛出异常:ERROR cluster.YarnSchedulerBackend$YarnSchedulerEndpoint: Sending RequestExecutors(0,0,Map(),Set()) to AM was unsuccessful
Spark以yarn方式运行时抛出异常: [spark@master bin]$ cd /opt/spark--bin-hadoop2./bin [spark@master bin]$ ./spark ...
- PKU-2104-K-th Number
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 36045 Accepted: 11522 Ca ...
- 转:C++操作mysql方法总结(1)
原文:http://www.cnblogs.com/joeblackzqq/p/4332945.html C++通过mysql的c api和通过mysql的Connector C++ 1.1.3操作m ...
- JavaScript 上下文环境和作用域,以及 call、apply 和 bind【转载+翻译+整理】
--看到这篇文章,翻译国外的,虽说写得有点矫情,但总体来看,还是相当不错的- 本文内容 我在哪儿?你又是谁 ? this? 用 apply 和 call 掌控上下文环境 bind 之美 本文将说明上下 ...
- jquery如何获取元素的滚动高度
获取浏览器显示区域(可视区域)的高度 : $(window).height(); 获取浏览器显示区域(可视区域)的宽度 : $(window).width(); 获取页面的文档高度 $(documen ...
- 简单实用的extend对象合并
/** * 合并对象 * 示例:o = extend({ a: 'a' }, o); */ function extend(s, t) { if (!s) { return {}; } if (!s) ...
- 详解JAVA输出Hello World
想必大家对这一段JAVA代码一定不会陌生: public class Test { public static void main(String[] args) { System.out.printl ...