Professional Documents
Culture Documents
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
NikeBlack Sports
<<prev next>>
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.
1. 2.
public static Session getDefaultInstance(Properties props) public static Session getDefaultInstance(Properties props,Authenticator auth)
2. 3.
www.javatpoint.com/example-of-sending-email-using-java-mail-api#
1/5
6/23/13
2.
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.*;
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();} } }
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
next>>
5815
219
26
Google +
Tw eet
91
Like
11k
javatpoint.com
Like You like this.
www.javatpoint.com/example-of-sending-email-using-java-mail-api#
4/5
6/23/13
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