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 *.*.*.*': 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败. -------------------------------- ...
随机推荐
- 关于在Python3.6下安装MySQL-python,flask-sqlalchemy模块的问题
这周末在学习Flask框架的时候,有需要安装MySQL-python模块,一开始用pip安装: pip install MySQL-python 但是安装的时候报错了: error: command ...
- django之关联field 描述子是如何实现的
model定义时,每个field都是一个类属性,一个对象.在生成类时,属性有contribute_to_class的方法,会调用该方法. m2m field,它会先调用自己的contribute_to ...
- resin4.0服务器远程调试说明
1.开发环境介绍: 目前dev用resin 作为服务容器.服务调用之间过于复杂,要进行本地代码调试过于复杂,甚至无法进行.但是可以通过jvm的远程代码调试来达到本地代码调试的目的,步骤如下. 2.re ...
- HTTP 响应代码
https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Status HTTP 响应状态代码指示特定 HTTP 请求是否已成功完成.响应分为五类:信息响应, ...
- python框架django-web层
1.demo 命令行:django-admin startproject HelloWorld 命令执行成功后会根据模版生产一个django项目 然后通过 命令:python3 manage.py r ...
- 【转】如何使用离线博客发布工具发布CSDN的博客文章
目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...
- mac 下直接给docker容器加映射 mysql 为例
如果你是下面这种情况,本文可能回给你一些帮助 os是Mac,docker中已有mysql容器,并且已经有数据,但是没有设置映射,想要从主机连接docker 中的mysql,以便更好的查看,增加,删除数 ...
- 【1天】黑马程序员27天视频学习笔记【Day02】
02.01常量的概述和使用 * A:什么是常量 * 在程序执行的过程中其值不可以发生改变 * B:Java中常量的分类 * 字面值常量 * 自定义常量(面向对象部分讲) * C:字面 ...
- Android 获取版本号名称工具类
package com.example.grenaderose.redthunder.utils; import android.content.Context; import android.con ...
- Codeforces Round #485 (Div. 2)
Codeforces Round #485 (Div. 2) https://codeforces.com/contest/987 A #include<bits/stdc++.h> us ...