英文:
Program for sending a message to email in Java
问题
我创建了一个在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();
        }
    }
}
希望这有所帮助!
英文:
I created a program that sends files to email in Java. How to insert a timer that will automatically send mail every 2 minutes?
I think it could be through a timer, but if someone has a different way or encountered this or a similar problem, it would help me a lot.
Here's the code:
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();
}
}
}
答案1
得分: 0
你可以使用Timer和TimerTask,它们是Java工具类,用于在后台线程中安排任务。
示例(您可以将此代码添加到main方法中):
TimerTask timerTask = new TimerTask() {
  @Override
  public void run() {
    发送邮件();
  }
};
Timer timer = new Timer("MyTimer");
timer.scheduleAtFixedRate(timerTask, 500, 2000);
英文:
You can use Timer and TimerTask which are java util classes used to schedule tasks in a background thread
Example (You can add this to main method):
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
SendEmail();
}
};
Timer timer = new Timer("MyTimer"); 
timer.scheduleAtFixedRate(timerTask, 500, 2000);
答案2
得分: 0
以下是翻译好的部分:
你可以像这样做。这段代码将使你能够每两分钟发送一封邮件。
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() {
            // 邮件发送逻辑实现
        }
        @Override
        public void run() {
            for (;;) {
                sendEmail();
                try {
                    Thread.sleep(120000); // 线程休眠2分钟
                } catch (InterruptedException e) {
                    System.out.println(e);
                }
            }
        }
    }
}
英文:
You can do something like this.This code will enable you to send mails for every two minutes.
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);
}
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论