You are on page 1of 5

6/23/13

Example of sending email using JavaMail API - javatpoint

New: JAXB | Junit

Advertise Us | Ask Question | forum | login|

Home

Core Java

Servlet

JSP

Struts2

Mail API

Hibernate

Spring

Android

Quiz

Projects

Interview Q

Comment

Forum

JavaMail API Basics of JavaMail Sending Email Sending email through Gmail server Receiving Email Sending email with attachment Receiving email with attachment Sending HTML content Forwarding email Deleting email

American TouristerBlack College BackpackShow Now!Rs. 1,520Neu

NikeBlack Sports
<<prev next>>

Sending Email using JavaMail API


There are various ways to send email using JavaMail API. For this purpose, you must have SMTP server that is responsible to send mails. You can use one of the following techniques to get the SMTP server: Install and use any SMTP server such as Postcast server, Apache James server, cmail server etc. (or) Use the SMTP server provided by the host provider e.g. my SMTP server is mail.javatpoint.com (or) Use the SMTP Server provided by other companies e.g. gmail etc.

Sending Email using JavaMail API Steps to send email using JavaMail API Get the session object Compose the message send the message Simple example of sending email using JavaMail API Example of sending email using JavaMail API through the SMTP server provided by the host provider

Here, we are going to learn above three aproches to send email using javamail API. But we should learn the basic steps to send email from java application.

Steps to send email using JavaMail API


There are following three steps to send email using JavaMail. They are as follows: 1. Get the session object that stores all the informations of host like host name, username, password etc. 2. compose the message 3. send the message

1) Get the session object


The javax.mail.Session class provides two methods to get the object of session, Session.getDefaultInstance() method and Session.getInstance() method. You can use anyone method to get the session object. Examples of these methods are as follows:

AdidasBlack Sports BackpackShow

Syntax of getDefaultInstance() method


There are two methods to get the session object by using the getDefaultInstance() method. It returns the default session.

Now!Rs. 1,999 GEARBlack BackpackShow Now!Rs. 899Neu

1. 2.

public static Session getDefaultInstance(Properties props) public static Session getDefaultInstance(Properties props,Authenticator auth)

Example of getDefaultInstance() method


1. Properties properties=new Properties(); //fill all the informations like host name etc. Session session=Session.getDefaultInstance(properties,null);

NikeRed Sports BackpackShow Now!-20%Rs.

2. 3.

Syntax of getInstance() method


1,836Rs. 2,295 NikeBlack Sports BackpackShow Now!-20%Rs.
There are two methods to get the session object by using the getInstance() method. It returns the new session. 1. 2. public static Session getInstance(Properties props) public static Session getInstance(Properties props,Authenticator auth)

www.javatpoint.com/example-of-sending-email-using-java-mail-api#

1/5

6/23/13

2.

public static Session getInstance(Properties props,Authenticator auth)

Example of sending email using JavaMail API - javatpoint

Example of getDefaultInstance() method


1,276Rs. 1,595
1. 2. 3. Properties properties=new Properties(); //fill all the informations like host name etc. Session session=Session.getInstance(properties,null);

2) Compose the message


The javax.mail.Message class provides methods to compose the message. But it is an abstract class so its subclass javax.mail.internet.MimeMessage class is mostly used. To create the message, you need to pass session object in MimeMessage class constructor. For example: 1. MimeMessage message=new MimeMessage(session); Now message object has been created but to store information in this object MimeMessage class provides many methods. Let's see methods provided by the MimeMessage class:

Commonly used methods of MimeMessage class


1) public void setFrom(Address address): is used to set the from header field. 2) public void addRecipients(Message.RecipientType type, String addresses): is used to add the given address to the recipient type. 3) public void setSubject(String subject): is used to set the subject header field. 4) public void setText(String textmessage) is used to set the text as the message content using text/plain MIME type.

Example to compose the message:


1. 2. 3. 4. 5. 6. MimeMessage message=new MimeMessage(session); message.setFrom(new InternetAddress("sonoojaiswal@javatpoint.com")); message.addRecipient(Message.RecipientType.To, new InternetAddress("sonoojaiswal@javatpoint.com")); message.setHeader("Hi, everyone"); message.setText("Hi, This mail is to inform you...");

3) Send the message


The javax.mail.Transport class provides method to send the message.

Commonly used methods of Transport class


1) public static void send(Message message): is used send the message. 2) public static void send(Message message, Address[] address): is used send the message to the given addresses.

Example to send the message:


1. Transport.send(message);

Simple example of sending email using JavaMail API


