grach

MailSenderWithRandomDelay

Jul 20th, 2020
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.08 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.IOException;
  3. import java.nio.file.Files;
  4. import java.nio.file.Path;
  5. import javax.mail.*;
  6. import javax.mail.internet.*;
  7. import java.io.File;
  8. import java.util.Scanner;
  9. import java.io.IOException;
  10. import java.nio.file.Files;
  11. import java.nio.file.Path;
  12. import java.util.concurrent.ThreadLocalRandom;
  13. import java.util.Random;
  14.  
  15. public class EmailSenderFromFile {
  16.     public static void main(String[] args) throws IOException, InterruptedException {
  17.         Scanner scan = new Scanner(System.in);
  18.  
  19.         Path fileName = Path.of("D:/JavaBasicBabyBeginner/NestedConditionalStatment/Java-Fundamentals-May-2020/11 Arrays Exercise/src/emailText2.txt");
  20.  
  21.         Files.readString(fileName);
  22.         // String actual = Files.readString(fileName);
  23.         Files.readString(fileName);
  24.         //String actual = Files.readString(fileName);
  25.         List<String> stringList = Files.readAllLines(fileName);
  26.  
  27.  
  28.         String importantInfo[] = stringList.toArray(new String[]{});
  29.         Random rand = new Random();
  30.  
  31.         int min = 600000;
  32.         int max = 1560000;
  33.  
  34.         for (int i = 0; i < importantInfo.length;i++) {
  35.             int delay = rand.nextInt((max - min) + 1) + min;
  36.             //Pause for 4 seconds
  37.             Thread.sleep(delay);
  38.             //Print a message
  39.             System.out.println(importantInfo[i]);
  40.             int minutes = delay/60000;
  41.             System.out.println("Delay EMAIL with= "+ minutes + " minutes" );
  42.  
  43.  
  44.             final String username = "rado*****@*****.com";  // like [email protected]
  45.             final String password = "*******";   // password here
  46.  
  47.             Properties props = new Properties();
  48.             props.put("mail.smtp.auth", "true");
  49.             props.put("mail.smtp.starttls.enable", "true");
  50.             props.put("mail.smtp.host", "smtp-mail.outlook.com");
  51.  
  52.             props.put("mail.smtp.port", "587");
  53.  
  54.             Session session = Session.getInstance(props,
  55.                     new javax.mail.Authenticator() {
  56.                         @Override
  57.                         protected PasswordAuthentication getPasswordAuthentication() {
  58.                             return new PasswordAuthentication(username, password);
  59.                         }
  60.                     });
  61.             session.setDebug(true);
  62.  
  63.             try {
  64.  
  65.                 Message message = new MimeMessage(session);
  66.                 message.setFrom(new InternetAddress(username));
  67.  
  68.  
  69.                 message.addRecipients(Message.RecipientType.BCC,
  70.                           InternetAddress.parse((importantInfo[i])));   // like [email protected]
  71.                 message.setSubject("90000 Samo za proba BCC");
  72.                 message.setText("Hello, this is example of sending Java email with 90000 milliseconds delay , message should be deleted, you can't see the sender list");
  73.  
  74.                 Transport.send(message);
  75.  
  76.                 System.out.println("Done");
  77.  
  78.             } catch (MessagingException e) {
  79.                 throw new RuntimeException(e);
  80.             }
  81.         }
  82.     }
  83. }
Add Comment
Please, Sign In to add comment