Sending Email from Java application (JavaMail sample program)

By: Narayanan  

You can use the JavaMail API to send emails from your Java application. Though the JavaMail API allows you to do many things including the ability to retrieve and read the emails or sending emails etc, this sample java program demonstrates how to send email from your Java application. Remember to change the email host (String host) to your email server host, otherwise it won't work.

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SimpleSendEmail {
public static void main(String[] args) {
// Collect the necessary information to send a simple message
// Make sure to replace the values for host, to, and from with
// valid information.
// host - must be a valid smtp server that you currently have
// access to.
// to - whoever is going to get your email
// from - whoever you want to be. Just remember that many smtp
// servers will validate the domain of the from address
// before allowing the mail to be sent.
String host = "";
String to = "[email protected]";
String from = "[email protected]";
String subject = "My First Email";
String messageText = "I am sending a message using the"
+ " JavaMail API.\n"
+ "Here type your message.";
boolean sessionDebug = false;
// Create some properties and get the default Session.
Properties props = System.getProperties();
props.put("", host);
props.put("mail.transport.protocol", "smtp");
Session session = Session.getDefaultInstance(props, null);
// Set debug on the Session so we can see what is going on
// Passing false will not echo debug info, and passing true
// will.
try {
// Instantiate a new MimeMessage and fill it with the
// required information.
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSentDate(new Date());
// Hand the message to the default transport service
// for delivery.
catch (MessagingException mex) {

Archived Comments

1. I am getting this exeception when trying to send mail..

DEBUG: JavaMail versi

View Tutorial          By: praveen at 2012-12-27 11:15:14

2. Hi,
What is the process if I want to know whether the email has been successfully sent to the

View Tutorial          By: Koushik Roy at 2012-07-13 05:35:35

3. Including the required mail properties should solve most of the errors listed above.... try it out
View Tutorial          By: Rajesh Vaddi at 2011-12-13 23:55:15

4. hello how can i get a better java date code
View Tutorial          By: ramutomotest at 2011-12-08 17:44:49

5. hiiiiiii i am debasis this is test mail.............!!!!!!!!
View Tutorial          By: debasis sahu at 2011-12-01 08:58:17

6. Hello, I have been using a very similar code on development of a email verification system. All have
View Tutorial          By: Rod at 2011-11-13 15:09:52

7. hello after working with this code i got the follwing Exception message

View Tutorial          By: Tapaswini Sabat at 2011-11-05 12:43:49

8. hi to all i m getting this error plz solve it and replied to my mail
[email protected]

View Tutorial          By: krishna prasad at 2011-10-28 10:16:26

9. hai, i tried the abvoe code, m getting the follwing errors... please help me...

View Tutorial          By: charan at 2011-10-20 12:59:59

10. Hi
I have this error in my application when servlet is tryinto to consult a db, but from oth

View Tutorial          By: María at 2011-10-04 11:47:50

11. how to know smtp host name and port number...
thak u....

View Tutorial          By: nag at 2011-09-10 12:29:17

12. Simple example and very useful.
Appreciate you.

View Tutorial          By: Mohan at 2011-08-11 05:48:19

13. Hi for this to work your smtp service should be running and the port 25 should be open.
View Tutorial          By: Helper at 2011-03-13 07:49:31

14. hai, i tried the abvoe code, m getting the follwing errors... please help me...

View Tutorial          By: anith at 2011-03-13 06:02:31

15. its not good
View Tutorial          By: rishi verma at 2010-10-17 07:59:17

16. worked for me thanks.
View Tutorial          By: dem125 at 2010-08-03 23:30:32

17. When copying this code into my java IDE, should i configure any thing else in my conputer?

View Tutorial          By: Barabas at 2010-02-11 03:57:09

18. i need some simple java programs
View Tutorial          By: vinodh at 2009-11-24 04:25:48

19. i need some simple java programs
View Tutorial          By: ramya at 2009-11-24 04:25:07

20. Hello,
I've tried the above code, but I've ended up in the following error.


View Tutorial          By: Phyo Phyo at 2009-09-14 05:32:28

21. I tried this code,but is showing this error


View Tutorial          By: anju at 2009-05-22 07:42:11

22. Hey Narayanan, maybe you should check out Vesijama as well. It's a small (open source) wrapper libra
View Tutorial          By: Benny Bottema at 2009-05-03 14:27:07

23. Mail me Codings in java of different programs
View Tutorial          By: vikram at 2008-09-12 06:04:42

24. Hi professional,
I tried the above code but it is throwing the following error(i included mai

View Tutorial          By: Srinivas at 2008-05-05 23:32:13

Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial