资源简介
通过Socket发送邮件的Java小程序
代码片段和文件信息
package com.vk.socket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import sun.misc.base64Encoder;
public class SMTPEmail {
private Socket socket;
private boolean needAuth;
private boolean debug = false;
base64Encoder encode = new base64Encoder();
public static void main(String[] args) {
String server = “smtp.163.com“;
MailMessage message = new MailMessage();
message.setFrom(“******@163.com“);
message.setTo(“******@qq.com“);
message.setSubject(“test“);
message.setContent(“test“);
message.setDatafrom(“******@163.com“);
message.setDatato(“******@qq.com“);
message.setUser(“******“);
message.setPassword(“******“);
try {
SMTPEmail smtp = new SMTPEmail(server true 25);
boolean flag = smtp.sendMail(message server);
if (flag) {
System.out.println(“send success“);
} else {
System.out.println(“send failed“);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public SMTPEmail(String server boolean needAuth int port)
throws UnknownHostException IOException {
try {
socket = new Socket(server port);
this.needAuth = needAuth;
} catch (SocketException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
public boolean sendMail(MailMessage message String server) {
try {
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
helo(server in out);// HELO
if(needAuth){
authLogin(message in out);// AUTH LOGIN
}
mailfrom(message.getFrom() in out);
String[] str= message.getTo().split(““);
for(int i=0;i rcpt(str[i] in out);
}
data(message.getDatafrom() message.getDatato()
message.getSubject() message.getContent() in out);
quit(in out);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public void helo(String server InputStream in OutputStream out)
throws IOException {
int result;
result = response(in);
if (result != 220) {
throw new IOException(“failed“);
}
result = sendServer(“HELO “ + server in out);
if (result != 250) {
throw new IOException(“failed“);
}
}
private int sendServer(String str InputStream in OutputStream out)
throws IOException {
out.write(str.getBytes());
sendNewline(out);
out.flush();
if (debug) {
System.out.println(“command:“ + str);
}
return response(in);
}
private void authLogin(MailMessage message InputStream in OutputStream out)
throws IOException {
int result;
result = sendServer(“AUTH LOGIN“ in out);
if (result != 334) {
throw new IOException(“AUTH
- 上一篇:android微信分享demo
- 下一篇:java 选择保存文件的路径
相关资源
- java 选择保存文件的路径
- 学生管理系统SSM
- Java用户文件分类管理系统
- SpringBoot 注解
- kerberos的java实现
- java实现爬取指定网站的数据源码
- java开发坦克大战_eclipse
- java移动文件夹下所有文件
- JavaSocket建立连接和发送消息.zip
- java基于socket公共聊天室的实现.zip
- java jsp登陆代码 用jsp跟mysql一起实现
- JAVA+sqlserver数据库酒店管理系统
- java实现自动扫描文件夹txt文档插入数
- 达内2019,Java全集视频,百度云资源,
- java基础练习代码
- javaismy.txt
- java is my .txt
- javacv-platform-1.5.1-bin.zip
- 《Java从入门到精通》第4版光盘资源
- eclipse Java EE 2018包
- java架构师视频
-
使用java解析xm
l文件,解析完之后将 - 韩顺平2017最新java
- 网上书店Java
- 金蝶笔试.txtsql+java
- Java 打开并显示图片
- java 多线程模拟钟表 首创
- java实现的简单的按照文件名检索文件
- Java从入门到精通视频+Java架构师vip视
- java,产生一个8乘8的0、1矩阵,并判断
评论
共有 条评论