已使用 163 邮箱测试通过,且支持 SSL 连接。 发送邮件
示例:Jack 发送一封邮件给 Rose。
public class SendMail {
public static void main(String[] args) {
boolean isSSL = true;
String host = "smtp.163.com";
int port = 465;
String from = "jack@163.com";
String to = "rose@163.com";
boolean isAuth = true;
final String username = "jack@163.com";
final String password = "jack";
Properties props = new Properties();
props.put("mail.smtp.ssl.enable", isSSL);
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
props.put("mail.smtp.auth", isAuth);
Session session = Session.getDefaultInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("主题");
message.setText("内容");
Transport.send(message);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
System.out.println("发送完毕!");
}
}
收取邮件
示例:Rose 收取最近一封邮件。
import java.util.Date;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
public class FetchMail {
public static void main(String[] args) {
String protocol = "pop3";
boolean isSSL = true;
String host = "pop.163.com";
int port = 995;
String username = "rose@163.com";
String password = "rose";
Properties props = new Properties();
props.put("mail.pop3.ssl.enable", isSSL);
props.put("mail.pop3.host", host);
props.put("mail.pop3.port", port);
Session session = Session.getDefaultInstance(props);
Store store = null;
Folder folder = null;
try {
store = session.getStore(protocol);
store.connect(username, password);
folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
int size = folder.getMessageCount();
Message message = folder.getMessage(size);
String from = message.getFrom()[0].toString();
String subject = message.getSubject();
Date date = message.getSentDate();
System.out.println("From: " + from);
System.out.println("Subject: " + subject);
System.out.println("Date: " + date);
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} finally {
try {
if (folder != null) {
folder.close(false);
}
if (store != null) {
store.close();
}
} catch (MessagingException e) {
e.printStackTrace();
}
}
System.out.println("接收完毕!");
}
}
常用邮件协议
发送邮件:SMTP
收取邮件:POP3、IMAP
常用邮件配置项
| 配置项 | 说明 |
| mail.xxx.ssl.enable | 是否支持 SSL 连接 |
| mail.xxx.host | 邮件服务器主机名 |
| mail.xxx.port | 邮件服务器端口号 |
| mail.xxx.auth | 是否进行身份认证 |
说明:xxx 表示协议名称,例如:smtp、pop3 等。
默认端口号
| SMTP | POP3 | IMAP | |
| 普通方式 | 25 | 110 | 143 |
| SSL 方式 | 465 | 995 | 993 |
使用 Apache Commons Email 发送邮件
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
public class SendMail {
public static void main(String[] args) {
boolean isSSL = true;
String host = "smtp.163.com";
int port = 465;
String from = "jack@163.com";
String to = "rose@163.com";
String username = "jack@163.com";
String password = "jack";
try {
Email email = new SimpleEmail();
email.setSSLOnConnect(isSSL);
email.setHostName(host);
email.setSmtpPort(port);
email.setAuthentication(username, password);
email.setFrom(from);
email.addTo(to);
email.setSubject("主题");
email.setMsg("内容");
email.send();
} catch (EmailException e) {
e.printStackTrace();
}
System.out.println("发送完毕!");
}
}
参考:http://commons.apache.org/proper/commons-email/userguide.html
完
已使用 163 邮箱测试通过,且支持 SSL 连接。 发送邮件的更多相关文章
- ECMall如何支持SSL连接邮件服务器的配置
首先,主要是ecmall使用的phpmailer版本太低,不支持加密连接. 然后,得对相应代码做一定调整. 1. 覆盖phpmailer 请从附件进行下载: http://files.cnblogs. ...
- 弄了一个支持SSL的TCP客户端
近日需要做一些TCP的收发的调试,到网上去找TCP调试工具,找了好几款,发现不是功能不全就是不支持HEX,更重要的SSL也不支持,于是动手写了一款,叫TCPRunner,有以下特性: 使用异步IO方式 ...
- SSL邮件发送(腾讯企业邮箱测试通过,可以支持多附件)
参考网址:http://www.cnblogs.com/LUA123/p/5575134.html ,谢谢! package net.common.utils.common; import java. ...
- jeakins配置邮件通知,附带解决535报错:authentication failed,如果发现测试邮件可以发出,项目构成无法发出邮件,请开启SSL认证,端口号改为(465),qq邮箱、163邮箱通用
535报错解决方案:调用163邮箱服务器来发送邮件,我们需要开启POP3/SMTP服务,这时163邮件会让我们设置客户端授权码,这个授权码替代上面代码部分的passwd即可成功发送邮件 如果设置的邮箱 ...
- [转]Redmine 配置163邮箱
redmine的邮件发送功能还是很有用的.像项目有更新啦,任务分配啦,都能邮件发送的相关责任人.我自己在linux服务器上安装并启动了redmine后,邮件一直发送了不了.查了网上的资料,都是讲修改下 ...
- [Python爬虫] Selenium实现自动登录163邮箱和Locating Elements介绍
前三篇文章介绍了安装过程和通过Selenium实现访问Firefox浏览器并自动搜索"Eastmount"关键字及截图的功能.而这篇文章主要简单介绍如何实现自动登录163邮箱,同时 ...
- dedecms织梦自定义表单发送到邮箱-用163邮箱发送邮件
https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&tn=monline_3_dg&wd=dedecms 邮箱&oq=d ...
- selenium数据驱动模式实现163邮箱的登录及添加联系人自动化操作
项目结构如下: 要求python3.0 selenium3.0 下面是代码: appModubles:addContactPersonActtion.py和LoginAction.py addCont ...
- 利用Python+163邮箱授权码发送带附件的邮件
背景 前段时间写了个自动爬虫的脚本,定时在阿里云服务器上执行,会从某个网站上爬取链接保存到txt文本中,但是脚本不够完善,我需要爬虫完毕之后通过邮件把附件给我发送过来,之前写过一个<利用Pyth ...
随机推荐
- iOS UI基础-2.0按钮操作与形变
按钮状态 1.normal:默认状态 Default 对应的枚举常量:UIControlStateNormal 2.highlighted(高亮状态) 按钮被按下去的时候(未松开) 对应的枚举常量 ...
- unity3d-绘制贴图
准备贴图 在屏幕在绘制一张静态贴图,需要用到GUI.DrawTexture()方法, 该方法可以设定图片的显示位置.缩放比例和渲染混合等 /* Rect position:表示图片的绘制区域 * Te ...
- quartz-job实现实时或定时发送短信任务
存放调度器(Job 和 Trigger)信息的xml配置文件: 这是某个指定的要实现的定时任务: <!-- 每天给项目经理发送短信避免短信服务挂了 定时每天08:30执行--> <j ...
- Mocha describe 生命周期
1 describe('test', function() { 2 // 在本测试块的所有测试用例之前执行且仅执行一次 3 before(function() { 4 5 }); 6 // 在本测试块 ...
- Linux基础命令---zip
zip zip是一种最通用的文件压缩方式,使用于unix.msdos.windows.OS等系统.如果在编译zip时包含bzip 2库,zip现在也支持bzip 2压缩.当将大于4GB的文件添加到存档 ...
- web前端----jQuery事件
事件 常用事件 click(function(){...}) hover(function(){...}) blur(function(){...}) focus(function(){...}) c ...
- c++第五天:默认初始化
1.算数类型.(整型和浮点型) 类型决定了数据所占的比特数以及该如何解释这些比特的内容. 练习2.1... 各种类型在计算机中所占的比特数不同,解释方法不同.有符号要花费一个比特存储符号,最大正值要比 ...
- Confluence5.8部分空间名称显示为问号的解决方案
Confluence5.8部分空间名称显示为问号的解决方案 原因: 连接MySQL的时候,有没有在连接串中指定&useUnicode=true&characterEncoding=ut ...
- mamcached+magent构建memcached集群
cat /etc/redhat-release CentOS release 6.7 (Final) 防火墙.selinux 关闭 192.168.12.30 安装libevent和memcached ...
- 20145105 《Java程序设计》实验三总结
实验三 一. 实验内容 结对修改实验一代码,重点学习重构 二. 实验步骤 下载结伴同学的实验一代码 初始代码 进行整数.小数和负数的多组数据测试,发现一个运行错误的例子 分析后 ...