Package test; import Java. util. date; import Java. util. properties; import javax. mail. address; 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;/*** send a common email, accept a common email with an attachment, and receive an HTML email with an attachment, an HTML-based email is used to send an email with an image. Summary. */Public class Sendmail {// email server private string host = "smtp.163.com"; // This is your email username private string username = "****** @ 163.com "; // your email password is private string Password = "*******"; private string mail_head_name = "mail title"; private string mail_head_value = "mail title "; private string mail_to = "******* @ QQ.com"; private string mail_from = "******* @ 163.com"; private string mail_subject =" mail title "; private string mail_body = "mail File Content "; private string personalname =" javamail send email test ";/*** this code is used to send a common email */Public void send () throws exception {try {properties props = new properties (); // obtain the system environment authenticator auth = new email_autherticator (); // perform Mail Server user authentication props. put ("mail. SMTP. host ", host); props. put ("mail. SMTP. auth "," true "); Session session = session. getdefaultinstance (props, auth); // sets the session to communicate with the email server. Mimemessage message = new mimemessage (session); // message. setcontent ("foobar," application/X-foobar "); // set the Message format. setsubject (mail_subject); // sets the mail subject message. settext (mail_body); // sets the message in the mail body. setheader (mail_head_name, mail_head_value); // set the mail title message. setsentdate (new date (); // set the mail sending date Address = new internetaddress (mail_from, personalname); message. setfrom (Address); // set the email sender's address ad Dress toaddress = new internetaddress (mail_to); // sets the mail recipient's address message. addrecipient (message. recipienttype. to, toaddress); transport. send (Message); // send the email system. out. println ("Send OK! ");} Catch (exception ex) {ex. printstacktrace (); throw new exception (ex. getmessage () ;}}/*** used for server-to-user authentication */public class email_autherticator extends authenticator {public email_autherticator () {super ();} public email_autherticator (string user, string PWD) {super (); username = user; Password = PWD;} public passwordauthentication getpasswordauthentication () {return New passwordauthentication (username, password );}} public static void main (string [] ARGs) {Sendmail = new Sendmail (); try {Sendmail. send () ;}catch (exception ex ){}}}
Jar: javax. Mail. Jar
Http://download.csdn.net/detail/javaalpha/5624731