Sending Email in Java through Gmail Server

Sending Email in Java through Gmail Server

In this guide we will discuss about Sending Email in Java through Gmail Server. We can send email by using the SMTP server of gmail. It is good if you are don’t have any SMTP server and reliable. Here we will learn how to send email through gmail server by SSL (Secured Socket Layer). SSL is basically used for security if you are sending email through gmail server.

For better understanding of this example, learn the steps of sending email using JavaMail API first.

For sending the email using JavaMail API, you need to load the two jar files:

  • mail.jar
  • activation.jar

go to the Oracle site to download the latest version.

Example of Sending Email through Gmail Server with SSL

import java.util.Properties;    
import javax.mail.*;    
import javax.mail.internet.*;    
class Mailer{  
    public static void send(String from,String password,String to,String sub,String msg){  
          //Get properties object    
          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");    
          //get Session   
          Session session = Session.getDefaultInstance(props,    
           new javax.mail.Authenticator() {    
           protected PasswordAuthentication getPasswordAuthentication() {    
           return new PasswordAuthentication(from,password);  
           }    
          });    
          //compose message    
          try {    
           MimeMessage message = new MimeMessage(session);    
           message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));    
           message.setSubject(sub);    
           message.setText(msg);    
           //send message  
           Transport.send(message);    
           System.out.println("message sent successfully");    
          } catch (MessagingException e) {throw new RuntimeException(e);}    
             
    }  
}  
public class SendMailSSL{    
 public static void main(String[] args) {    
     //from,password,to,subject,message  
     Mailer.send("[email protected]","xxxxx","[email protected]","hello adglob","How r u?");  
     //change from, password and to  
 }    
}    

As you can see in the above example, userid and password need to be authenticated. As, this program illustrates, you can send email easily but change the username and password accordingly. Let’s see how to run it once again by simple technique:

Load the jar filec:\> set classpath=mail.jar;activation.jar;.;
compile the source filec:\> javac SendMailSSL.java
run byc:\> java SendMailSSL

Resolving AuthenticationFailedException

Click on this link and click on turn on radio button to allow users to send mail from unknown location. https://www.google.com/settings/security/lesssecureapps

Next Topic : Click Here

Leave a Reply