📄 smtpexample.java
字号:
//SmtpExample.java
//导入被使用的类
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.Socket;
public class SmtpExample {
//默认的SMTP服务使用25端口
public static final int SMTP_PORT = 25;
//与SMTP服务器的连接相关的输出流
public static BufferedWriter out;
//与SMTP服务器的连接相关的输入流
public static BufferedReader in;
//方法printToOutputStream()向SMTP服务器发送命令
private static void printToOutputStream(String words) throws IOException {
out.write(words);
out.flush();
System.out.println("S : " + words);
}
//方法readFromInputStream()接收从SMTP服务器发回的响应信息
private static String readFromInputStream() throws IOException {
//读取服务器传回的响应信息
String sentence = in.readLine();
//响应信息开头为文本表示的状态码
//2,3开头代表正确或目前正确,出现其它状态码则视为出现错误
//如550代表无此用户,无法登陆
if (!sentence.startsWith("2") && !sentence.startsWith("3")) {
//打印错误信息
System.err.print("Error :");
System.err.println(sentence);
}
//日志输出
System.out.println("R : " + sentence);
return sentence;
}
//从邮件地址中取出主机名
public static String getDestHost(String mail) throws MalformedURLException {
int at = mail.indexOf("@");
if (at < 0)
throw new MalformedURLException("MailAddress : " + mail + " is not valid.");
//邮件地址中@符号后的为主机名
return mail.substring(at + 1);
}
//从邮件地址中取出用户名
public static String getSenderName(String mail) throws MalformedURLException {
int at = mail.indexOf("@");
if (at < 0)
throw new MalformedURLException("MailAddress : " + mail + " is not valid.");
//邮件地址中@符号前面的为用户名
return mail.substring(0, at);
}
public static void main(String[] args) {
Socket s = null;
try {
//构着从终端读取键盘输入的读者对象,从终端读取邮件信息
BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
//从终端读取邮件发送者地址
System.out.print("Mail From: ");
String from = cin.readLine();
//从终端读取邮件接受者地址
System.out.print("Mail To: ");
String to = cin.readLine();
//从终端读取邮件主题
System.out.print("SMTP Host: ");
String smtphost = cin.readLine();
System.out.println("Message body, end with Ctrl-D");
String line, message = "";
while (true) {
line = cin.readLine();
if (line.length() == 0)
break;
message = message + line + "\n";
}
//准备发送邮件
System.out.println("Prepare to send.");
//建立套接字连接到SMTP服务器
s = new Socket(smtphost, SMTP_PORT);
//获取相关的输入流
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
//获取相关的输出流
out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//读取欢迎信息
String response = readFromInputStream();
//发送HELLO命令
printToOutputStream("HELLO" + "\n");
//读取响应
response = readFromInputStream();
//发送邮件头
printToOutputStream("MAIL FROM:" + from + "\n");
response = readFromInputStream();
printToOutputStream("RCPT TO:" + to + "\n");
response = readFromInputStream();
//发送邮件主体
printToOutputStream("DATA\n");
response = readFromInputStream();
printToOutputStream(message + "\n.\n");
//读取响应信息
response = readFromInputStream();
//发送完毕,QUIT命令退出
printToOutputStream("QUIT\n");
//关闭连接
s.close();
System.out.println("Message sent.");
System.out.flush();
} catch (Exception e) {
//异常处理,异常信息输出到标准错误流中
e.printStackTrace(System.err);
System.out.println("Usage: java SmtpExample");
if (null != s)
try {
//如果发送过程中产生异常,在退出前也好关闭连接
s.close();
} catch (IOException ioe) {
ioe.printStackTrace(System.err);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -