The following is referenced from http://wiki.jikexueyuan.com/project/java/sending-email.html:
It is simple enough to send an email with a Java application, but the JavaMail API and the Java Activation Framework (JAF) should be installed on the machine at the beginning.
- The latest version of JavaMail (version 1.2) can be downloaded from the Java Standard Enterprise website.
- The latest version of JAF (1.1.1 version) can be downloaded from the Java Standard Enterprise Web site.
Download and unzip these files and find the jar files for many applications in the newly created top-level directory. You need to add Mail.jar and Activation.jar files in Classpath. (This step is omitted from the POM project and the Eclipse Project)
One, send a simple e-mail
This is an example of sending a simple e-mail message from the machine. This assumes that the local host is connected to the Internet and is able to send an e-mail message.
ImportJava.util.*;ImportJavax.mail.*;Importjavax.mail.internet.*;Importjavax.activation.*; Public classSendEmail { Public Static voidMain (string[] args) {//Recipient ' s email ID needs to be mentioned.String to = "[Email protected]"; //Sender ' s email ID needs to be mentionedString from = "[Email protected]"; //assuming you is sending email from localhostString host = "localhost";//This is SMTP server,ex:smtp.163.com//Set Email usernameString user = "User"; //Set Email passwordString password = "Password"; //Get System PropertiesProperties Properties =system.getproperties (); //Setup Mail ServerProperties.setproperty ("Mail.smtp.host", host); Properties.setproperty ("Mail.user", user); Properties.setproperty ("Mail.password", password); //Get The default Session object.Session session =Session.getdefaultinstance (properties); Try { //Create a default MimeMessage object.MimeMessage message =NewMimeMessage (session); //Set from:header field of the header.Message.setfrom (Newinternetaddress (from)); //Set to:header field of the header.Message.addrecipient (Message.RecipientType.TO,Newinternetaddress (to)); //Set subject:header FieldMessage.setsubject ("This is the Subject line!"); //Now set the actual messageMessage.settext ("This is actual message"); //Send Messagetransport.send (message); System.out.println ("Sent message successfully ..."); } Catch(Messagingexception Mex) {mex.printstacktrace (); } }}//compile and run the program to send a simple e-mail message:$ java sendemailsent message successfully ....
If you want to send an e-mail message to multiple recipients, the following methods will be sent to the specified number of email IDs:
void addrecipients (message.recipienttype type, throws messagingexception
Here is the description of the parameter:
- Type: This will be set to TO,CC or Bcc. Here cc represents a copy, and Bcc represents black Carbon copy. such as Message.RecipientType.TO.
- Addresses: This is an array of e-mail IDs. When specifying an e-mail id, you need to use InternetAddress ().
Second, send an HTML e-mail
This is an example of sending an HTML email from the machine. This assumes that the local host is connected to the Internet and is able to send an e-mail message.
This example is very similar to the previous one except that in this case, the second argument is "text/html" to specify the content that the HTML content is included in the message, in addition to the SetContent () method.
Using this example, you can send any HTML content.
//File Name Sendhtmlemail.javaImportJava.util.*;ImportJavax.mail.*;Importjavax.mail.internet.*;Importjavax.activation.*; Public classSendhtmlemail { Public Static voidMain (string[] args) {//Recipient ' s email ID needs to be mentioned.String to = "[Email protected]"; //Sender ' s email ID needs to be mentionedString from = "[Email protected]"; //assuming you is sending email from localhostString host = "localhost";//This is SMTP server,ex:smtp.163.com//Set Email usernameString user = "User"; //Set Email passwordString password = "Password"; //Get System PropertiesProperties Properties =system.getproperties (); //Setup Mail ServerProperties.setproperty ("Mail.smtp.host", host); Properties.setproperty ("Mail.user", user); Properties.setproperty ("Mail.password", password); //Get The default Session object.Session session =Session.getdefaultinstance (properties); Try { //Create a default MimeMessage object.MimeMessage message =NewMimeMessage (session); //Set from:header field of the header.Message.setfrom (Newinternetaddress (from)); //Set to:header field of the header.Message.addrecipient (Message.RecipientType.TO,Newinternetaddress (to)); //Set subject:header FieldMessage.setsubject ("This is the Subject line!"); //Send The actual HTML message, as big as you likeMessage.setcontent ("); //Send Messagetransport.send (message); System.out.println ("Sent message successfully ..."); } Catch(Messagingexception Mex) {mex.printstacktrace (); } }}//compile and run the program to send an HTML e-mail message:$ java sendhtmlemailsent message successfully ....
Third, send the attachment in the e-mail
This is an example of sending an email message with an attachment from the machine. This assumes that the local host is connected to the Internet and is able to send an e-mail message.
//File Name Sendfileemail.javaImportJava.util.*;ImportJavax.mail.*;Importjavax.mail.internet.*;Importjavax.activation.*; Public classSendfileemail { Public Static voidMain (string[] args) {//Recipient ' s email ID needs to be mentioned.String to = "[Email protected]"; //Sender ' s email ID needs to be mentionedString from = "[Email protected]"; //assuming you is sending email from localhostString host = "localhost";//This is SMTP server,ex:smtp.163.com//Set Email usernameString user = "User"; //Set Email passwordString password = "Password"; //Get System PropertiesProperties Properties =system.getproperties (); //Setup Mail ServerProperties.setproperty ("Mail.smtp.host", host); Properties.setproperty ("Mail.user", user); Properties.setproperty ("Mail.password", password); //Get The default Session object.Session session =Session.getdefaultinstance (properties); Try { //Create a default MimeMessage object.MimeMessage message =NewMimeMessage (session); //Set from:header field of the header.Message.setfrom (Newinternetaddress (from)); //Set to:header field of the header.Message.addrecipient (Message.RecipientType.TO,Newinternetaddress (to)); //Set subject:header FieldMessage.setsubject ("This is the Subject line!"); //Create the message partBodyPart Messagebodypart =NewMimeBodyPart (); //Fill the messageMessagebodypart.settext ("This is message body"); //Create a MULTIPAR messageMultipart Multipart =NewMimemultipart (); //Set text Message partMultipart.addbodypart (Messagebodypart); //Part AttachmentMessagebodypart =NewMimeBodyPart (); String filename= "File.txt"; DataSource Source=Newfiledatasource (filename); Messagebodypart.setdatahandler (NewDataHandler (source)); Messagebodypart.setfilename (filename); Multipart.addbodypart (Messagebodypart); //Send The complete message partsmessage.setcontent (multipart); //Send Messagetransport.send (message); System.out.println ("Sent message successfully ..."); } Catch(Messagingexception Mex) {mex.printstacktrace (); } }}//compile and run the program to send an HTML e-mail message:$ java sendfileemailsent message successfully ....
Iv. User identity Authentication section
If you need to provide your e-mail server with a user ID and password for authentication purposes, you can set these properties like this:
Props.setproperty ("Mail.user", "MyUser"), Props.setproperty ("Mail.password", "MyPwd");
The remainder of the e-mail delivery mechanism is the same as explained above.
Test Project: Https://github.com/easonjim/5_java_example/tree/master/javabasicstest/test27
Send mail for Java