发送邮件到邮箱的Java程序

huangapple go评论101阅读模式
英文:

Program for sending a message to email in Java

问题

我创建了一个在Java中将文件发送到电子邮件的程序。如何插入一个定时器,以便每2分钟自动发送邮件?
我认为可以通过一个定时器来实现,但如果有人有不同的方法或遇到类似的问题,那将对我有很大帮助。

以下是代码:

  1. public class EMail {
  2. public static void main(String[] args) {
  3. SendEmail();
  4. }
  5. private static void SendEmail() {
  6. final String username = "youremail@gmail.com";
  7. final String password = "password";
  8. Properties props = new Properties();
  9. props.put("mail.smtp.auth", true);
  10. props.put("mail.smtp.starttls.enable", true);
  11. props.put("mail.smtp.host", "smtp.gmail.com");
  12. props.put("mail.smtp.port", "587");
  13. Session session = Session.getInstance(props, new javax.mail.Authenticator() {
  14. protected PasswordAuthentication getPasswordAuthentication() {
  15. return new PasswordAuthentication(username, password);
  16. }
  17. });
  18. try {
  19. Message message = new MimeMessage(session);
  20. message.setFrom(new InternetAddress("youremail@gmail.com"));
  21. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("youremail@gmail.com"));
  22. message.setSubject("Testing Subject");
  23. message.setText("PFA");
  24. MimeBodyPart messageBodyPart = new MimeBodyPart();
  25. Multipart multipart = new MimeMultipart();
  26. messageBodyPart = new MimeBodyPart();
  27. String file = "C:\\Users\\Name\\UserData\\Logs.txt";
  28. String fileName = "Logs.txt";
  29. DataSource source = new FileDataSource(file);
  30. messageBodyPart.setDataHandler(new DataHandler(source));
  31. messageBodyPart.setFileName(fileName);
  32. multipart.addBodyPart(messageBodyPart);
  33. message.setContent(multipart);
  34. System.out.println("Sending");
  35. Transport.send(message);
  36. System.out.println("Done");
  37. } catch (MessagingException e) {
  38. e.printStackTrace();
  39. }
  40. }
  41. }

希望这有所帮助!

英文:

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:

  1. public class EMail {
  2. public static void main(String[] args)
  3. {
  4. SendEmail();
  5. }
  6. private static void SendEmail ()
  7. {
  8. final String username = "youremail@gmail.com";
  9. final String password = "password";
  10. Properties props = new Properties();
  11. props.put("mail.smtp.auth", true);
  12. props.put("mail.smtp.starttls.enable", true);
  13. props.put("mail.smtp.host", "smtp.gmail.com");
  14. props.put("mail.smtp.port", "587");
  15. Session session = Session.getInstance(props,
  16. new javax.mail.Authenticator()
  17. {
  18. protected PasswordAuthentication getPasswordAuthentication() {
  19. return new PasswordAuthentication(username, password);
  20. }
  21. });
  22. try {
  23. Message message = new MimeMessage(session);
  24. message.setFrom(new InternetAddress("youremail@gmail.com"));
  25. message.setRecipients(Message.RecipientType.TO,
  26. InternetAddress.parse("youremail@gmail.com"));
  27. message.setSubject("Testing Subject");
  28. message.setText("PFA");
  29. MimeBodyPart messageBodyPart = new MimeBodyPart();
  30. Multipart multipart = new MimeMultipart();
  31. messageBodyPart = new MimeBodyPart();
  32. String file = "C:\\Users\\Name\\UserData\\Logs.txt";
  33. String fileName = "Logs.txt";
  34. DataSource source = new FileDataSource(file);
  35. messageBodyPart.setDataHandler(new DataHandler(source));
  36. messageBodyPart.setFileName(fileName);
  37. multipart.addBodyPart(messageBodyPart);
  38. message.setContent(multipart);
  39. System.out.println("Sending");
  40. Transport.send(message);
  41. System.out.println("Done");
  42. } catch (MessagingException e) {
  43. e.printStackTrace();
  44. }
  45. }
  46. }

答案1

得分: 0

你可以使用TimerTimerTask,它们是Java工具类,用于在后台线程中安排任务。

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

  1. TimerTask timerTask = new TimerTask() {
  2. @Override
  3. public void run() {
  4. 发送邮件();
  5. }
  6. };
  7. Timer timer = new Timer("MyTimer");
  8. 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):

  1. TimerTask timerTask = new TimerTask() {
  2. @Override
  3. public void run() {
  4. SendEmail();
  5. }
  6. };
  7. Timer timer = new Timer("MyTimer");
  8. timer.scheduleAtFixedRate(timerTask, 500, 2000);

答案2

得分: 0

以下是翻译好的部分:

你可以像这样做。这段代码将使你能够每两分钟发送一封邮件。

  1. public class EmailHandler {
  2. public static void main(String[] args) {
  3. Thread emailSenderThread = new Thread(new EmailSender());
  4. emailSenderThread.start();
  5. }
  6. private static class EmailSender implements Runnable {
  7. private void sendEmail() {
  8. // 邮件发送逻辑实现
  9. }
  10. @Override
  11. public void run() {
  12. for (;;) {
  13. sendEmail();
  14. try {
  15. Thread.sleep(120000); // 线程休眠2分钟
  16. } catch (InterruptedException e) {
  17. System.out.println(e);
  18. }
  19. }
  20. }
  21. }
  22. }
英文:

You can do something like this.This code will enable you to send mails for every two minutes.

  1. public class EmailHandler{
  2. public static void main(String[] args){
  3. Thread emailSenderThread = new Thread(new EmailSender());
  4. emailSenderThread.start();
  5. }
  6. private static class EmailSender implements Runnable {
  7. private void sendEmail(){
  8. //Email sending logic Impl
  9. }
  10. @Override
  11. public void run() {
  12. for(;;){
  13. sendEmail();
  14. try {
  15. Thread.sleep(120000);//Thread is sleeping for 2 minutes
  16. } catch (InterruptedException e) {
  17. System.out.println(e);
  18. }
  19. }
  20. }
  21. }
  22. }

huangapple
  • 本文由 发表于 2020年8月7日 19:35:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63300988.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定