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

问题描述 投票:0回答:1

我有一个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 = "[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 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
java smtp gmail javamail
1个回答
1
投票

根据日志,你无法访问gmail smtp。你是否可能落后于代理/防火墙?你试过ping smtp吗?

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

© www.soinside.com 2019 - 2024. All rights reserved.