Java has powerful network functions and is easy to develop and use. It is no wonder that microsoft strives to regain the dominant position of the programming language. According to the smtp protocol, java Socket is used to write a program for sending emails,
The implementation principle is very simple. First, establish a Socket connection with the mail server, then shake hands with the server, then send the smtp command, encapsulate the mail body, and then send it.
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 = "";
Boolean isNeedAuthLogin = false;
Vector to = new Vector ();
Public static void main (String [] args ){
SMTPSender smtp = new SMTPSender ();
Smtp. setMailServer ("mail.ehawa.com ");
Smtp. setMailFrom ("root@ehawa.com ","??? ","??? ");
Smtp. addMailTo ("root@ehawa.com ");
If (smtp. send ("hello", "this is a test! ")){
System. out. println ("email sent successfully! ");
} Else System. out. println ("email sending failed! ");
}
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 ()));
// The email server is connected successfully.
ReadResponse ("220 ");
// HELO host
SendRequest ("HELO" + smtpServer + LINEFEED );
ReadResponse ("250 ");
If (isNeedAuthLogin ){
// AUTH LOGIN
SendRequest ("auth login" + LINEFEED );
ReadResponse ("334 ");
// USERNAME:
SendRequest (new String (Base64.encodeString (user) + LINEFEED );
ReadResponse ("334 ");
// PASSWORD:
SendRequest (new String (Base64.encodeString (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 ");
// Email content
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 ");
// Exit QUIT
SendRequest ("QUIT" + LINEFEED );
ReadResponse ("221 ");
Try {
InData. close ();
InData = null;
} Catch (Exception ex ){}
Try {
OutData. close ();
OutData = null;
} Catch (Exception ex ){}
Try {
Socket. close ();
Socket = null;
} Catch (Exception ex ){}
} Catch (Exception e ){
Return false;
// E. printStackTrace ();
}
Return true;
}
Private void readResponse (String cmd) throws Exception {
String tmp = inData. readLine ();
If (tmp. startsWith (cmd); // System. out. println ("[S:]" + tmp );
Else throw new Exception ("######### failed to send the email! ######### "+ Tmp );
While (tmp. startsWith (cmd + "-") tmp = inData. readLine ();
}
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 ){}
}
}