English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Use Java applications to send E-JavaMail is very simple, but first you should install the JavaMail API and Java Activation Framework (JAF) on your machine.
You can download the latest version of JavaMailOn the right side of the webpage, there is a Downloads link, click it to download.
You can download the latest version of JAF (version 1.1.1).
You can also use the download links provided on this site:
Download and unzip these files. In the newly created top-level directory, you will find some jar files for these applications. You need to add mail.jar and activation.jar Add the file to your CLASSPATH.
If you use a third-party email server like QQ's SMTP server, you can check the complete user authentication example at the bottom of the article.
Below is a simple E-mail instance. Assume that your local host is already connected to the network.
// Filename SendEmail.java import java.util.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; public class SendEmail { public static void main(String [] args) { // Recipient's email address String to = "[email protected]"; // Sender's email address String from = "[email protected]"; // Specify the sending email host as localhost String host = "localhost"; // Get system properties Properties properties = System.getProperties(); // Set the email server properties.setProperty("mail.smtp.host", host); // Get the default session object Session session = Session.getDefaultInstance(properties); try{ // Create a default MimeMessage object MimeMessage message = new MimeMessage(session); // Set From: Header Field message.setFrom(new InternetAddress(from)); // Set To: Header Field message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set Subject: Header Field message.setSubject("This is the Subject Line!"); // Set Message Body message.setText("This is actual message"); // Send Message Transport.send(message); System.out.println("Sent message successfully...."); }catch (MessagingException mex) { mex.printStackTrace(); } } }
Compile and run this program to send a simple E-mail:
$ java SendEmail Sent message successfully....
If you want to send an e-If you want to send an email to multiple recipients, use the following method to specify multiple recipient IDs:
void addRecipients(Message.RecipientType type, Address[] addresses) throws MessagingException
The following is a description of the parameters:
type:To be set to TO, CC, or BCC, where CC stands for Carbon Copy and BCC stands for Blind Carbon Copy. For example:Message.RecipientType.TO
addresses: This is an array of email IDs. When specifying an email ID, you will need to use the InternetAddress() method.
Below is an example of sending an HTML E-mail instance. Assume that your local host is already connected to the network.
Similar to the previous example, except that we use the setContent() method to set the content by the second parameter as "text/html",to set the content to specify the HTML content to be sent.
// Filename SendHTMLEmail.java import java.util.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; public class SendHTMLEmail { public static void main(String [] args) { // Recipient's email address String to = "[email protected]"; // Sender's email address String from = "[email protected]"; // Specify the sending email host as localhost String host = "localhost"; // Get system properties Properties properties = System.getProperties(); // Set the email server properties.setProperty("mail.smtp.host", host); // Get the default Session object. Session session = Session.getDefaultInstance(properties); try{ // Create a default MimeMessage object. MimeMessage message = new MimeMessage(session); // Set From: Header Field message.setFrom(new InternetAddress(from)); // Set To: Header Field message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set Subject: header field message.setSubject("This is the Subject Line!"); // Send HTML message, can insert html tags message.setContent("<h1>This is actual message</h1"> "text/html"); // Send Message Transport.send(message); System.out.println("Sent message successfully...."); }catch (MessagingException mex) { mex.printStackTrace(); } } }
Compile and run this program to send HTML e-mail:
$ java SendHTMLEmail Sent message successfully....
Below is an example of sending an E-mail with an attachment-mail instance. Assume that your local host is already connected to the network.
// Filename SendFileEmail.java import java.util.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; public class SendFileEmail { public static void main(String [] args) { // Recipient's email address String to = "[email protected]"; // Sender's email address String from = "[email protected]"; // Specify the sending email host as localhost String host = "localhost"; // Get system properties Properties properties = System.getProperties(); // Set the email server properties.setProperty("mail.smtp.host", host); // Get the default Session object. Session session = Session.getDefaultInstance(properties); try{ // Create a default MimeMessage object. MimeMessage message = new MimeMessage(session); // Set From: Header Field message.setFrom(new InternetAddress(from)); // Set To: Header Field message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set Subject: header field message.setSubject("This is the Subject Line!"); // Create message part BodyPart messageBodyPart = new MimeBodyPart(); // Message messageBodyPart.setText("This is message body"); // Create a multipart message Multipart multipart = new MimeMultipart(); // Set text message part multipart.addBodyPart(messageBodyPart); // Attachment part messageBodyPart = new MimeBodyPart(); String filename = "file.txt"; DataSource source = new FileDataSource(filename); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(filename); multipart.addBodyPart(messageBodyPart); // Send the complete message message.setContent(multipart); // Send Message Transport.send(message); System.out.println("Sent message successfully...."); }catch (MessagingException mex) { mex.printStackTrace(); } } }
Compile and run your program to send an email with an attachment.
$ java SendFileEmail Sent message successfully....
If you need to provide username and password to e-To achieve user authentication purposes through mail servers, you can complete the following settings:
props.put("mail.smtp.auth", "true"); props.setProperty("mail.user", "myuser"); props.setProperty("mail.password", "mypwd");
e-mail's other sending mechanisms are consistent with the above.
This example takes the QQ mail server as an example, you need to enable POP in the account settings of the QQ mail background after logging in3/SMTP service, as shown in the figure below:
QQ mail sets the password through generating an authorization code:
The Java code is as follows:
// Example of sending email with username and password //File name SendEmail2.java //This example takes QQ mail as an example, you need to set it in the QQ background import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class SendEmail2 { public static void main(String [] args) { // Recipient's email address String to = "[email protected]"; // Sender's email address String from = "[email protected]"; // Specify the sending email host as smtp.qq.com String host = "smtp.qq.com"; //QQ email server // Get system properties Properties properties = System.getProperties(); // Set the email server properties.setProperty("mail.smtp.host", host); properties.put("mail.smtp.auth", "true"); // Get the default session object Session session = Session.getDefaultInstance(properties, new Authenticator(){ public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("[email protected]", "QQ email authorization code"); //Sender's email username and authorization code } }); try{ // Create a default MimeMessage object MimeMessage message = new MimeMessage(session); // Set From: Header Field message.setFrom(new InternetAddress(from)); // Set To: Header Field message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set Subject: Header Field message.setSubject("This is the Subject Line!"); // Set Message Body message.setText("This is actual message"); // Send Message Transport.send(message); System.out.println("Sent message successfully....from oldtoolbag.com)); }catch (MessagingException mex) { mex.printStackTrace(); } } }