⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mailclient.java

📁 基于SMTP的邮件发送
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;


class Mailclient{
	private int intPort = 25;
	private String stringSmtpServer = "10.30.30.218";
	private Frame f;
	private Label label1;
	private Label label2;
	private Label label3;
	private Label label4;
	private TextField input1;
	private TextField input2;
	private TextField input3;
	TextField input4;
	private Button button1;
	private Button button2;
	private Button button3;
	private Panel p1,p2,p3,p4;
	
	public void sendCommandCheck(Socket smtp,BufferedReader smtp_in,DataOutputStream smtp_out,String command,int success_code)
	throws IOException{
		smtp_out.writeBytes(command + "\r\n");
		smtp_out.flush();
		System.out.println("send>" + command);
		resultCheck(smtp,smtp_in,smtp_out,success_code);		
	}
	
	public void resultCheck(Socket smtp,BufferedReader smtp_in,DataOutputStream smtp_out,int success_code)
	throws IOException{
		String res = smtp_in.readLine();
		System.out.println("recv>" + res);
		if(Integer.parseInt(res.substring(0,3)) != success_code){
			smtp.close();
			throw new RuntimeException(res);
			}
	}
	
	public void send(String stringFrom,String stringTo,String stringSubject,String stringMessage)
	throws IOException{
		Socket smtp = new Socket(stringSmtpServer,intPort);
		BufferedReader smtp_in =
				new BufferedReader(new InputStreamReader(smtp.getInputStream()));
		DataOutputStream smtp_out =
				new DataOutputStream(smtp.getOutputStream());
		resultCheck(smtp,smtp_in,smtp_out,220);
		
		String myname = InetAddress.getLocalHost().getHostName();
		sendCommandCheck(smtp,smtp_in,smtp_out,"HELLO " + stringSmtpServer,250);
		sendCommandCheck(smtp,smtp_in,smtp_out,"mail from:" + stringFrom,250);
		sendCommandCheck(smtp,smtp_in,smtp_out,"rcpt to:" + stringTo,250);
		sendCommandCheck(smtp,smtp_in,smtp_out,"DATA",354);
		smtp_out.writeBytes("From:" + stringFrom + "\r\n");
		smtp_out.writeBytes("To:" + stringTo + "\r\n");
		smtp_out.writeBytes("Subject:" + stringSubject + "\r\n");
		System.out.println("send>" + "Subject:" + stringSubject);
		smtp_out.writeBytes("\r\n");
		smtp_out.writeBytes(stringMessage + "\r\n");
		System.out.println("send>" + stringMessage);
		sendCommandCheck(smtp,smtp_in,smtp_out,".",250);
		sendCommandCheck(smtp,smtp_in,smtp_out,"quit",221);
		smtp.close();
	}
	
	
	
	
public Mailclient() {
		f = new Frame("SMTP客户端");	
		label1 = new Label("发送邮箱:");
		label2 = new Label("接收邮箱:");
		label3 = new Label("邮件主题:");
		label4 = new Label("邮件内容:");
		input1 = new TextField(40);
		input2 = new TextField(40);
		input3 = new TextField(40);
		input4 = new TextField(40);
		button1 = new Button("发送");
		button2 = new Button("清除");
		button3 = new Button("退出");
  }
  
  public void launchFrame(){
  	f.addWindowListener(this.new QuitHandler());
 	
  	f.setLayout(new FlowLayout());  	
  	f.add(label1);
  	f.add(input1);
  	f.add(label2);
  	f.add(input2); 
  	f.add(label3);
  	f.add(input3); 
  	f.add(label4); 
  	f.add(input4); 
  	
  	button1.addActionListener(this.new EnterHandler());
  	
  	f.add(button1);  	
  	
  	button2.addActionListener(this.new StopHandler());
  	
  	f.add(button2); 	
  	
  	button3.addActionListener(this.new QuitHandler());
  	
  	f.add(button3);
  	f.setSize(400,200); 	
  	f.setVisible(true);
  }
  
  class EnterHandler implements ActionListener{
  	public void actionPerformed(ActionEvent e){  			
  			String stringFrom = input1.getText();
  			String stringTo = input2.getText();
  			String stringSubject = input3.getText();
  			String stringMessage = input4.getText();
  			try{
  				send(stringFrom,stringTo,stringSubject,stringMessage);
  			}catch(Exception ec){System.out.println(ec);}
  			
  	}
  }
  
  class StopHandler implements ActionListener{
  	public void actionPerformed(ActionEvent e){
  		input1.setText("");
  		input2.setText("");
  		input3.setText("");
  		input4.setText("");
  	}
  }
  
  class QuitHandler extends WindowAdapter implements ActionListener{
  	public void actionPerformed(ActionEvent e){
  		System.out.println("SMTP客户端已经退出……");
  		System.exit(0);
  	}
  	public void windowClosing(WindowEvent e){
  		System.out.println("SMTP客户端已经退出……");
  		System.exit(0);
  	}
  }
  
  
public static void main(String args[]){
  	Mailclient gs = new Mailclient();
  	gs.launchFrame();  	
  }
     
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -