java使用Socket实现SMTP协议发送邮件
作者:IamOkay 发布时间:2022-06-08 19:25:50
标签:java,Socket,SMTP,邮件
本文实例为大家分享了java 利用Socket实现SMTP协议发送邮件的具体代码,供大家参考,具体内容如下
package mail;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.codec.binary.Base64;
public class Mail {
public static void main(String[] args) throws IOException {
Mail mail = new Mail();
mail.setSmtpServer("smtp.qq.com");
mail.setFromMail("1344364****@qq.com");
mail.addToMail("105648****@qq.com");
mail.addToMail("long*****@sina.com");
mail.setUserName("134364****");
mail.setPassword("*************");
mail.setSubject("测试邮件");
mail.setContent("<h1>你好</h1><br/><img src=\"https://www.baidu.com/img/baidu_jgylogo3.gif?v=39549282.gif\" />");
mail.setShowLog(true);
mail.send();
System.out.println("程序结束");
}
/** 邮件主题 **/
private String subject;
/** 从此地址发出 **/
private String fromMail;
/** 用户名 **/
private String userName;
/** 登录密码 **/
private String password;
/** SMTP 服务器地址 **/
private String smtpServer;
/** SMTP 服务器端口(默认:25) **/
private int smtpPort = 25;
/** 发送到 toMail 中的所有地址 **/
private List<String> toMail;
/** 邮件内容 **/
private String content;
/** 是否显示日志 **/
private boolean showLog;
public void addToMail(String mail) {
if (toMail == null)
toMail = new ArrayList<String>();
toMail.add(mail);
}
public void send() {
if (smtpServer == null) {
throw new RuntimeException("smtpServer 不能为空");
}
if (userName == null) {
throw new RuntimeException("userName 不能为空");
}
if (password == null) {
throw new RuntimeException("password 不能为空");
}
if (fromMail == null) {
throw new RuntimeException("fromMail 不能为空");
}
if (toMail == null || toMail.isEmpty()) {
throw new RuntimeException("toMail 不能为空");
}
if (content == null || toMail.isEmpty()) {
throw new RuntimeException("content 不能为空");
}
Socket socket = null;
InputStream in = null;
OutputStream out = null;
try {
socket = new Socket(smtpServer, smtpPort);
socket.setSoTimeout(3000);
in = socket.getInputStream();
out = socket.getOutputStream();
} catch (IOException e) {
throw new RuntimeException("连接到 " + smtpServer + ":" + smtpPort + " 失败", e);
}
BufferedReaderProxy reader = new BufferedReaderProxy(new InputStreamReader(in), showLog);
PrintWriterProxy writer = new PrintWriterProxy(out, showLog);
reader.showResponse();
writer.println("HELO " + smtpServer);
reader.showResponse();
writer.println("AUTH LOGIN");
reader.showResponse();
writer.println(new String(Base64.encodeBase64(userName.getBytes())));
reader.showResponse();
writer.println(new String(Base64.encodeBase64(password.getBytes())));
reader.showResponse();
writer.println("MAIL FROM:" + fromMail);
reader.showResponse();
for (String mail : toMail) {
writer.println("RCPT TO:" + mail);
reader.showResponse();
}
writer.println("DATA");
writer.println("Content-Type:text/html");
if (subject != null) {
writer.println("Subject:" + subject);
}
writer.println("From:" + fromMail);
writer.print("To:");
for (String mail : toMail) {
writer.print(mail + "; ");
}
writer.println();
writer.println();
writer.println(content);
writer.println(".");
reader.showResponse();
writer.println("QUIT");
reader.showResponse();
try {
socket.close();
} catch (IOException e) {
System.err.println("发送邮件完成,关闭 Socket 出错:" + e.getMessage());
}
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getFromMail() {
return fromMail;
}
public void setFromMail(String fromMail) {
this.fromMail = fromMail;
}
public String getSmtpServer() {
return smtpServer;
}
public void setSmtpServer(String smtpServer) {
this.smtpServer = smtpServer;
}
public int getSmtpPort() {
return smtpPort;
}
public void setSmtpPort(int smtpPort) {
this.smtpPort = smtpPort;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public List<String> getToMail() {
return toMail;
}
public void setToMail(List<String> toMail) {
this.toMail = toMail;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean getShowLog() {
return showLog;
}
public void setShowLog(boolean showLog) {
this.showLog = showLog;
}
static class PrintWriterProxy extends PrintWriter {
private boolean showRequest;
public PrintWriterProxy(OutputStream out, boolean showRequest) {
super(out, true);
this.showRequest = showRequest;
}
@Override
public void println() {
if (showRequest)
System.out.println();
super.println();
}
public void print(String s) {
if (showRequest)
System.out.print(s);
super.print(s);
}
}
static class BufferedReaderProxy extends BufferedReader {
private boolean showResponse = true;
public BufferedReaderProxy(Reader in, boolean showResponse) {
super(in);
this.showResponse = showResponse;
}
public void showResponse() {
try {
String line = readLine();
String number = line.substring(0, 3);
int num = -1;
try {
num = Integer.parseInt(number);
} catch (Exception e) {
}
if (num == -1) {
throw new RuntimeException("响应信息错误 : " + line);
} else if (num >= 400) {
throw new RuntimeException("发送邮件失败 : " + line);
}
if (showResponse) {
System.out.println(line);
}
} catch (IOException e) {
System.out.println("获取响应失败");
}
}
}
}
0
投稿
猜你喜欢
- 抽象类(abstract):抽象类不能创建实例,它只能作为父类被继承。抽象类是从多个具体类中抽象出来的父类,它具有更高层次的抽象。从多个具有
- 前几天在这里分享了手写 sql 分页查询实现分页,现在来看看使用 mybatis 分页插件 pagehepler 来实现分页使用分页插件的原
- 目录概述c#方法概述在微信支付中,当用户支付成功后,微信会把相关支付结果和用户信息发送给商户,商户需要接收处理,并返回应答。接收微信支付异步
- feignclient https接口调用报证书错误问题最近在使用 feignclient 过程中,和第三方通过https 协议交互的时候,
- 若使用 Spring IoC 容器(ApplicationContext或BeanFactory)作为你的业务对象(你也应该这么做!),你会
- 现在有一张订单表t_stockorder,其拥有id、code、client_id、merchandise_id、merchandise_n
- 本文实例为大家分享了java实现简单的猜数字的具体代码,供大家参考,具体内容如下题目描述:猜数字(又称 Bulls and Cows )是一
- Mybatis防止sql注入原理SQL 注入是一种代码注入技术,用于攻击数据驱动的应用,恶意的SQL 语句 * 入到执行的实体字段中(例如,为
- 一、工具类代码public class TaskHelper {#region 多线程操作 &nbs
- 依赖配置结合前面的内容,这里我们要嵌入数据库的操作,这里以操作MySQL为例整合Mybatis,首先需要在原来的基础上添加以下依赖<!
- 二进制数据一般输入的格式是0x45, 0x3a, 0xc3, 这种数据格式看起来是16进制的字符串,但是实际上在存储的时候每个都对应一个字节
- 安装jdk(介绍三种方法)查看java版本:java -version方法一:利用yum源来安装jdk(此方法不需要配置环境变量)查看yum
- Java super关键字super 关键字与 this 类似,this 用来表示当前类的实例,super 用来表示父类。super 可以用
- DAO模式是接口的一个典型应用。1. StudenDaoListImpl.java与StudentDaoArrayImpl.java有何不同
- 一.需求使用JAVA实现单链表,使用单链表检测字符串是否是回文串二.需求分析回文串最重要的就是对称,那么最重要的问题就是找到那个中心,用快指
- Kotlin中的面向对象面向对象面向对象的含义大家应该并不陌生,通过将事物抽象成对象,大大简化了程序的开发难度。我们常用的Java、Pyth
- 简述偶然看到一篇关于阿里新orm框架的文章,好奇的点了进去。开发后端多年,看到这个还是有点兴奋的。常用mysql的orm框架mybatis、
- 原因:给定的流不支持mark和reset就会报这个错误。获取到一个网络流,这个网络流不允许读写头来回移动,也就不允许mark/reset机制
- Eclipse的应用需要众多的插件,但是Eclipse的插件大家又知道多少呢?“Eclipse最牛的30个插件”不知道看官们是否了解,51C
- 目录一、什么是Spring二、什么是IOC三、快速搭建框架环境四、spring之依赖注入五、详解Spring框架的IOC之注解方式七、Spr