javaMail的使用以及trying to connect to host "1xxx@163.com", port 25, isSSL false异常
最近项目用到邮件系统,开始了解javaMail。。。话不多说先上代码:
pom依赖:
<!-- 邮件 https://mvnrepository.com/artifact/javax.mail/mail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.5.0-b01</version>
</dependency>
工具类:
import java.util.Date;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailUtils {
//发件人地址
private static String senderAddress = "1234567891@163.com";
//收件人地址
private static String recipientAddress = "123456789@qq.com";
//发件人账户
public static String senderAccount = "1234567891@163.com";
//发件人授权码
public static String auth = "xxxxxxx";
//邮件主题
public static String subject = "xxxxxx通知";
//邮件正文
public static String content = "您有一则xxxxx!";
public static void main(String [] args) throws MessagingException {
//1.连接邮件服务器的配置参数
Properties properties = new Properties();
//设置用户认证方式
properties.setProperty("mail.smtp.auth", "true");
//设置传输协议
properties.setProperty("mail.transport.protocol", "smtp");
//设置发件人的SMTP服务器地址
properties.setProperty("mail.smtp.host", "smtp.163.com");
//properties.setProperty("mail.smtp.port", "465");
//2.创建定义整个应用程序所需的环境信息的Session对象
Session session = Session.getInstance(properties);
session.setDebug(true);
//3.创建邮件的实例对象
MimeMessage msg = getMimeMessage(session);
//4.根据session对象获取邮件传输对象
Transport transport = session.getTransport("smtp");
transport.connect("smtp.163.com", senderAccount, auth);
//发送邮件,并发送到所有收件人地址,message.getAllRecipients()获取到创建又见对象时添加的所有收件人抄送人密送人
transport.sendMessage(msg, msg.getAllRecipients());
//如果只想发送给指定的人,可以如下写法
//transport.sendMessage(msg, new Address[]{new InternetAddress("xxx@qq.com")});
//5.关闭邮件链接
transport.close();
}
/**
* 獲得創建一封郵件的實例對象
* @param session
* @return
* @throws MessagingException
* @throws AddressException
*/
public static MimeMessage getMimeMessage(Session session) throws MessagingException {
//創建郵件的實例對象
MimeMessage msg = new MimeMessage(session);
//设置发件人地址
msg.setFrom(new InternetAddress(senderAddress));
/*
* 设置收件人地址(可以增加多个收件人,抄送人,密送)
* MimeMessage.RecipientType.TO 发送
* MimeMessage.RecipientType.CC 抄送
* MimeMessage.RecipientType.BCC 密送
*/
msg.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(recipientAddress));
//设置邮件主题
msg.setSubject(subject, "UTF-8");
//设置邮件正文
msg.setContent(content, "text/html;charset=UTF-8");
//设置邮件的发送时间默认立即发送
msg.setSentDate(new Date());
return msg;
}
}
需要注意首先发件人邮箱需要设置开启POP3/SMTP/IMAP协议
transport.connect("smtp.163.com", senderAccount, auth);这里的参数注意第三个不是密码
javaMail的使用以及trying to connect to host "1xxx@163.com", port 25, isSSL false异常的更多相关文章
- ssh: connect to host gitlab.alpha.com port 22: Network is unreachable
在这里只说明我遇到的问题和解决方法,可能并不能解决你遇到的问题: git clone git@gitlab.alpha.com:ipcam/ambarella.gitCloning into 'amb ...
- $ ssh -T git@github.com ssh: connect to host ssh.github.com port 22: Connection timed out
在C:/用户/用户名/.ssh中添加几个文件 之前的电脑生成都是四个文件,分别是 id_rsa id_rsa.pub config known_hosts 不知道为什么在另一台电脑上却生成两个文件 ...
- Could not connect to SMTP host: localhost, port: 25;
1.错误描写叙述 DEBUG: setDebug: JavaMail version 1.3.3 DEBUG: getProvider() returning javax.mail.Provider[ ...
- selenium在Eclipse中打开fireFox浏览器是报报错connect to host 127.0.0.1 on port 7055
1.相信很多同学刚接触selenium时,在Eclipse中打开fireFox浏览器是报报错: org.openqa.selenium.firefox.NotConnectedException: U ...
- Selenium2学习-038-firefox、webdriver版本不对称问题解决:org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055
今天有个朋友在群里问,为何脚本运行不通过,其脚本操作步骤简单描述如下: 1.启动火狐浏览器 2.打开百度 3.查询框输入关键字 4.点击按钮[百度一下] 脚本挺简单的,其给出的应用报错信息如下所示: ...
- Error -27780: [GENERAL_MSG_CAT_SSL_ERROR]connect to host "124.202.213.70" failed: [10054] Connection reset by peer [MsgId: MERR-27780]
解决方案一: 备注: 此方案如果请求响应时间太长,勾选"WinInet replay instead of Sockets(Windows only)"将会导致如下错误:
- 解决org.openqa.selenium.WebDriverException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms org.springframework.beans.BeanInstantiation
解决方法为将selenium-server-standalone-2.37.0.jar升级至selenium-server-standalone-2.41.0.jar即可. 下载地址:http://s ...
- WebDriver:org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms
今天尝试最新的webDriver与fireFox搭配: 运行代码时出现如下的问题,但是浏览器却可以打开: org.openqa.selenium.firefox.NotConnectedExcepti ...
- svn :Can't connect to host *.*.*.*': 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。
Can't connect to host *.*.*.*': 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败. -------------------------------- ...
随机推荐
- pycharm同步代码到coding
代码同步coding三步曲: 1.pycharm的tips---vcs---checkout from version control---git 选择git后会弹出clone repository弹 ...
- verilog 介绍
Verilog HDL Verilog HDL是在C语言的基础上发展起来的一种硬件描述语言,语法较自由.VHDL和Verilog HDL两者相比,VHDL的书写规则比Verilog HDL烦琐一些,但 ...
- Open SuSE 设置开关机时自动执行脚本
在open SuSE中,有两个文件是用于存放开机自动执行命令的.这两个文件分别是: /etc/init.d/before.local /etc/init.d/after.local /etc/init ...
- PHP批量保存图片
关于这个问题网上也有回答,我只是把我在工作中用到的代码分解出来,供需要的朋友使用.以下代码都已经经过测试,所以没有任何问题.有问题请加QQ:1127173874 第一部分 数据库表结构 create ...
- 视频剪辑软件调研:Adobe Premiere、会声会影、抖音短视频
Adobe Premiere.会声会影.抖音短视频基本功能特点对比: 特点 Adobe Premiere 会声会影 抖音短视频 运行平台 Win7/Win8/Win10.macOS Win7/Win ...
- swift 加载 本地html 和 网络路径
先上代码: xcode 9.4 ios 11.4 import UIKit import WebKit class RootViewController: UIViewController, WKN ...
- MATLAB 出一张好看的图
1.坐标轴的视点(viewpoint):从哪个方向看整个坐标系统,这决定了坐标轴的方向和位置,通过view函数实现视点的设置:view([z y ]):(将坐标系统想象为一座房子,而自己是个会飞的天使 ...
- Scratch 数字游戏
本想用Scratch给女儿做一个类似舒尔特方格的程序来认识数字和提升专注,想想这对刚刚3岁的孩子来说还是比较困难的,于是只做了3*3的方格,来认识数字1-9. 游戏地址:Random 9 v0.21 ...
- 怎样用Python的Scikit-Learn库实现线性回归?
来源商业新知号网,原标题:用Python的Scikit-Learn库实现线性回归 回归和分类是两种 监督 机器 学习算法, 前者预测连续值输出,而后者预测离散输出. 例如,用美元预测房屋的价格是回归问 ...
- CodeForces - 864C-Bus-(模拟加油站问题)
https://vjudge.net/problem/CodeForces-864C 题意:两地之间有个加油站,往返走k个单程,最少加油多少次. 大佬几十行代码就解决,我却要用一百多行的if语句模拟解 ...