【问题标题】:Program for sending a message to email in Java用 Java 向电子邮件发送消息的程序
【发布时间】:2020-11-27 17:50:47
【问题描述】:

我创建了一个用 Java 将文件发送到电子邮件的程序。如何插入一个每 2 分钟自动发送邮件的计时器? 我认为可以通过计时器,但如果有人有不同的方式或遇到这个或类似的问题,它会对我有很大帮助。

代码如下:

public class EMail {

    public static void main(String[] args) 
    {
        SendEmail();

    }
    private static void SendEmail ()
    {
        final String username = "youremail@gmail.com";
        final String password = "password";

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

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

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("youremail@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("youremail@gmail.com"));
            message.setSubject("Testing Subject");
            message.setText("PFA");

            MimeBodyPart messageBodyPart = new MimeBodyPart();

            Multipart multipart = new MimeMultipart();

            messageBodyPart = new MimeBodyPart();
            String file = "C:\\Users\\Name\\UserData\\Logs.txt";
            String fileName = "Logs.txt";
            DataSource source = new FileDataSource(file);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(fileName);
            multipart.addBodyPart(messageBodyPart);

            message.setContent(multipart);

            System.out.println("Sending");

            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            e.printStackTrace();
        }
    
    }
}

【问题讨论】:

  • 带定时器的线程适合这个。您的发送电子邮件代码与此无关

标签: java email send


【解决方案1】:

您可以这样做。此代码将使您能够每两分钟发送一次邮件。

public class EmailHandler{

    public static void main(String[] args){

        Thread emailSenderThread = new Thread(new EmailSender());
        emailSenderThread.start();

    }
    private static class EmailSender implements Runnable {

          private void sendEmail(){
          //Email sending logic Impl
          }
          @Override
          public void run() {
               for(;;){
                   sendEmail();
                   try {
                       Thread.sleep(120000);//Thread is sleeping for 2 minutes
                   } catch (InterruptedException e) {
                       System.out.println(e);
                   }

               }
           }

      }


}

【讨论】:

    【解决方案2】:

    您可以使用TimerTimerTask,它们是用于在后台线程中安排任务的 java util 类

    示例(您可以将此添加到main 方法):

    TimerTask timerTask = new TimerTask() {
    
      @Override
      public void run() {
       SendEmail();
     }
    };
    Timer timer = new Timer("MyTimer"); 
    timer.scheduleAtFixedRate(timerTask, 500, 2000);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-10
      • 1970-01-01
      • 1970-01-01
      • 2015-01-13
      • 2015-05-15
      • 2021-10-25
      • 1970-01-01
      • 2016-11-21
      相关资源
      最近更新 更多