In this example, we are going to learn how to send email by SMTP server installed on the machine e.g. Postcast server, Apache James server, Cmail server etc. If you want to send email by using your SMTP server provided by the host provider, see the example after this one. For sending the email using JavaMail API, you need to load the two jar files: mail.jar activation.jar download these jar files or go to the Oracle site to download the latest version. 1. 2. import java.util.*; import javax.mail.*;

www.javatpoint.com/example-of-sending-email-using-java-mail-api#

2/5

6/23/13

2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32.

import javax.mail.*;

Example of sending email using JavaMail API - javatpoint

import javax.mail.internet.*; import javax.activation.*; public class SendEmail { public static void main(String [] args){ String to = "sonoojaiswal1988@gmail.com";//change accordingly String from = "sonoojaiswal1987@gmail.com";change accordingly String host = "localhost";//or IP address //Get the session object Properties properties = System.getProperties(); properties.setProperty("mail.smtp.host", host); Session session = Session.getDefaultInstance(properties); //compose the message try { MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO,new InternetAddress(to)); message.setSubject("Ping"); message.setText("Hello, this is example of sending email "); // Send message Transport.send(message); System.out.println("message sent successfully...."); }catch (MessagingException mex) {mex.printStackTrace();} } }

download this example to send email


In this example, we are going to learn how to send email by SMTP server installed on the machine e.g. Postcast server, Apache James server, Cmail server etc. If you want to send email by using your SMTP server provided by the host provider, see the example after this one. To run this example, you need to load two jar files. There are 4 ways to load the jar file. One of the way is set classpath. Let's see how to run this example: Load the jar file c:\> set classpath=mail.jar;activation.jar;.; compile the source filec:\> javac SendEmail.java run by c:\> java SendEmail

Example of sending email using JavaMail API through the SMTP server provided by the host provider
If you are using the SMTP server provided by the host provider e.g. mail.javatpoint.com , you need to authenticate the username and password. The javax.mail.PasswordAuthentication class is used to authenticate the password. If you are sending the email using JavaMail API, load the two jar files: mail.jar activation.jar download these jar files or go to the Oracle site to download the latest version. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. //Get the session object Properties props = new Properties(); String to="sonoojaiswal1987@gmail.com";//change accordingly String host="mail.javatpoint.com"; final String user="sonoojaiswal@javatpoint.com";//change accordingly final String password="xxxxx";//change accordingly public class SendMailBySite { public static void main(String[] args) { import java.util.Properties; import javax.mail.*; import javax.mail.internet.*;

www.javatpoint.com/example-of-sending-email-using-java-mail-api#

3/5

6/23/13
15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. } } //send the message

Example of sending email using JavaMail API - javatpoint


Properties props = new Properties(); props.put("mail.smtp.host",host); props.put("mail.smtp.auth", "true"); Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user,password); } }); //Compose the message try { MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(user)); message.addRecipient(Message.RecipientType.TO,new InternetAddress(to)); message.setSubject("javatpoint"); message.setText("This is simple program of sending email using JavaMail API");

Transport.send(message); System.out.println("message sent successfully..."); } catch (MessagingException e) {e.printStackTrace();}

download this example to send email


As you can see in the above example, userid and password need to be authenticated. As, this program illustrates, you can send email easily. Change the username and password accordingly. Let's see how to run it once again by simple technique: Load the jar file c:\> set classpath=mail.jar;activation.jar;.; compile the source filec:\> javac SendMailBySite.java run by c:\> java SendMailBySite

next>>

5815

219

26
Google +

Tw eet

91

Like

11k

javatpoint.com
Like You like this.

You and 11,982 others like javatpoint.com.

F acebook social plugin

www.javatpoint.com/example-of-sending-email-using-java-mail-api#

4/5

6/23/13

Example of sending email using JavaMail API - javatpoint

Like the www.javatpoint.com on facebook / google+ / twitter / subscribe to get latest updates
Sitemap javatpoint.com is developed Core Java Servlet JSP Struts2 Hibernate Spring Android Interview Questions

to provide easy and point to point learning and training in detail. Examples might be simplified to improve reading and basic understanding. Tutorials and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. If there is any mistake, please mail to sonoojaiswal1987@gmail.com or sonoojaiswal@javatpoint.com. We provide assurance of 90% interview questions. While using this site, you agree to have read and accepted our terms of use and privacy policy. 2011-2013 Javatpoint. All Rights Reserved.

www.javatpoint.com/example-of-sending-email-using-java-mail-api#

5/5

You might also like