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

📄 smtpclient.java

📁 自己做的一个发送邮件程序
💻 JAVA
字号:
import java.net.*; 
import java.io.*; 
/** 
* <p>Title: 使用SMTP发送邮件</p> 
* <p>Description: 通过使用Socket方式,根据SMTP协议发送邮件</p> 
* <p>Filename: SMTPClient.java</p> 
* @version 1.0 
*/ 
public class SMTPClient {
	
	public String mailServer;//本机IP
	public String recipient;//邮件目的地
	
	public static void main(String argv[])throws Exception{
	  System.out.println("SMTP Client Program");
	  SMTPClient smtp = new SMTPClient();
	  smtp.SMTPSend("219.242.120.21","ypf512@sohu.com");
	}
	
	public void SMTPSend(String mailServer, String recipient){
	try{
		Socket socket;
		BufferedReader in; //输入缓冲器
		DataOutputStream out; //输出缓冲器
        //有Socket时打开端口25
		socket=new Socket(mailServer,25);
		System.out.println(socket.getRemoteSocketAddress().toString()+" connected");
        //缓存输入
		in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
		//缓存输出
		out=new DataOutputStream(socket.getOutputStream());
		
		if(Read(in)!=220){
			QuitOnError(socket,in,out);
			return;
		}
        //发出“HELO”命令,表示向服务器发出问候 
		Send(out,"HELO");
		if(Read(in)!=250){
			QuitOnError(socket,in,out);
			return;
		}
        //使用“MAIL FROM”命令告诉服务器发邮件的发送地址
		Send(out,"MAIL FROM:<ypf412@163.com>");
		if(Read(in)!=250){
			QuitOnError(socket,in,out);
			return;
		}
        //使用“RCPT TO”命令告诉服务器解释邮件的目的地址
		Send(out,"RCPT TO:"+recipient);
		if(Read(in)!=250){
			QuitOnError(socket,in,out);
			return;
		}
        //发送一个“DATA”表示下面将是邮件的主体
		Send(out,"DATA");
		if(Read(in)!=354){
			QuitOnError(socket,in,out);
			return;
		}
        //使用“From”标注邮件的来源 
		Send(out,"From: ypf412@163.com");
        //使用“To”标注邮件的目的地
		Send(out,"To: "+recipient);
        //使用Subject命令标注邮件主题 
		Send(out,"Subject: Hello");
		Send(out," ");
        //邮件的主体部分 
		Send(out,"This is a SMTP test program!");
		//结束标志
		Send(out,".");
		if(Read(in)!=250){
			QuitOnError(socket,in,out);
			return;
		}
        //发送“QUIT”端口邮件的通讯 
		Send(out,"QUIT");
		Read(in);
		//关闭输入,输出以及Socket流
		in.close();
		out.close();
		socket.close();
	  }
	  catch (Exception e){ 
	      e.printStackTrace(); 
	  } 
	}
	//读取信息
	public static int Read(BufferedReader in)throws Exception{
	    try{
		     String s=in.readLine();
		     System.out.println("Message received: "+s);
		     s=s.substring(0,3);
		     return Integer.parseInt(s);
		  }
		  catch (Exception e){ 
		     e.printStackTrace(); 
		     return 0;
		  } 
	}
	//发送信息
	public static void Send(DataOutputStream out,String s)throws Exception{
		try{
		     System.out.println("Message send:	  "+s);
		     out.writeBytes(s+"\r\n");
		     out.flush(); 
		  }
		  catch (Exception e){ 
		      e.printStackTrace(); 
		  } 
	}
	//出错情况
	public static void QuitOnError(Socket socket,BufferedReader in,DataOutputStream out)throws Exception{
		try{
		    System.out.println("Error occurred");
		    Send(out,"QUIT");
		    socket.close();
		    in.close();
		    out.close();
		  }
		  catch (Exception e){ 
		      e.printStackTrace(); 
		  } 
	}
	
}


⌨️ 快捷键说明

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