In some cases, we only need to verify that the account and password of the mailbox are correct. Instead of sending emails. Use the followingCodeRight.
The jar package to be used is jmail. jar.
Method 1
Package com.cn. mail. test; import Java. util. properties; import javax. mail. messagingexception; import javax. mail. session; import javax. mail. urlname; import COM. sun. mail. SMTP. smtptransport; public class emaillogin extends smtptransport {public emaillogin (session, urlname) {super (Session, urlname );} /*** @ Param ARGs */public static void main (string [] ARGs) {urlname = new urlname ("SMTP", "smtp.163.com", 25 ,"", "xxx@163.com", "password"); Session session = session. getinstance (new properties (); emaillogin login = new emaillogin (Session, urlname); system. out. println ("login:" + login. login ();} public Boolean login () {Boolean login = false; try {login = This. protocolconnect (URL. gethost (), URL. getport (), URL. getUserName (), URL. getPassword ();} catch (messagingexception e) {login = false;} return login ;}}
Method 2
You can also use the socket Method
// Test the Sina email address. coremail is not a good test for the server mailbox (163), because the error message is too slow. not easy to use, others are okay. too many passwords are required. The server will consider the password as an attack.
// Block! Import java.net. *; import Java. io. *; public class mypass {public static void main (string ARGs []) {try {string pass = ""; Boolean EOF = false; bufferedreader fp = new bufferedreader (New filereader (new file ("pass.txt"); // while (! EOF) {pass = FP. readline (); If (pass = NULL) EOF = true; else system. out. println (LOGIN ("abc@sina.com", pass. trim () + "Pass:" + pass); // test user name} catch (exception e) {e. printstacktrace () ;}} public static Boolean login (string user, string pass) {try {socket conn = new socket (inetaddress. getbyname ("202.108.3.230"), 110); // ip address of the POP3 server used for testing. Bufferedreader input = new bufferedreader (New inputstreamreader (Conn. getinputstream (); printwriter out = new printwriter (Conn. getoutputstream (), true); string line = input. readline (); out. print ("user" + User + "\ r \ n"); out. flush (); line = input. readline (); out. print ("pass" + pass + "\ r \ n"); out. flush (); line = input. readline (); // system. out. println (line); If (line. substring (0, 3 ). equals ("+ OK") {return true;} return false;} catch (exception e) {e. printstacktrace ();} return false;} public static void quit (printwriter out) {out. print ("Quit \ r \ n ");}}
Comparison of the two methods:
The socket uses the TCP/IP protocol, while the browser adopts the HTTP protocol.ProgramWhile the channel in the browser mode is OK by default, the channel in the TCP mode needs to be established through socket, and the transmitted data is in the packet format, you can understand the message as a string of Dongdong, which can be binary, decimal, or string for user name and password authentication, as a string, you can parse the packets according to the format specified by the client and the server. The verification work is the same as that of the HTTP protocol. In simple words, it is placed on the servlet. The difference is that the socket needs to establish a connection and customize the Message format by itself, and the browser only needs to transmit the message in the request mode.