Sending Email from Java application (JavaMail sample program)

By: Narayanan Viewed: 153818 times  Printer Friendly Format    


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 = "server.myhost.com";
String to = "YourFriend@someemail.com";
String from = "Me@myhost.com";
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("mail.host", 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.
session.setDebug(sessionDebug);
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.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText);
// Hand the message to the default transport service
// for delivery.
Transport.send(msg);
}
catch (MessagingException mex) {
mex.printStackTrace();
}
}
}


Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

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

2. Hi professional,
I tried the above code but

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

3. Hey Narayanan, maybe you should check out Vesijama
View Tutorial          By: Benny Bottema at 2009-05-03 14:27:07

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

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

5. Hello,
I've tried the above code, but I've

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

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

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

8. When copying this code into my java IDE, should i
View Tutorial          By: Barabas at 2010-02-11 03:57:09

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

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

11. hai, i tried the abvoe code, m getting the follwin
View Tutorial          By: anith at 2011-03-13 06:02:31

12. Hi for this to work your smtp service should be ru
View Tutorial          By: Helper at 2011-03-13 07:49:31

13. Simple example and very useful.
Appreciate

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

14. how to know smtp host name and port number...

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

15. Hi
I have this error in my application whe

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

16. hai, i tried the abvoe code, m getting the follwin
View Tutorial          By: charan at 2011-10-20 12:59:59

17. hi to all i m getting this error plz solve it and
View Tutorial          By: krishna prasad at 2011-10-28 10:16:26

18. hello after working with this code i got the follw
View Tutorial          By: Tapaswini Sabat at 2011-11-05 12:43:49

19. Hello, I have been using a very similar code on de
View Tutorial          By: Rod at 2011-11-13 15:09:52

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

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

22. Including the required mail properties should solv
View Tutorial          By: Rajesh Vaddi at 2011-12-13 23:55:15

23. Hi,
What is the process if I want to know w

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

24. I am getting this exeception when trying to send m
View Tutorial          By: praveen at 2012-12-27 11:15:14