import java.io.*;
import java.net.*;
import java.util.*;
public class SMTPClientDemo
{
protected int port = 25;
protected String hostname = "localhost";
protected String from = "";
protected String to = "";
protected String subject = "";
protected String body = "";
protected Socket socket;
protected BufferedReader br;
protected PrintWriter pw;
// Constructs a new instance of the SMTP Client
public SMTPClientDemo() throws Exception
{
try
{
getInput();
sendEmail();
}
catch (Exception e)
{
System.out.println ("Error sending message - " + e);
}
}
public static void main(String[] args) throws Exception
{
// Start the SMTP client, so it can send messages
SMTPClientDemo client = new SMTPClientDemo();
}
// Check the SMTP response code for an error message
protected int readResponseCode() throws Exception
{
String line = br.readLine();
System.out.println("< "+line);
line = line.substring(0,line.indexOf(" "));
return Integer.parseInt(line);
}
// Write a protocol message both to the network socket and to the screen
protected void writeMsg(String msg) throws Exception
{
pw.println(msg);
pw.flush();
System.out.println("> "+msg);
}
// Close all readers, streams and sockets
protected void closeConnection() throws Exception
{
pw.flush();
pw.close();
br.close();
socket.close();
}
// Send the QUIT protocol message, and terminate connection
protected void sendQuit() throws Exception
{
System.out.println("Sending QUIT");
writeMsg("QUIT");
readResponseCode();
System.out.println("Closing Connection");
closeConnection();
}
// Send an email message via SMTP, adhering to the protocol known as RFC 2821
protected void sendEmail() throws Exception
{
System.out.println("Sending message now: Debug below");
System.out.println("--------------------------------------------------------------");
System.out.println("Opening Socket");
socket = new Socket(this.hostname,this.port);
System.out.println("Creating Reader & Writer");
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
System.out.println("Reading first line");
int code = readResponseCode();
if(code != 220) { socket.close(); throw new Exception("Invalid SMTP Server"); }
System.out.println("Sending helo command");
writeMsg("HELO "+InetAddress.getLocalHost().getHostName());
code = readResponseCode();
if(code != 250)
{
sendQuit();
throw new Exception("Invalid SMTP Server");
}
System.out.println("Sending mail from command");
writeMsg("MAIL FROM:<"+this.from+">");
code = readResponseCode();
if(code != 250)
{
sendQuit();
throw new Exception("Invalid from address");
}
System.out.println("Sending rcpt to command");
writeMsg("RCPT TO:<"+this.to+">");
code = readResponseCode();
if(code != 250)
{
sendQuit();
throw new Exception("Invalid to address");
}
System.out.println("Sending data command");
writeMsg("DATA");
code = readResponseCode();
if(code != 354)
{
sendQuit();
throw new Exception("Data entry not accepted");
}
System.out.println("Sending message");
writeMsg("Subject: "+this.subject);
writeMsg("To: "+this.to);
writeMsg("From: "+this.from);
writeMsg("");
writeMsg(body);
code = readResponseCode();
sendQuit();
if(code != 250)
throw new Exception("Message may not have been sent correctly");
else
System.out.println("Message sent");
}
// Obtain input from the user
protected void getInput() throws Exception
{
// Read input from user console
String data=null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// Request hostname for SMTP server
System.out.print("Please enter SMTP server hostname: ");
data = br.readLine();
if (data == null || data.equals("")) hostname="localhost";
else
hostname=data;
// Request the sender's email address
System.out.print("Please enter FROM email address: ");
data = br.readLine();
from = data;
// Request the recipient's email address
System.out.print("Please enter TO email address :");
data = br.readLine();
if(!(data == null || data.equals("")))
to=data;
System.out.print("Please enter subject: ");
data = br.readLine();
subject=data;
System.out.println("Please enter plain-text message ('.' character on a blank line signals end of message):");
StringBuffer buffer = new StringBuffer();
// Read until user enters a . on a blank line
String line = br.readLine();
while(line != null)
{
// Check for a '.', and only a '.', on a line
if(line.equalsIgnoreCase("."))
{
break;
}
buffer.append(line);
buffer.append("\n");
line = br.readLine();
}
buffer.append(".\n");
body = buffer.toString();
}
}
smtp.rar_smtp java_stmp
版权申诉
148 浏览量
2022-09-22
18:06:15
上传
评论
收藏 2KB RAR 举报
小贝德罗
- 粉丝: 70
- 资源: 1万+
最新资源
- 华为云会议安装包适配华为擎云L410 -1060 ARM架构
- 清空redis缓存脚本指令
- STM8S微控制器-寄存器操作指南
- apk一键安装,获取apk签名信息
- opencv4.8.0+contrib+cuda文件
- 【全网最新最全】大气公司年度月度总结汇报PPT
- C语言实现低功耗STM32F411开发板(原理图+PCB源文件+官方例程+驱动等).zip
- C语言实现基于STM32 的联合调试侦听设备解决方案(原理图、PCB源文件、调试工具、视频).zip
- 【全网最全最酷】部门年度年终工作总结汇报PPT模板
- C语言实现基于STM32F103RC的电子相册(原理图、PCB源文件、程序源码及制作).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