Search code examples
javasmtpgmailjakarta-mail

Automating existing web application and email generated report


I do have a java web application that is used for storing data about issues we faces daily. There are many users and each user will be inserting data into it daily and by the end of day we'll gather that particular days data and then mail it to everyone. We are using the web app to do this. But as of now web app generates consolidated data for one day when I trigger it using a button press. And the code written to create excel sheet drafts a excel sheet and this one i mail to everyone. This web application is being run on a server. I wish to automate this process i.e. each night at some specified time it should automatically trigger the function and generate the excel sheet and it should mail it to everyone. I don't have much idea about how to go forward so I thought first I'll write java code to send mail and later think about triggering the function at a particular time. I would like to know whether is there any other better way to deal with my requirement. I'am open to suggestions. Also, the code I have written to send the mail is not working. It is giving me exception and I tried almost everything I could find but nothing is working. Could someone help me in sorting out the error. Here's the code :

package com.email;

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 SendEmail {
   public static void main(String[] args) {
      // Recipient's email ID needs to be mentioned.
      String to = "[email protected]";

      // Sender's email ID needs to be mentioned
      String from = "[email protected]";
      final String username = "username";//change accordingly
      final String password = "password";//change accordingly

      String host = "smtp.gmail.com";

      Properties props = new Properties();
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.starttls.enable", "true");
      props.put("mail.smtp.host", host);
      props.put("mail.smtp.port", "587");

      // Get the Session object.
      Session session = Session.getInstance(props,
         new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
               return new PasswordAuthentication(username, password);
       }
         });

      try {
       // Create a default MimeMessage object.
       Message message = new MimeMessage(session);

       // Set From: header field of the header.
       message.setFrom(new InternetAddress(from));

       // Set To: header field of the header.
       message.setRecipients(Message.RecipientType.TO,
               InternetAddress.parse(to));

       // Set Subject: header field
       message.setSubject("Testing Subject");

       // Now set the actual message
       message.setText("Hello, this is sample for to check send " +
        "email using JavaMailAPI ");

       // Send message
       Transport.send(message);

       System.out.println("Sent message successfully....");

      } catch (MessagingException e) {
         throw new RuntimeException(e);
      }
   }
}

Exception that I am receiving is :

Exception in thread "main" java.lang.RuntimeException: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;
  nested exception is:
    java.net.UnknownHostException: smtp.gmail.com
    at com.tutorialspoint.SendEmail.main(SendEmail.java:62)
Caused by: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 465; timeout -1;
  nested exception is:
    java.net.UnknownHostException: smtp.gmail.com
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2209)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:740)
    at javax.mail.Service.connect(Service.java:388)
    at javax.mail.Service.connect(Service.java:246)
    at javax.mail.Service.connect(Service.java:195)
    at javax.mail.Transport.send0(Transport.java:254)
    at javax.mail.Transport.send(Transport.java:124)
    at com.email.SendEmail.main(SendEmail.java:57)
Caused by: java.net.UnknownHostException: smtp.gmail.com
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at java.net.Socket.connect(Socket.java:528)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:353)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:239)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2175)
    ... 7 more

Solution

  • According to the logs you cannot reach the gmail smtp. Are you maybe behind a proxy/firewall? Did you try pinging the smtp?

    For running the tasks automatically you just need to add a scheduling framework to run the code at a certain time. You could use quartz for example