Sunday, September 15, 2019

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
download these jar files (or) go to the Oracle site to download the latest version.

Disable the antivirus e.g. avast etc. because it may interrupt your code to send emails.

Example of Sending Email through Gmail Server with SSL

  1. import java.util.Properties;    
  2. import javax.mail.*;    
  3. import javax.mail.internet.*;    
  4. class Mailer{  
  5.     public static void send(String from,String password,String to,String sub,String msg){  
  6.           //Get properties object    
  7.           Properties props = new Properties();    
  8.           props.put("mail.smtp.host""smtp.gmail.com");    
  9.           props.put("mail.smtp.socketFactory.port""465");    
  10.           props.put("mail.smtp.socketFactory.class",    
  11.                     "javax.net.ssl.SSLSocketFactory");    
  12.           props.put("mail.smtp.auth""true");    
  13.           props.put("mail.smtp.port""465");    
  14.           //get Session   
  15.           Session session = Session.getDefaultInstance(props,    
  16.            new javax.mail.Authenticator() {    
  17.            protected PasswordAuthentication getPasswordAuthentication() {    
  18.            return new PasswordAuthentication(from,password);  
  19.            }    
  20.           });    
  21.           //compose message    
  22.           try {    
  23.            MimeMessage message = new MimeMessage(session);    
  24.            message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));    
  25.            message.setSubject(sub);    
  26.            message.setText(msg);    
  27.            //send message  
  28.            Transport.send(message);    
  29.            System.out.println("Message sent successfully");    
  30.           } catch (MessagingException e) {throw new RuntimeException(e);}    
  31.              
  32.     }  
  33. }  
  34. public class SendMailSSL{    
  35.  public static void main(String[] args) {    
  36.      //from,password,to,subject,message  
  37.      Mailer.send("santosh3033@gmail.com","xxxxx","biveka3033@gmail.com","Hello JavaCodeGuruJi","How are you?");  
  38.      //change from, password and to  
  39.  }    
  40. }    
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

No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...