Java Sending Email
π
2026-06-18 | π Java
Sending E-mail using a Java application 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 (http://www.oracle.com/technetwork/java/javamail/index.html) from the Java website. On 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)](http://www.oracle.com/technetwork/articles/java/index-135046.html) from the Java website.
You can also use the download links provided on this site:
* [JavaMail mail.jar 1.4.5]
* [JAF (version 1.1.1) activation.jar]
Download and unzip these files. In the newly created top-level directory, you will find some jar files for these two applications. You need to add the **mail.jar** and **activation.jar** files to your CLASSPATH.
If you are using a third-party mail server like QQ's SMTP server, you can refer to the complete user authentication example at the bottom of the article.
* * *
## Sending a Simple E-mail
Below is an example of sending a simple E-mail. Assume your local host is connected to the network.
## SendEmail.java File Code:
import java.util.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; public class SendEmail{public static void main(String[]args){String to = "abcd@gmail.com"; String from = "web@gmail.com"; String host = "localhost"; Properties properties = System.getProperties(); properties.setProperty("mail.smtp.host", host); Session session = Session.getDefaultInstance(properties); try{MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject("This is the Subject Line!"); message.setText("This is actual 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 SendEmailSent message successfully....
If you want to send an e-mail to multiple recipients, use the following method to specify multiple recipient IDs:
void addRecipients(Message.RecipientType type, Address[]addresses)throws MessagingException
Here is a description of the parameters:
* **type:** This should be set to TO, CC, or BCC. Here, CC stands for Carbon Copy, and BCC stands for Blind Carbon Copy. Example: **Message.RecipientType.TO**
* **addresses:** This is an array of email IDs. When specifying email IDs, you will need to use the InternetAddress() method.
* * *
## Sending an HTML E-mail
Below is an example of sending an HTML E-mail. Assume your local host is connected to the network.
This is very similar to the previous example, except that we use the setContent() method to set the content with the second parameter as "text/html" to specify that we are sending HTML content.
## SendHTMLEmail.java File Code:
import java.util.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; public class SendHTMLEmail{public static void main(String[]args){String to = "abcd@gmail.com"; String from = "web@gmail.com"; String host = "localhost"; Properties properties = System.getProperties(); properties.setProperty("mail.smtp.host", host); Session session = Session.getDefaultInstance(properties); try{MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject("This is the Subject Line!"); message.setContent("
This is actual message
", "text/html"); Transport.send(message); System.out.println("Sent message successfully...."); }catch(MessagingException mex){mex.printStackTrace(); }}}
Compile and run this program to send an HTML e-mail:
$ java SendHTMLEmailSent message successfully....
* * *
## Sending an E-mail with Attachment
Below is an example of sending an E-mail with an attachment. Assume your local host is connected to the network.
## SendFileEmail.java File Code:
import java.util.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; public class SendFileEmail{public static void main(String[]args){String to = "abcd@gmail.com"; String from = "web@gmail.com"; String host = "localhost"; Properties properties = System.getProperties(); properties.setProperty("mail.smtp.host", host); Session session = Session.getDefaultInstance(properties); try{MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject("This is the Subject Line!"); BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setText("This is message body"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); messageBodyPart = new MimeBodyPart(); String filename = "file.txt"; DataSource source = new FileDataSource(filename); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(filename); multipart.addBodyPart(messageBodyPart); message.setContent(multipart); 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 SendFileEmailSent message successfully....
* * *
## User Authentication Section
If you need to provide a username and password to the e-mail server for user authentication purposes, you can complete it with the following settings:
props.put("mail.smtp.auth", "true"); props.setProperty("mail.user", "myuser"); props.setProperty("mail.password", "mypwd");
The other sending mechanisms for e-mail remain the same as above.
### Email Sending Example Requiring Username and Password Verification:
This example uses the QQ mail server. You need to log in to the QQ Mail backend and enable POP3/SMTP service in "Settings" => "Accounts", as shown in the figure below:
!(#)
QQ Mail sets the password by generating an authorization code:
!(#)
The Java code is as follows:
## SendEmail2.java File Code:
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){String to = "xxx@qq.com"; String from = "xxx@qq.com"; String host = "smtp.qq.com"; Properties properties = System.getProperties(); properties.setProperty("mail.smtp.host", host); properties.put("mail.smtp.auth", "true"); Session session = Session.getDefaultInstance(properties,new Authenticator(){public PasswordAuthentication getPasswordAuthentication(){return new PasswordAuthentication("xxx@qq.com", "qqEmail Authorization Code"); }}); try{MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject("This is the Subject Line!"); message.setText("This is actual message"); Transport.send(message); System.out.println("Sent message successfully....from example.com"); }catch(MessagingException mex){mex.printStackTrace(); }}}