Send a mail through core java by using Gmail authentication
In the current post, I am trying to explain how to send a mail
through the core java program by using Gmail authentication. The below mentioned
only one core program which will contain the source code of mail sender. In
the below source you have to place your Gmail username, password and from
address and to address.
As we know mail.jar is not present in J2SE, we have to download externally and place it into CLASSPATH. Along with mail.jar download the below list of jars and add it into CLASSPATH to avoid compilation error.
Hurrreeeyyyyy………did you got an exception which is related to the Authentication. Now, you’re in the right way. You have to log in to your Gmail to modify authentication settings by clicking on this link.
As we know mail.jar is not present in J2SE, we have to download externally and place it into CLASSPATH. Along with mail.jar download the below list of jars and add it into CLASSPATH to avoid compilation error.
- dsn-1.5.5.jar
- gimap-1.5.5.jar
- imap-1.5.5.jar
- javax.mail.jar
- javax.mail-api-1.5.5.jar
- logging-mailhandler-1.5.5.jar
- mailapi-1.5.5.jar
- pop3-1.5.5.jar
- smtp-1.5.5.jar
GoogleMail.java
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 GoogleMail { public static void main(String... args) { final String username = "username@gmail.com"; final String password = "password"; Properties prop = new Properties(); prop.put("mail.smtp.auth", "true"); prop.put("mail.smtp.starttls.enable", "true"); prop.put("mail.smtp.host", "smtp.gmail.com"); prop.put("mail.smtp.port", "587"); Session session = Session.getInstance(prop, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { Message finalMessage = new MimeMessage(session); finalMessage.setFrom(new InternetAddress("from@gmail.com")); finalMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse("to-mail@gmail.com")); finalMessage.setSubject("Testing Email"); finalMessage.setText("Dear Crawler," + "\n\n This mail your getting as part of testing, please ignore! "); Transport.send(finalMessage); System.out.println("COOOLLL....Successfully sent email to your TO address. Please verify it. :)"); } catch (MessagingException e) { throw new RuntimeException(e); } } }
Please clear if you have got any compilation errors and run
the program to send an email. But, before running the program you make sure you
have placed proper username, password, from address and to address.
Hurrreeeyyyyy………did you got an exception which is related to the Authentication. Now, you’re in the right way. You have to log in to your Gmail to modify authentication settings by clicking on this link.
Then you run once again the GmailSender.java program to send
an email. If there are no exceptions you are getting, you will get the below
message on your console.
COOOLLL....Successfully
sent email to your TO address. Please verify it. :)
If you got that message in your console, you login into the
Gmail which you have placed into TO mail and verify you got the mail or not.