最近做一个小项目要用到JAVA的邮箱的发送功能。遇到一些坑这里记录分享一下:QQ群交流:697028234

1、QQ邮箱一定要设置开通SMTP/POP这项。并生成授权码。

2、用MAVEN生成一个QUICKSTART项目,测试用的哈。加入依赖如下:

<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>

3、在main方法里加入如下代码测试成功的哈。

package com.sendmail.sendmail_test;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage; /**
* Hello world!
*
*/
public class App
{
public static void main( String[] args ) throws MessagingException
{ // 配置发送邮件的环境属性
final Properties props = new Properties();
/*
* 可用的属性: mail.store.protocol / mail.transport.protocol / mail.host /
* mail.user / mail.from
*/
// 表示SMTP发送邮件,需要进行身份验证
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.qq.com");
props.put("mail.transport.protocol", "smtp");
props.put("mail.debug", "true");
//遇到最多的坑就是下面这行,不加要报“A secure connection is requiered”错。
props.put("mail.smtp.starttls.enable", "true");
// 发件人的账号
props.put("mail.user", "XXXX@qq.com");
// 访问SMTP服务时需要提供的密码
props.put("mail.password", "这里是QQ邮箱授权码"); // 构建授权信息,用于进行SMTP进行身份验证
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// 用户名、密码
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
// 使用环境属性和授权信息,创建邮件会话
Session mailSession = Session.getInstance(props, authenticator);
// 创建邮件消息
MimeMessage message = new MimeMessage(mailSession);
// 设置发件人
InternetAddress form = new InternetAddress(
props.getProperty("mail.user"));
message.setFrom(form); // 设置收件人
InternetAddress to = new InternetAddress("3489417@qq.com");
message.setRecipient(RecipientType.TO, to); // 设置抄送,抄送和密送如果不写正确的地址也要报错。最好注释不用。
// InternetAddress cc = new InternetAddress("");
// message.setRecipient(RecipientType.CC, cc);
//
// // 设置密送,其他的收件人不能看到密送的邮件地址
// InternetAddress bcc = new InternetAddress("");
// message.setRecipient(RecipientType.CC, bcc); // 设置邮件标题
message.setSubject("JAVA测试邮件"); // 设置邮件的内容体
message.setContent("<a href='http://www.XXX.org'>测试的邮件</a>", "text/html;charset=UTF-8"); // 发送邮件
Transport.send(message); }
}

使用QQ邮箱SMTP服务的javamail配置的更多相关文章

  1. java代码发送邮箱验证码与qq邮箱smtp服务

    发送邮箱的类封装,在此之前需要一个jar包  javax.mail.jar 下载链接https://github.com/javaee/javamail/releases/download/JAVAM ...

  2. C#如何利用QQ邮箱SMTP发送邮件

    public void SendEmail() { MailMessage msg = new MailMessage(); msg.To.Add("to@qq.com");//收 ...

  3. Python Django中QQ邮箱授权码问题

    Python Django中QQ邮箱授权码问题 系统及软件版本如下: Ubuntu Kylin 16.04 Python 3.5.1 Django 1.9.7 PyCharm Community Ed ...

  4. SpringBoot2.x整合JavaMail以qq邮箱发送邮件

    本文参考spring官网email接口文档所写. spring-email官方网址:https://docs.spring.io/spring/docs/5.1.8.RELEASE/spring-fr ...

  5. jenkins配置QQ邮箱自动发送RF测试构建结果通知邮件

    声明:转载请注明出处,谢谢 首先确认QQ邮箱SMTP服务器的地址和端口号.如下图所示,请谨记,JENKINS全局邮箱配置需要使用: 步骤1:开启QQ邮箱的smtp服务:登陆QQ邮箱-设置-账户-开启P ...

  6. C#使用SMTP协议发送验证码到QQ邮箱

    C#使用SMTP协议发送验证码到QQ邮箱 在程序设计中,发送验证码是常见的一个功能,用户在注册账号时或忘记密码后,通常需要发送验证码到手机短信或邮箱来验证身份,此篇博客介绍在C#中如何使用SMTP协议 ...

  7. C# WinForm 使用SMTP协议发送QQ邮箱验证码

    文章来自:https://blog.csdn.net/IT_xiao_guang_guang/article/details/104336604 前言   在程序设计中,发送验证码是常见的一个功能,用 ...

  8. PHP 利用QQ邮箱发送邮件「PHPMailer」

    在 PHP 应用开发中,往往需要验证用户邮箱.发送消息通知,而使用 PHP 内置的 mail() 函数,则需要邮件系统的支持. 如果熟悉 IMAP/SMTP 协议,结合 Socket 功能就可以编写邮 ...

  9. TP5实现邮件发送(PHP 利用QQ邮箱发送邮件「PHPMailer」)

    在 PHP 应用开发中,往往需要验证用户邮箱.发送消息通知,而使用 PHP 内置的 mail() 函数,则需要邮件系统的支持. 如果熟悉 IMAP/SMTP 协议,结合 Socket 功能就可以编写邮 ...

随机推荐

  1. Feed系统架构资料收集(转)

    add by zhj:有些链接已经失效,后续会修改. 原文:http://blog.csdn.net/zhangzhaokun/article/details/7834797 完全用nosql轻松打造 ...

  2. Design Pattern in Simple Examples

    Instead of defining what is design pattern lets define what we mean by design and what we mean by pa ...

  3. vsftpd文件服务器安装与配置

    -d<登入目录>:指定用户登入时的启始目录:. -s<shell>:指定用户登入后所使用的shell: /sbin/nologin指的是不允许login当前Linux系统.当用 ...

  4. Mirror--镜像相关操作

    其他相关操作1. 关闭镜像--关闭镜像USE [master]GOALTER DATABASE Demo1 SET PARTNER OFFGO  2. 证服务器--移除见证服务器USE [master ...

  5. Linux(CentOS)系统下搭建svn服务器

    由于GitHub的私有项目需要收费,gitlab对服务器的要求必须是4GB内存以上.对于一些个人的小型项目,想要免费的版本控制工具来管理自己的代码,又不想代码公开,无疑SVN是比较好的选择.windo ...

  6. An error occurred: No action handlers found - check JMeterHome and libraries

    An error occurred: No action handlers found - check JMeterHome and libraries Writing log file to: D: ...

  7. this指向 - 浏览器环境

    1.全局上下文中的 this <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

  8. RESTful源码笔记之RESTful Framework的APIview, Viewset总结分析

    0x00  引言 官方文档:http://www.django-rest-framework.org/drf为我们提供强大的通用view的功能,本博客对这些view进行简要的总结分析.首先,我们看一下 ...

  9. 前端学习笔记之CSS文档流

    先引用一段W3C的文档: 9.3 Positioning schemes In CSS 2.1, a box may be laid out according to three positionin ...

  10. EL表达式判断条件要写在${}内

    由于老没开发框架了,今天提取公共省市县三级联动组件时,使用jsp传参搞了半天才弄出来. 组件代码 $(function(){console.log("${param.init}") ...