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

📄 smtpmail.java

📁 用JAVA编写的多线程TCP SOCKET程序
💻 JAVA
字号:
package Pro123;
import java.io.*;
import java.net.Socket;
import java.util.*;

public class SmtpMail {
	private boolean sendConf=false;
	public static final int OK = 1;
	public static final int ERROR = 0;
	private static final String TEXT = "1";
	private static final String TFILE = "2";
	private static final String BFILE = "3";
	private static final String CPR = "Java 1.0";
	private static final String MAILER = "X-Mailer";
	private static final int BUFFER_SIZE = 48;
	private String DELIMETER;
	private String SEPARATOR;
	private static final int HOW_LONG = 6;
	private static final char SMTP_ERROR_CODE1 = 52;
	private static final char SMTP_ERROR_CODE2 = 53;
	private static final int fillchar = 61;
	private static final String cvt = 
	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
	private Socket mailSocket;
	private BufferedReader recv;
	private PrintWriter send;
	private String from;
	private String to;
	private String domain;
	private Vector x_set;
	private Vector body;
	private Vector attach;
	public SmtpMail(){
	DELIMETER = "";
	SEPARATOR = "";
	mailSocket = null;
	recv = null;
	send = null;
	from = "";
	to = "";
	domain = "";
	x_set = new Vector();
	body = new Vector();
	attach = new Vector();
	//DELIMETER = getId();
	SEPARATOR = System.getProperty("file.separator");
	}
	public int open(String serverName, int port){
		try{
		mailSocket = new Socket(serverName, port);
		send = new PrintWriter(mailSocket.getOutputStream(), true);
		recv = new BufferedReader(new InputStreamReader(mailSocket.getInputStream()));
		String s1 = recv.readLine();
		char c = s1.charAt(0);
		if((c == '4') | (c == '5'))
		return 0;
		}
		catch(Exception e){
		return 0;
		}
		return 1;
		}
	public int transmit(){
		boolean flag = true;
//		发送HELO 命令
		if(domain.length() != 0){
		int i = sendString("HELO " + domain);
		if(i != 1)
		return 0;
		}
//		发送MAIL FROM 命令(发件人)
		if(from.length() != 0){
		int j = sendString("MAIL FROM:" + from);
		if(j != 1)
		return 0;
		}
//		发送RCPT TO 命令(收件人)
		if(to.length() != 0){
		int k = sendString("RCPT TO:" + to);
		if(k != 1)
		return 0;
		}
//		发送邮件正文(DATA 命令)
		if(sendString("DATA") != 1)
		return 0;
//		发送邮件头信息
		for(int l = 0; l < x_set.size(); l += 2){
		String s = (String)x_set.elementAt(l);
		send.println(s + ": " + x_set.elementAt(l + 1));
		}
//		发送时间及相关内容格式说明
		if(x_set.indexOf("Date") <0)
		send.println("Date: " + (new Date()).toString());
		return 1;
	}
	private int sendString(String s){
		String s1 = "";
		try{
		send.println(s);
		s1 = recv.readLine();
		}
		catch(Exception e){
		System.out.print(s1);
		return 0;
		}
		if(s1.length() == 0)
		return 0;
		char c = s1.charAt(0);
		return !((c == '4') | (c == '5')) ? 1 : 0;
		}
	public int close(){
		int i = 0;
		try{
		i += sendString("QUIT");
		mailSocket.close();
		}
		catch(Exception e){
		return 0;
		}
		return i == 0 ? 1 : 0;
		}

	public void setFrom(String s){
	this.from=s;
	}
	public void setTo(String s){
		this.to=s;
		
	}
	public void addHeader(String s,String a){
		this.x_set.add(s);
		this.x_set.add(a);
	}
	public void addData(String s){
		this.x_set.add("date");
		this.x_set.add(s);
	}
	public void addAttachment(String s){
		
	}




}

⌨️ 快捷键说明

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