Send an email using javamail

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.