【问题标题】:Automating existing web application and email generated report自动化现有的 Web 应用程序和电子邮件生成的报告
【发布时间】:2019-07-07 07:47:21
【问题描述】:

我确实有一个 Java 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

【问题讨论】:

    标签: java smtp gmail jakarta-mail


    【解决方案1】:

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

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

    【讨论】:

    • 好的。所以在那种情况下,我工作的公司可能有防火墙,它会阻止我的电话。我尝试 ping,但我收到套接字超时消息。所以可能我的防火墙会阻止它。
    猜你喜欢
    • 2012-11-30
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    • 2020-04-09
    • 2012-06-20
    • 1970-01-01
    • 2012-09-15
    • 1970-01-01
    相关资源
    最近更新 更多