📄 sendmail.java
字号:
import java.net.Socket;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
public class SendMail
{
public static byte SMTP_PORT = 25;
//action;
static byte SMTP_HELLO = 0;
static byte SMTP_TELLNAME = 1;
static byte SMTP_ACTION = 2;
static byte SMTP_DATA = 3;
static byte SMTP_MAILTO = 4;
static byte SMTP_MAILFROM = 5;
static byte SMTP_QUIT = 6;
//response
static String SMTP_Ok_Str = "220";
static String SMTP_SERVER_CLOSE = "221";
static String SMTP_TRANSFER_OK = "354";
static String SMTP_ACT_OK = "250";
//member;
String m_sHost;
int m_nPort;
Socket m_socket;
boolean m_bConnected;
Email m_Email;
//func.
public SendMail(){
m_bConnected = false;
}
public SendMail(String sHost,int nPort){
SetServerInfo( sHost, nPort);
m_bConnected = false;
}
public void SetServerInfo(String sHost,int nPort){
m_sHost = sHost;
m_nPort = nPort;
}
public void SetEmail(Email email){
m_Email = email;
}
public boolean DisConnectToServer(){
if( m_bConnected==false )
return true;
try{
//send hello string;
StringBuffer sHello = new StringBuffer("QUIT\r\n");
if( SendMsg(sHello.toString().getBytes () )==false )
return false;
if( GetResult(SMTP_QUIT)==false )
return false;
m_socket.close ();
}catch(Exception e){
return false;
}
m_bConnected=false;
return true;
}
public boolean ConnectToServer(){
if( m_bConnected )
return true;
try{
m_socket= new Socket (m_sHost,m_nPort);
if( GetResult(SMTP_HELLO)==false )
return false;
m_bConnected = true;
//send hello string;
StringBuffer sHello = new StringBuffer("HELO ");
sHello.append (InetAddress.getLocalHost().getHostName());
sHello.append ("\r\n");
if( SendMsg(sHello.toString().getBytes () )==false ){
m_bConnected = false;
return false;
}
if( GetResult(SMTP_TELLNAME)==false ){
m_bConnected = false;
return false;
}
}catch(Exception e){
System.out.println ("connect error:"+e.toString());
return false;
}
m_bConnected =true;
return true;
}
public boolean SendMsg( byte[] sMsg){
if( sMsg.length == 0 )
return false;
try{
if( m_bConnected==false )
return false;
//send hello string;
OutputStream sOut = m_socket.getOutputStream();
sOut.write(sMsg,0,sMsg.length );
sOut.flush ();
}catch(Exception e){
return false;
}
return true;
}
boolean GetResult( int nAct ){
String sResponse;
byte[] bRead = new byte[1024];
int nRead = -1;
String sRead ;
try{
InputStream sIn = m_socket.getInputStream();
nRead = sIn.read(bRead);
if( nRead <= 0 )
return false;
sRead = new String(bRead);
}catch(Exception e){
return false;
}
switch(nAct){
case 0: //hello
if( sRead.indexOf (SMTP_Ok_Str)== 0 )
return true;
break;
case 3: //data
if( sRead.indexOf (SMTP_TRANSFER_OK)== 0 )
return true;
break;
case 6: //quit
if( sRead.indexOf (SMTP_SERVER_CLOSE)== 0 )
return true;
break;
default :
if( sRead.indexOf (SMTP_ACT_OK)== 0 )
return true;
break;
}
return false;
}
//send mail;
public boolean SendEMail(){
if(m_Email==null)
return false;
if( m_bConnected==false ){
if(ConnectToServer()==false)
return false;
}
if( SendEMailHeader()==false )
return false;
if( SendEMailBody()==false )
return false;
DisConnectToServer();
return true;
}
boolean SendEMailHeader(){
StringBuffer sReturn=new StringBuffer();
if( m_Email.GetMailHeader().GetToNum() == 0 )
return false;
//send from;
sReturn.append("MAIL From: <");
sReturn.append(m_Email.GetMailHeader().GetFrom ());
sReturn.append(">\r\n");
if( SendMsg(sReturn.toString ().getBytes () )==false )
return false;
if( GetResult(SMTP_MAILFROM)==false )
return false;
sReturn = new StringBuffer();
//send to;
for( int i = 0; i <m_Email.GetMailHeader().GetToNum() ;i++ )
{
sReturn.append("RCPT TO:<");
sReturn.append(m_Email.GetMailHeader().GetToAdd(i));
sReturn.append(">\r\n");
if( SendMsg(sReturn.toString ().getBytes () )==false )
return false;
if( GetResult(SMTP_ACTION)==false )
return false;
sReturn = new StringBuffer();
}
//send Cc;
for( int i = 0; i <m_Email.GetMailHeader().GetCcNum() ;i++ )
{
sReturn.append("RCPT TO:<");
sReturn.append(m_Email.GetMailHeader().GetCcAdd(i));
sReturn.append(">\r\n");
if( SendMsg(sReturn.toString ().getBytes () )==false )
return false;
if( GetResult(SMTP_ACTION)==false )
return false;
sReturn = new StringBuffer();
}
//send Bcc;
for( int i = 0; i <m_Email.GetMailHeader().GetBCcNum() ;i++ )
{
sReturn.append("RCPT TO:<");
sReturn.append(m_Email.GetMailHeader().GetBCcAdd(i));
sReturn.append(">\r\n");
if( SendMsg(sReturn.toString ().getBytes () )==false )
return false;
if( GetResult(SMTP_ACTION)==false )
return false;
sReturn = new StringBuffer();
}
//send data tag;
sReturn.append("DATA\r\n");
if( SendMsg(sReturn.toString ().getBytes () )==false )
return false;
if( GetResult(SMTP_DATA)==false )
return false;
sReturn = new StringBuffer();
return true;
}
boolean SendEMailBody(){
if( SendMsg( m_Email.CreateEMailHeadInfoForSend().getBytes() )==false )
return false;
StringBuffer sReturn=new StringBuffer();
sReturn.append ("This is a multi-part message in MIME format.\r\n\r\n");
if( SendMsg(sReturn.toString ().getBytes () )==false )
return false;
sReturn = new StringBuffer();
int nItem = m_Email.GetItemNum();
MailItem tItem;
for( int i=0;i<nItem;i++ ){
sReturn.append ("--");
sReturn.append (m_Email.GetBoundary());
sReturn.append ("\r\n");
if( SendMsg(sReturn.toString ().getBytes () )==false )
return false;
sReturn = new StringBuffer();
//send item head;
tItem = m_Email.GetItemAt (i);
if( SendMsg( tItem.CreateHeaderForSend().getBytes() )==false )
return false;
//send item body;
byte[] tCode = tItem.CreateBodyCodeForSend();
if( tCode!=null && tCode.length !=0 )
if( SendMsg( tCode )==false )
return false;
//end item;
sReturn.append ("\r\n");
if( SendMsg(sReturn.toString ().getBytes () )==false )
return false;
sReturn = new StringBuffer();
}
sReturn.append ("--");
sReturn.append (m_Email.GetBoundary());
sReturn.append ("--");
sReturn.append ("\r\n");
if( SendMsg(sReturn.toString ().getBytes () )==false )
return false;
sReturn = new StringBuffer();
sReturn.append ("\r\n.\r\n");
if( SendMsg(sReturn.toString ().getBytes () )==false )
return false;
if( GetResult(SMTP_ACTION)==false )
return false;
return true;
}
//
public boolean SetEmailInfo(String sTo,String sFrom,String Sub,
String sCc,String sBCc){
if( m_Email==null )
m_Email =new Email();
if(sTo==null )
return false;
if( sFrom==null )
sFrom = "";
if( Sub==null )
Sub = "";
if( sCc==null )
sCc = "";
if( sBCc==null )
sBCc = "";
m_Email.SetInfo( sTo, sFrom, Sub,
sCc, sBCc);
return m_Email.GetMailHeader().CheckData();
}
public boolean addPlainBody( String sName,
byte encode,String sIn ){
if( m_Email==null )
return false;
m_Email.addPlainBody( sName,
encode, sIn );
return true;
}
public boolean addAttach( String sName,
byte encode,String sFile ){
if( m_Email==null )
return false;
m_Email.addAttach( sName,
encode, sFile );
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -