Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- import javax.mail.*;
- import javax.mail.internet.*;
- import javax.mail.Message;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- import javax.sql.*;
- import javax.activation.*;
- public class SecretSanta {
- //Set 2 boolean arrays
- Boolean[] santa;
- Boolean[] chosen;
- boolean santaPaired = true;
- boolean chosenPaired = true;
- public static void main(String[] args) {
- //For the main method, just use the fileread method in order to pair the santas/chosens
- SecretSanta ss = new SecretSanta();
- ss.fileRead();
- }
- public void fileRead() {
- List<String> aList = new ArrayList<String>();
- int size = 0;
- try {
- File emails = new File("/Users/Refai343643/Desktop/emails.txt");
- BufferedReader br = new BufferedReader(new FileReader(emails));
- //create a blank/null string in order to read the upcoming file lines
- String line = null;
- while((line = br.readLine()) != null) {
- size = size +1;
- aList.add(line);
- }
- br.close();
- }
- catch(Exception ex) {
- ex.printStackTrace();
- //If there's an error then catch it here
- }
- //now create 2 more different boolean arrays and fill them all with false
- chosen = new Boolean[size];
- santa = new Boolean[size];
- Arrays.fill(chosen, false);
- Arrays.fill(santa, false);
- //Assign random numbers to a integer in order to grab a random pair and assign them to p1 and p2
- Random rand = new Random();
- while(true) {
- int randomInt1 = rand.nextInt(size);
- int randomInt2 = rand.nextInt(size);
- String p1 = aList.get(randomInt1);
- String p2 = aList.get(randomInt2);
- if(p1==p2){
- //do nothing because you can't get yourself as a pair, try again.
- }
- if((p1!=p2) && (santa[randomInt1] == false) && (chosen[randomInt2] == false)) {
- String[] person1 = p1.split(" ");
- String[] person2 = p2.split(" ");
- String message = person1[0] + ", you are " + person2[0] + " " + "'s secret Santa!";
- System.out.println(message); //show what message is being sent
- sendEmail(person1[0], message);
- //use the send email method to send the emails from these messages
- santa[randomInt1] = true;
- chosen[randomInt2] = true;
- if(arrCompleted(santa) == true && arrCompleted(chosen) == true) {
- break;
- }
- }
- }
- }
- //runs test to see if all array is true and returns true
- //otherwise returns false.
- boolean arrCompleted(Boolean[] boolarray) {
- for (int i=0; i < boolarray.length; i++) {
- if (boolarray[i] == false) {
- return false;
- }
- }
- return true;
- }
- //send emails
- public void sendEmail(String to, String mass){
- Scanner keyboard = new Scanner(System.in);
- ArrayList<String> Emails = new ArrayList<String>();
- String userpt2 = "";String passpt2 = "";
- String total = "";String bodypt2 = "";
- String subjectpt2 = "";
- String wantToAttach = "";
- String pathToFile = "";
- String nameOfFile = "";
- 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");
- System.out.println("How many Emails would you like to send to? (one or multiple)");
- total = keyboard.nextLine();
- if(total.equals("one")){System.out.println("Enter your email (i.e. myemail@gmail.com)");
- userpt2 = keyboard.nextLine();
- System.out.println("Enter your email account's password (case-sensitive)");
- passpt2 = keyboard.nextLine();
- System.out.println("Enter the email's subject");
- subjectpt2 = keyboard.nextLine();
- System.out.println("Would you like to add an attachment? (yes or no)");
- wantToAttach = keyboard.nextLine();
- if(wantToAttach.equals("yes")){
- System.out.println("Enter the file path to your attachment, without including the file name. (i.e. C://Users/person/Desktop/Pictures/)");
- pathToFile = keyboard.nextLine();
- System.out.println("Enter the name of your attachment, including its extention. (i.e. mycat.jpg)");
- nameOfFile = keyboard.nextLine();
- }
- bodypt2 = mass;
- }
- else if(total.equals("multiple")){String done = "no";
- String status = "yes";
- System.out.println("Enter your email (i.e. myemail@gmail.com)");
- userpt2 = keyboard.nextLine();System.out.println("Enter your email account's password (case-sensitive)");
- passpt2 = keyboard.nextLine();
- System.out.println("Enter the email's subject");
- subjectpt2 = keyboard.nextLine();System.out.println("Would you like to add an attachment? (yes or no)");
- wantToAttach = keyboard.nextLine();
- if(wantToAttach.equals("yes")){System.out.println("Enter the file path to your attachment, without including the file name. (i.e. C://Users/person/Desktop/Pictures/)");
- pathToFile = keyboard.nextLine();
- System.out.println("Enter the name of your attachment, including its extention. (i.e. mycat.jpg)");
- pathToFile = keyboard.nextLine();
- }
- System.out.println("Enter the email's body text");
- bodypt2 = keyboard.nextLine();
- System.out.println("Enter the recipient's email (i.e. youremail@gmail.com)");
- Emails.add(keyboard.nextLine());
- while(done.equals("no")){System.out.println("Enter the next recipient's email (i.e. youremail@gmail.com)");
- Emails.add(keyboard.nextLine());
- System.out.println("Would you like to add another email? (yes or no)");
- status = keyboard.nextLine();
- if(status.equals("no")){done = "yes";}}}
- final String username = userpt2;
- final String password = passpt2;
- final String subject = subjectpt2;
- final String body = bodypt2;
- final String pathToFilept2 = pathToFile;
- final String nameOfFilept2 = nameOfFile;
- final String wantToAttachpt2 = wantToAttach;
- Session session = Session.getInstance(props, new javax.mail.Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(username, password);} });
- try{Multipart multipart = new MimeMultipart();
- Message message = new MimeMessage(session);
- message.setFrom(new InternetAddress(username));
- message.setSubject(subject);
- BodyPart messageBodyPart = new MimeBodyPart();
- multipart.addBodyPart(messageBodyPart);
- message.setText(body);if(wantToAttachpt2.equals("yes")){
- String file = pathToFilept2;
- String fileName = nameOfFilept2;
- FileDataSource source = new FileDataSource(file);
- messageBodyPart.setDataHandler(new DataHandler(source));
- messageBodyPart.setFileName(fileName);
- multipart.addBodyPart(messageBodyPart);
- message.setContent(multipart);
- }
- for(int i = 0; i < 1 /*Emails.size()*/; i++)
- {
- message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
- System.out.println("sending...");
- Transport.send(message);
- System.out.printf("Email %d sent.\n", i + 1);
- }
- System.out.println("-------------\nOperation Complete.");
- }
- catch (MessagingException e)
- {
- throw new RuntimeException(e);}}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement