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

📄 smtpsender.java

📁 SOCKET MAIL
💻 JAVA
字号:
import java.net.*;
import java.io.*;
import java.util.*;

public class SMTPSender{

Socket socket=null;
PrintWriter outData=null;
BufferedReader inData=null;

String smtpServer="";

String user="";
String pass="";
String from="";

String LINEFEED="\r\n";
boolean isNeedAuthLogin=false;
Vector to=new Vector();

public static void main(String[] args){
	SMTPSender smtp=new SMTPSender();
	smtp.setMailServer("SZSHPM");
	smtp.setMailFrom("smisdata@coscochs.com.hk","coscoszdm/smisuser","coscochs");
	smtp.addMailTo("test@cosbulk.com");
	if(smtp.send("test","succeed!")){
		System.out.println("succeed!"); 
	}else {
		System.out.println("fail!"); 
	}
}

public void setMailServer(String s){
	smtpServer=s;
}

public void setMailFrom(String s,String uid,String pwd){
	this.from=s;
	this.user=uid;
	this.pass=pwd;
	this.isNeedAuthLogin=(this.user!=null&&this.pass!=null&&!this.user.equals("")&&!this.pass.equals(""));
}
public boolean addMailTo(String mailAddr){
	to.addElement(mailAddr);
	return true;
}
public boolean send(String subject,String content){
	try{
		if(smtpServer==null||smtpServer.equals(""))
			return false;
		if(from==null||from.equals(""))
			return false;
		if(to.size()<1)
			return false;
			
		socket=new Socket(smtpServer,25);
		outData=new PrintWriter(socket.getOutputStream());
		inData=new BufferedReader(new InputStreamReader(socket.getInputStream()));
		
		readResponse("220");
		//HELO host
		sendRequest("HELO "+smtpServer+LINEFEED);
		readResponse("250");
		if(isNeedAuthLogin){
			//AUTH LOGIN
			sendRequest("AUTH LOGIN"+LINEFEED);
			readResponse("334");
			//USERNAME:
			sendRequest(new String(user)+LINEFEED);
			readResponse("334");
			//PASSWORD:
			sendRequest(new String(pass)+LINEFEED);
			readResponse("235");
		}
		//MAIL FROM:<..>
		sendRequest("MAIL FROM:<"+from+">"+LINEFEED);
		readResponse("250");
		//RCPT TO:<..>
		for(Enumeration enu=to.elements();enu.hasMoreElements();){
			String to1=(String)enu.nextElement();
			sendRequest("RCPT To:<"+to1+">"+LINEFEED);
			readResponse("250");
		}
		//DATA
		sendRequest("DATA"+LINEFEED);
		readResponse("354");
		
		StringBuffer s1=new StringBuffer("From: <"+from+">"+LINEFEED);
		s1.append("To: <"+to+">"+LINEFEED);
		s1.append("Subject: "+subject+LINEFEED);
		s1.append("Date: "+new java.util.Date().toLocaleString()+LINEFEED);
		s1.append("Content-Type: text/plain;charset=\"GB2312\""+LINEFEED);
		s1.append(LINEFEED);
		s1.append(content);
		s1.append(LINEFEED+"."+LINEFEED);//Send
		sendRequest(s1.toString());
		readResponse("250");
		
		sendRequest("QUIT"+LINEFEED);
		readResponse("221");
		
		try{
			inData.close();
			inData=null;
		}catch(Exception ex){
			System.out.println(ex.getMessage());
		}
		
		try{
			outData.close();
			outData=null;
		}catch(Exception ex){
			System.out.println(ex.getMessage());
		}
		
		try{
			socket.close();
			socket=null;
		}catch(Exception ex){
			System.out.println(ex.getMessage());
		}
	}catch(Exception e){
			System.out.println(e.getMessage());
		return false;
		//e.printStackTrace();
	}
	return true;
}

private void readResponse(String cmd){
	try{
		String tmp=inData.readLine();
		if(tmp.startsWith(cmd))
			System.out.println(" [S:]"+tmp);
		else 
			System.out.println("##########fail!##########"+tmp);
			
		while(tmp.startsWith(cmd+"-")){
			tmp=inData.readLine();
		}
	}catch(Exception ex){}
}
private void sendRequest(String msg){
	//System.out.print("***[C:]"+msg);
	outData.write(msg);
	outData.flush();
}

	public void close(){
		try{
			inData.close();
			inData=null;
		}catch(Exception ex){}
		
		try{
			outData.close();
			outData=null;
		}catch(Exception ex){}
		
		try{
			socket.close();
			socket=null;
		}catch(Exception ex){}
	}
}

⌨️ 快捷键说明

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