Import Java. util. arraylist; import Java. util. date; import Java. util. list; import Java. util. properties; import javax. mail. authenticator; import javax. mail. message; import javax. mail. passwordauthentication; import javax. mail. session; import javax. mail. transport; import javax. mail. internet. internetaddress; import javax. mail. internet. mimemessage; public class Sendmail {public static string smtp_host = "smtp.126.com"; public static string smtp_port = "25"; public static string smtp_protocol = "SMTP "; public static string smtp_auth = "true"; public static string auth_user = "t *** gon@126.com"; public static string auth_password = "asdfsdf "; public static string from_address = "***** @ 126.com";/***** send email, the destination address is ** @ Param to * destination email address * @ Param title * email title * @ Param content * Email content * @ return: whether the email is successfully sent */public static boolean send (string, string title, string content) {Boolean issuccess = true; If (to = NULL | Title = NULL | content = NULL) return false; properties property = new properties (); // set some basic properties property. put ("mail. SMTP. host ", smtp_host); property. put ("mail. SMTP. port ", smtp_port); property. put ("mail. SMTP. protocol ", smtp_protocol); property. put ("mail. SMTP. auth ", smtp_auth); myauthenticator myauth = new myauthenticator (auth_user, auth_password); // obtain the mail sending session mailsession = session. getdefaultinstance (property, myauth); // generates the sent message = new mimemessage (mailsession); try {// form the sent mail address internetaddress fromaddress = new internetaddress (from_address ); message. setfrom (fromaddress); internetaddress toaddress = new internetaddress (to); // added two overload functions: Message. addrecipient (message. recipienttype. to, toaddress); // set the message. setsubject (title); // sets the message topic message. settext (content); // Save the message. savechanges ();} catch (exception e) {issuccess = false; system. out. println (E. getmessage ();} // send mailtry {transport. send (message, message. getrecipients (message. recipienttype. to);} catch (exception e) {issuccess = false; system. out. println (E. getmessage ();} return issuccess;}/*** send email, the destination address is a set of email addresses ** @ Param tolist * @ Param title * email title * @ Param content * Email content * @ return Boolean returns whether the email is successful */Public static Boolean send (list <string> tolist, string title, string content) {Boolean issuccess = true; If (tolist = NULL | Title = NULL | content = NULL | tolist. size () = 0) return false; properties property = new properties (); // you can specify basic properties. put ("mail. SMTP. host ", smtp_host); property. put ("mail. SMTP. port ", smtp_port); property. put ("mail. SMTP. protocol ", smtp_protocol); property. put ("mail. SMTP. auth ", smtp_auth); myauthenticator myauth = new myauthenticator (auth_user, auth_password); // obtain the mail sending session mailsession = session. getdefaultinstance (property, myauth); // generates the sent message = new mimemessage (mailsession); try {// form the sent mail address internetaddress fromaddress = new internetaddress (from_address ); message. setfrom (fromaddress); For (string to: tolist) {internetaddress toaddress = new internetaddress (to); // added two overload functions: Message. addrecipient (message. recipienttype. to, toaddress);} // sets the message. setsubject (title); // sets the message topic message. settext (content); // Save the message. savechanges ();} catch (exception e) {issuccess = false; system. out. println (E. getmessage ();} // send mailtry {transport. send (message, message. getrecipients (message. recipienttype. to);} catch (exception e) {issuccess = false; system. out. println (E. getmessage ();} return issuccess;}/*** @ Param ARGs */public static void main (string [] ARGs) {// todostring to = "xuzhanyi@yahoo.com.cn "; string title = "title"; string content = "content"; List <string> tolist = new arraylist <string> (); tolist. add (to); int I; for (;) {// system. out. println (new date () + ":" + Sendmail. send (tolist, title + (INT) (1000 * Math. random (), content); try {thread. sleep (10000);} catch (interruptedexception e) {e. printstacktrace () ;}}} class myauthenticator extends authenticator {private string user; private string password; Public myauthenticator (string user, string password) {This. user = user; this. password = password;} protected passwordauthentication getpasswordauthentication () {return New passwordauthentication (user, password );}}
Mail. jar is required