JavaMail API – Sending email via Gmail SMTP example
Written on March 28, 2010 at 1:39 pm by
mkyong
Here’s two examples to show how to use JavaMail API method to send an email via Gmail SMTP server using both TLS and SSL connection.
GMail SMTP details
http://mail.google.com/support/bin/answer.py?hl=en&answer=13287
Outgoing Mail (SMTP) Server requires TLS or SSL: smtp.gmail.com (use authentication) Use Authentication: Yes Port for TLS/STARTTLS: 587 Port for SSL: 465
1. JavaMail – GMail via TLS
Sending an email via Gmail SMTP server using TLS connection.
package com.mkyong.common; import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class JavaMailApp1 { public static void main( String[] args ) { String host = "smtp.gmail.com"; int port = 587; String username = "username"; String password = "password"; Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); Session session = Session.getInstance(props); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress("from@no-spam.com")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("to@no-spam.com")); message.setSubject("Testing Subject"); message.setText("Dear Mail Crawler,\n\n No spam to my email, please!"); Transport transport = session.getTransport("smtp"); transport.connect(host, port, username, password); Transport.send(message); System.out.println("Done"); } catch (MessagingException e) { throw new RuntimeException(e); } } }
2. JavaMail – GMail via SSL
Sending an email via Gmail SMTP server using SSL connection.
package com.mkyong.common; import java.util.Properties; 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 JavaMailApp2 { public static void main( String[] args ) { Properties props = new Properties(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "465"); Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("username","password"); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress("from@no-spam.com")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("to@no-spam.com")); message.setSubject("Testing Subject"); message.setText("Dear Mail Crawler,\n\n No spam to my email, please!"); Transport.send(message); System.out.println("Done"); } catch (MessagingException e) { throw new RuntimeException(e); } } }
This article was posted in Java category.
Oracle Magazine - Free Magazine
Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for Java's developers and DBAs, and more.
Securing & Optimizing Linux: The Hacking Solution - Free Guide
A comprehensive collection of Linux security products and explanations in the most simple and structured manner on how to safely and easily configure and run many popular Linux-based applications and services.
The Windows 7 Guide: From Newbies to Pros - Free Guide
In this 46 page guide you will be introduced to Windows 7 and what it has to offer. This guide will go over the software compatible issues, you will learn about the new taskbar, how to use and customize Windows Aero, what Windows 7 Libraries are all about, what software is included in Windows 7, and how easy networking is with Windows 7 along with other topics.
All Java Tutorials
- Java Core Technology - Java RegEx, Java XML, Java I/O, Java Misc
- J2EE Frameworks - Hibernate, Spring 2.5, Spring MVC, Struts 1.x, Struts 2.x
- Build Tools - Maven, Archiva
- Unit Test - jUnit, TestNG
- Client Scripts - jQuery
thank you sir, but it dose not work witn me and give me this erroe:
Exception in thread “main” java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 535-5.7.1 Username and Password not accepted. Learn more at
535 5.7.1 http://mail.google.com/support/bin/answer.py?answer=14257 g17sm4815100wee.5
at com.mkyong.common.JavaMailAppl2.main(JavaMailAppl2.java:78)
Caused by: javax.mail.AuthenticationFailedException: 535-5.7.1 Username and Password not accepted. Learn more at
535 5.7.1 http://mail.google.com/support/bin/answer.py?answer=14257 g17sm4815100wee.5
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:648)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:583)
at javax.mail.Service.connect(Service.java:313)
at javax.mail.Service.connect(Service.java:172)
at javax.mail.Service.connect(Service.java:121)
at javax.mail.Transport.send0(Transport.java:190)
at javax.mail.Transport.send(Transport.java:120)
at com.mkyong.common.JavaMailAppl2.main(JavaMailAppl2.java:57)
and I’m sure from username and password
Very good site thank you for all the content.
Using the above email prog how would I go about adding in the ability to attach a file?
Many thanks,
Peter
thank you sir this example helped me a lot and is very useful
it complete as you have shown the settings to be done on gmail for this
purpose via link
plz tell what should be done for rediffmail
Sorry, i dont have rediffmail account, you should check with rediffmail mail server setting and modify the code accordingly
TLS Failed for me? No tuts seem to work for GMail or Windows Live TLS :\
Thank you ! This example is very useful for me.
SSL works fine, and is very easier!!! Thanks man!
Cheers!!
SSL worked but TLS did not worked it says the authentication failed so may i know the reason why it say so and thanks for the code it worked after i tried so many codes and each code failed except urs so thank u so much
Excellent article! I used SSL approach and able to send mail to gmail account. I was stuck in this for some time. Thanks for the help.
you’re welcome