Advertisement
Guest User

Secret Santa

a guest
Nov 28th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.64 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import javax.mail.*;
  4. import javax.mail.internet.*;
  5. import javax.mail.Message;
  6. import javax.mail.Session;
  7. import javax.mail.Transport;
  8. import javax.mail.internet.InternetAddress;
  9. import javax.mail.internet.MimeMessage;
  10. import javax.sql.*;
  11. import javax.activation.*;
  12. public class SecretSanta {
  13.  
  14. //Set 2 boolean arrays
  15. Boolean[] santa;
  16. Boolean[] chosen;
  17. boolean santaPaired = true;
  18. boolean chosenPaired = true;
  19.  
  20.  
  21. public static void main(String[] args) {
  22.  
  23. //For the main method, just use the fileread method in order to pair the santas/chosens
  24. SecretSanta ss = new SecretSanta();
  25. ss.fileRead();
  26. }
  27.  
  28. public void fileRead() {
  29.  
  30. List<String> aList = new ArrayList<String>();
  31.  
  32. int size = 0;
  33.  
  34. try {
  35.  
  36. File emails = new File("/Users/Refai343643/Desktop/emails.txt");
  37. BufferedReader br = new BufferedReader(new FileReader(emails));
  38.  
  39. //create a blank/null string in order to read the upcoming file lines
  40. String line = null;
  41.  
  42. while((line = br.readLine()) != null) {
  43. size = size +1;
  44. aList.add(line);
  45. }
  46. br.close();
  47. }
  48.  
  49. catch(Exception ex) {
  50. ex.printStackTrace();
  51. //If there's an error then catch it here
  52. }
  53.  
  54. //now create 2 more different boolean arrays and fill them all with false
  55. chosen = new Boolean[size];
  56. santa = new Boolean[size];
  57. Arrays.fill(chosen, false);
  58. Arrays.fill(santa, false);
  59.  
  60.  
  61. //Assign random numbers to a integer in order to grab a random pair and assign them to p1 and p2
  62. Random rand = new Random();
  63. while(true) {
  64. int randomInt1 = rand.nextInt(size);
  65. int randomInt2 = rand.nextInt(size);
  66.  
  67. String p1 = aList.get(randomInt1);
  68. String p2 = aList.get(randomInt2);
  69.  
  70. if(p1==p2){
  71. //do nothing because you can't get yourself as a pair, try again.
  72. }
  73. if((p1!=p2) && (santa[randomInt1] == false) && (chosen[randomInt2] == false)) {
  74.  
  75. String[] person1 = p1.split(" ");
  76. String[] person2 = p2.split(" ");
  77.  
  78. String message = person1[0] + ", you are " + person2[0] + " " + "'s secret Santa!";
  79. System.out.println(message); //show what message is being sent
  80. sendEmail(person1[0], message);
  81. //use the send email method to send the emails from these messages
  82. santa[randomInt1] = true;
  83. chosen[randomInt2] = true;
  84.  
  85. if(arrCompleted(santa) == true && arrCompleted(chosen) == true) {
  86. break;
  87.  
  88. }
  89. }
  90. }
  91. }
  92.  
  93. //runs test to see if all array is true and returns true
  94. //otherwise returns false.
  95. boolean arrCompleted(Boolean[] boolarray) {
  96. for (int i=0; i < boolarray.length; i++) {
  97. if (boolarray[i] == false) {
  98. return false;
  99. }
  100. }
  101. return true;
  102. }
  103. //send emails
  104. public void sendEmail(String to, String mass){
  105. Scanner keyboard = new Scanner(System.in);
  106. ArrayList<String> Emails = new ArrayList<String>();
  107. String userpt2 = "";String passpt2 = "";
  108. String total = "";String bodypt2 = "";
  109. String subjectpt2 = "";
  110. String wantToAttach = "";
  111. String pathToFile = "";
  112. String nameOfFile = "";
  113. Properties props = new Properties();
  114. props.put("mail.smtp.auth", "true");
  115. props.put("mail.smtp.starttls.enable", "true");
  116. props.put("mail.smtp.host", "smtp.gmail.com");
  117. props.put("mail.smtp.port", "587");
  118. System.out.println("How many Emails would you like to send to? (one or multiple)");
  119. total = keyboard.nextLine();
  120. if(total.equals("one")){System.out.println("Enter your email (i.e. myemail@gmail.com)");
  121. userpt2 = keyboard.nextLine();
  122. System.out.println("Enter your email account's password (case-sensitive)");
  123. passpt2 = keyboard.nextLine();
  124. System.out.println("Enter the email's subject");
  125. subjectpt2 = keyboard.nextLine();
  126. System.out.println("Would you like to add an attachment? (yes or no)");
  127. wantToAttach = keyboard.nextLine();
  128. if(wantToAttach.equals("yes")){
  129. System.out.println("Enter the file path to your attachment, without including the file name. (i.e. C://Users/person/Desktop/Pictures/)");
  130. pathToFile = keyboard.nextLine();
  131. System.out.println("Enter the name of your attachment, including its extention. (i.e. mycat.jpg)");
  132. nameOfFile = keyboard.nextLine();
  133. }
  134. bodypt2 = mass;
  135. }
  136. else if(total.equals("multiple")){String done = "no";
  137. String status = "yes";
  138. System.out.println("Enter your email (i.e. myemail@gmail.com)");
  139. userpt2 = keyboard.nextLine();System.out.println("Enter your email account's password (case-sensitive)");
  140. passpt2 = keyboard.nextLine();
  141. System.out.println("Enter the email's subject");
  142. subjectpt2 = keyboard.nextLine();System.out.println("Would you like to add an attachment? (yes or no)");
  143. wantToAttach = keyboard.nextLine();
  144. 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/)");
  145. pathToFile = keyboard.nextLine();
  146. System.out.println("Enter the name of your attachment, including its extention. (i.e. mycat.jpg)");
  147. pathToFile = keyboard.nextLine();
  148. }
  149. System.out.println("Enter the email's body text");
  150. bodypt2 = keyboard.nextLine();
  151. System.out.println("Enter the recipient's email (i.e. youremail@gmail.com)");
  152. Emails.add(keyboard.nextLine());
  153. while(done.equals("no")){System.out.println("Enter the next recipient's email (i.e. youremail@gmail.com)");
  154. Emails.add(keyboard.nextLine());
  155. System.out.println("Would you like to add another email? (yes or no)");
  156. status = keyboard.nextLine();
  157. if(status.equals("no")){done = "yes";}}}
  158. final String username = userpt2;
  159. final String password = passpt2;
  160. final String subject = subjectpt2;
  161. final String body = bodypt2;
  162. final String pathToFilept2 = pathToFile;
  163. final String nameOfFilept2 = nameOfFile;
  164. final String wantToAttachpt2 = wantToAttach;
  165. Session session = Session.getInstance(props, new javax.mail.Authenticator() {
  166. protected PasswordAuthentication getPasswordAuthentication() {
  167. return new PasswordAuthentication(username, password);} });
  168. try{Multipart multipart = new MimeMultipart();
  169. Message message = new MimeMessage(session);
  170. message.setFrom(new InternetAddress(username));
  171. message.setSubject(subject);
  172. BodyPart messageBodyPart = new MimeBodyPart();
  173. multipart.addBodyPart(messageBodyPart);
  174. message.setText(body);if(wantToAttachpt2.equals("yes")){
  175. String file = pathToFilept2;
  176. String fileName = nameOfFilept2;
  177. FileDataSource source = new FileDataSource(file);
  178. messageBodyPart.setDataHandler(new DataHandler(source));
  179. messageBodyPart.setFileName(fileName);
  180. multipart.addBodyPart(messageBodyPart);
  181. message.setContent(multipart);
  182. }
  183. for(int i = 0; i < 1 /*Emails.size()*/; i++)
  184. {
  185. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
  186. System.out.println("sending...");
  187. Transport.send(message);
  188. System.out.printf("Email %d sent.\n", i + 1);
  189. }
  190. System.out.println("-------------\nOperation Complete.");
  191. }
  192. catch (MessagingException e)
  193. {
  194. throw new RuntimeException(e);}}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement