程序入口:
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. Neo4j 2.0 生产环境集群搭建

    一.在windows上搭建Neo4j ha cluster的配置方法: 例如:建立集群的三台机器的ip分别为:10.230.9.91,10.230.9.92,10.230.9.93. 10.230.9 ...

  2. 自己定义View时,用到Paint Canvas的一些温故,简单的帧动画(动画一 ,&quot;掏粪男孩Gif&quot;顺便再提提onWindowFocusChanged)

    转载请注明出处:王亟亟的大牛之路 之前在绘画的过程中提到了静态的旋转啊,缩放啊,平移等一些效果.那么自己定义的View当然也有动态的效果也就是我们的Animation.经常使用的有三种 View An ...

  3. 给ajax表单提交数据前面加上实体名称

    有时候我们后台做了一个引用类型例如: 下面的实体以C#为例 public class Order{ public string orderId{get;set;} public OrderItem o ...

  4. ArcGIS读取dem格式数据

    DEM是GIS常用的一种数据,用来做各种分析.展示等,十分有用!它实质上就是一个栅格,只不过这个栅格值表示高程,常用的格式是tif,grid等.今天听到了另外一种说法:*.dem是最常见到的DEM的格 ...

  5. MSSQL 2005/2008 日志压缩清理方法小结

    适用于SQL Server 2005的方法 --------------------------------------------- 复制代码 代码如下: USE DNName GO 1,清理日志 ...

  6. WinForm 之 应用程序开机自启动设置方法

    一.原理 需要开机自启动的程序,需要将其启动程序的路径写到注册表中指定的文件夹下. 二.实现方式 方法1:在生成安装程序时配置: 方法2:在程序运行时动态配置. 三.在生成安装程序时配置 1.右击安装 ...

  7. C#通过代码调用PowerShell

    var userId = "MyAccount@XXXXX.partner.onmschina.cn"; var tenantId = "XXXXX-ca13-4bcb- ...

  8. tcmalloc asan

    http://blog.csdn.net/jinzhuojun/article/details/46659155 http://blog.csdn.net/hanlizhong85/article/d ...

  9. Mavlink地面站编写之二--Mission Planner编译

    软件下载:        本文使用VS2013进行编译和改动Mission Planner,其它版本号没有尝试过. 首先下载Mission Planner源码. https://github.com/ ...

  10. CentOS7中zip压缩和unzip解压缩命令详解

    安装zip.unzip应用 yum install zip unzip 以下命令均在/home目录下操作cd /home #进入/home目录1.把/home目录下面的mydata目录压缩为mydat ...