自动化现有的Web应用程序和电子邮件生成的报告

时间:2019-02-13 12:59:56

标签: java smtp gmail javamail

我确实有一个Java Web应用程序,用于存储有关我们每天面临的问题的数据。 有很多用户,每个用户每天都会在其中插入数据,到一天结束时,我们将收集特定日期的数据,然后将其邮寄给所有人。 我们正在使用网络应用程序执行此操作。但是截至目前,当我使用按钮触发时,Web应用程序会生成一天的合并数据。 编写用于创建excel工作表的代码起草了一份excel工作表,并将此工作表邮寄给所有人。该Web应用程序正在服务器上运行。 我希望自动化该过程,即每天晚上在指定的时间自动触发该函数并生成excel工作表并将其邮寄给所有人。 我对如何前进没有太多想法,所以我认为首先要编写Java代码来发送邮件,然后再考虑在特定时间触发该功能。 我想知道是否还有其他更好的方法来满足我的要求。我愿意接受建议。 另外,我为发送邮件编写的代码不起作用。它给了我例外,我尝试了几乎所有可以找到的东西,但是没有任何效果。有人可以帮我解决错误。 这是代码:

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 = "toemail@gmail.com";

      // Sender's email ID needs to be mentioned
      String from = "fromemail@gmail.com";
      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 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

1 个答案:

答案 0 :(得分:1)

根据日志,您无法访问gmail smtp。您可能在代理/防火墙后面吗?您是否尝试ping smtp?

要自动运行任务,您只需要添加一个计划框架即可在特定时间运行代码。 您可以使用quartz例如