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 *.*.*.*': 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败. -------------------------------- ...
随机推荐
- Python第9天
迭代器(减少代码量增强可读性)和生成器(只能遍历一次):遵循迭代器协议,就是生成可迭代对象 生产者和消费者模型:可触发生成器运行的方法 直接调用next方法 用系统next+生成器的方法 send y ...
- 7Linux存储结构和磁盘划分
FHS yum的.repo的配置文件多个的话,是依次生效吗? /boot 开机所需文件—内核.开机菜单以及所需配置文件等/dev 以文件形式存放任何设备与接口/etc 配置文件/home 用户主目录/ ...
- SpringBoot多模块项目打包问题
项目结构图如下: 在SpringBoot多模块项目打包时遇见如下错误: 1.repackage failed: Unable to find main class -> [Help 1] 解决步 ...
- C++重写new和delete,比想像中困难
关于C++内存管理这话题,永远都不过时.在我刚出道的时候,就已经在考虑怎么检测内存泄漏(https://www.cnblogs.com/coding-my-life/p/3985164.html).想 ...
- spring加载配置新旧方式对比
老方式 1.首先要配置配置文件,如beans.xml,内容如下: <?xml version="1.0" encoding="UTF-8"?> &l ...
- win7 升级Power Shell到4.0
因为用到EntityFrameworkCore ,想使用scaffold 来生成models. 提示我power Shell 2.0不支持命令,然后需要升级PS. PS win7 升级文件下载地址是 ...
- border三角形
border:100px solid ; box-shadow: inset 0 1px,inset 1px 0px,inset 0 -1px,inset -1px 0px; width:0px; h ...
- vue---canvas实现二维码和图片合成的海报
应项目需求,要一张宣传页面上加一个太阳码合成一张宣传海报,用户用微信可以识别进入微信小程序. 1. npm安装 npm install html2canvas --save //html转canv ...
- Lua + Redis 解决高并发
一.业务背景 优惠券业务主要提供用户领券和消券的功能:领取优惠券的动作由用户直接发起,由于资源有限,我们必须对用户的领取动作进行一些常规约束. 约束1(优惠券维度): 券的最大数量 max: 约束2( ...
- phpstorm中open in browser端口和路径设置
phpstorm默认的端口号是:63342但是我装的apache服务器的默认端口是80网上查找资料,都说可以加listen的端口,比如这里 #Listen 12.34.56.78:80Listen 8 ...