|
| 1 | +/* |
| 2 | + * A Mass Mailer Java Application using Jakarta Mail API. |
| 3 | + * The purpose of this program is to enable the user to send out bulk emails. |
| 4 | + * |
| 5 | + * Download the Jakarta Mail and the Jakarta Activation JAR Files and configure in your repository from: |
| 6 | + * https://eclipse-ee4j.github.io/mail/#Download_Jakarta_Mail_Release |
| 7 | + * https://mvnrepository.com/artifact/com.sun.activation/jakarta.activation/2.0.1 |
| 8 | + * |
| 9 | + * |
| 10 | + * The recipients.txt found in src\resources\ folder is where the recipient emails are stored (separated by a ',(comma)'). |
| 11 | + * The user can manually add new recipients in the recipient.txt file from within the program too. |
| 12 | + * |
| 13 | + * The content.txt found in src\resources\ folder is where the text written in the email is stored. |
| 14 | + * The user can override the text from within the program. |
| 15 | + * |
| 16 | + * The user is required to add a Subject for the Email(s). |
| 17 | + * |
| 18 | + * The user can choose if he wants to add CC, BCC or Attachments to his email(s). This is Optional. |
| 19 | + * |
| 20 | + * The user then needs to enter his Email ID, from which he wishes to send the mail(s). |
| 21 | + * |
| 22 | + * Once the user email is entered, the user has a choice to send each mail individually or to send 1 mail to all of the recipients. |
| 23 | + * If the user chooses the 1 mail(all) option, he can futher choose if he wants to hide the recipient emails while sending |
| 24 | + * (this is done by adding all emails to BCC which in turn hides the emails from the end user - the reciever). |
| 25 | + * |
| 26 | + * |
| 27 | + * This Program is set up for the Fake SMTP Server, which can be downloaded at: |
| 28 | + * http://nilhcem.com/FakeSMTP/download.html |
| 29 | + * |
| 30 | + * Once the Fake SMTP Server is downloaded, start the server at 8080 Port, |
| 31 | + * if you want to use a different port go to src\resouces\fake_smtp.properties |
| 32 | + * and change the 'mail.smtp.port' to any port of your choice. |
| 33 | + * |
| 34 | + * |
| 35 | + * @author - 04xRaynal |
| 36 | + */ |
| 37 | +package raynal.mass_mailer; |
| 38 | + |
| 39 | +import java.io.BufferedInputStream; |
| 40 | +import java.io.File; |
| 41 | +import java.io.FileInputStream; |
| 42 | +import java.io.FileNotFoundException; |
| 43 | +import java.io.FileWriter; |
| 44 | +import java.io.IOException; |
| 45 | +import java.util.ArrayList; |
| 46 | +import java.util.Date; |
| 47 | +import java.util.List; |
| 48 | +import java.util.Properties; |
| 49 | +import java.util.Scanner; |
| 50 | + |
| 51 | +import javax.swing.JFileChooser; |
| 52 | +import javax.swing.UIManager; |
| 53 | +import javax.swing.UnsupportedLookAndFeelException; |
| 54 | + |
| 55 | +import jakarta.mail.Message; |
| 56 | +import jakarta.mail.MessagingException; |
| 57 | +import jakarta.mail.Multipart; |
| 58 | +import jakarta.mail.Session; |
| 59 | +import jakarta.mail.Transport; |
| 60 | +import jakarta.mail.internet.AddressException; |
| 61 | +import jakarta.mail.internet.InternetAddress; |
| 62 | +import jakarta.mail.internet.MimeBodyPart; |
| 63 | +import jakarta.mail.internet.MimeMessage; |
| 64 | +import jakarta.mail.internet.MimeMultipart; |
| 65 | + |
| 66 | +public class MassMailer_FakeSmtp { |
| 67 | + Properties properties; |
| 68 | + Session session; |
| 69 | + Message message; |
| 70 | + private ArrayList<File> files; |
| 71 | + |
| 72 | + public MassMailer_FakeSmtp() { |
| 73 | + try { |
| 74 | + properties = System.getProperties(); |
| 75 | + FileInputStream fis = new FileInputStream("src\\resources\\fake_smtp.properties"); |
| 76 | + properties.load(fis); |
| 77 | + fis.close(); |
| 78 | + |
| 79 | + session = Session.getDefaultInstance(properties); |
| 80 | + |
| 81 | + files = new ArrayList<>(); //List to store the Attachment Files if added |
| 82 | + List<String> recipientEmails = new ArrayList<>(); //List to store the recipient emails |
| 83 | + List<String> ccEmailsList = new ArrayList<>(); //List to store the CC Emails if added |
| 84 | + List<String> bccEmailsList = new ArrayList<>(); //List to store the BCC Emails if added |
| 85 | + |
| 86 | + try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } |
| 87 | + catch (ClassNotFoundException e) {} |
| 88 | + catch (InstantiationException e) {} |
| 89 | + catch (IllegalAccessException e) {} |
| 90 | + catch (UnsupportedLookAndFeelException e) {} //Refines the look of the ui (For the JFileChooser) |
| 91 | + |
| 92 | + final JFileChooser fileChooser = new javax.swing.JFileChooser(); |
| 93 | + fileChooser.setCurrentDirectory(new File(".")); |
| 94 | + int sentCount = 0; |
| 95 | + |
| 96 | + System.out.println("Email Recipients are taken from recipients.txt file found in the src\\resources\\ folder.\n" |
| 97 | + + "You can also manually add recipient emails in the recipients.txt file."); |
| 98 | + |
| 99 | + System.out.println("Do you want to manually enter recipient emails?\n" |
| 100 | + + "Press Y for Yes or N for No"); |
| 101 | + Scanner sc = new Scanner(System.in); |
| 102 | + String manualEnter = sc.nextLine(); |
| 103 | + |
| 104 | + switch(manualEnter) { |
| 105 | + case ("Y"): |
| 106 | + case ("y"): |
| 107 | + String manualInput = null; |
| 108 | + |
| 109 | + do { |
| 110 | + System.out.println("Enter a recipient email, enter 'STOP' to stop:"); |
| 111 | + manualInput = sc.nextLine(); |
| 112 | + recipientEmails.add(manualInput); |
| 113 | + } while(!manualInput.toLowerCase().equals("stop")); |
| 114 | + |
| 115 | + recipientEmails.remove(recipientEmails.size() - 1); //"stop" is removed from the list |
| 116 | + |
| 117 | + FileWriter fw = new FileWriter(new File("src\\resources\\recipients.txt"), true); //true denotes [append - true] |
| 118 | + for(String email: recipientEmails) { |
| 119 | + fw.write(email); |
| 120 | + fw.write(","); |
| 121 | + } |
| 122 | + fw.flush(); |
| 123 | + fw.close(); |
| 124 | + break; |
| 125 | + |
| 126 | + case ("N"): |
| 127 | + case ("n"): |
| 128 | + StringBuilder sbEmail = new StringBuilder(); |
| 129 | + BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("src\\resources\\recipients.txt"))); |
| 130 | + int readByte; |
| 131 | + while((readByte = bis.read()) != -1) { |
| 132 | + sbEmail.append((char)readByte); |
| 133 | + } |
| 134 | + bis.close(); |
| 135 | + |
| 136 | + String[] emailArray = sbEmail.toString().split(","); //Values from the StringBuilder are stored in a String, with commas removed |
| 137 | + for(String email: emailArray) |
| 138 | + recipientEmails.add(email); |
| 139 | + break; |
| 140 | + |
| 141 | + default: System.out.println("Invalid Input! Program terminated."); |
| 142 | + } |
| 143 | + |
| 144 | + System.out.println("Enter a Subject for your Mail:"); |
| 145 | + String subject = sc.nextLine(); |
| 146 | + |
| 147 | + System.out.println("Press 'C' to use content.txt or 'X' to input text"); |
| 148 | + String contentInput = sc.nextLine(); |
| 149 | + StringBuilder contentSB = new StringBuilder(); |
| 150 | + |
| 151 | + switch(contentInput) { |
| 152 | + case ("C"): |
| 153 | + case ("c"): |
| 154 | + BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("src\\resources\\content.txt"))); |
| 155 | + int contentRead; |
| 156 | + while((contentRead = bis.read()) != -1) { |
| 157 | + contentSB.append((char)contentRead); |
| 158 | + } |
| 159 | + break; |
| 160 | + |
| 161 | + case ("X"): |
| 162 | + case ("x"): |
| 163 | + default: |
| 164 | + String inputText = null; |
| 165 | + do { |
| 166 | + System.out.println("Enter Line of Text, input 'STOP' to stop:"); |
| 167 | + inputText = sc.nextLine(); |
| 168 | + contentSB.append(inputText + "\n"); |
| 169 | + } while(! (inputText.toLowerCase().equals("stop"))); |
| 170 | + |
| 171 | + contentSB.delete(contentSB.length() - 5, contentSB.length()); //deleting (length - 5) from the end to remove "stop\n" |
| 172 | + } |
| 173 | + |
| 174 | + System.out.println("Do you want to add CC, BCC or add File Attachments?\n" |
| 175 | + + "Input 'Y' for Yes or 'N' for No"); |
| 176 | + String attachmentInput = sc.nextLine(); |
| 177 | + |
| 178 | + if(attachmentInput.toLowerCase().equals("y")) { |
| 179 | + System.out.println("Do you want to add emails in CC ?\n" |
| 180 | + + "Press 'Y for Yes or 'N' for No."); |
| 181 | + String ccInput = sc.nextLine(); |
| 182 | + String ccInputEmail = ""; |
| 183 | + if(ccInput.equals("Y") || ccInput.equals("y")) { |
| 184 | + do { |
| 185 | + System.out.println("Input an email for CC, Enter STOP to stop"); |
| 186 | + ccInputEmail = sc.nextLine(); |
| 187 | + ccEmailsList.add(ccInputEmail); |
| 188 | + }while(! ccInputEmail.toLowerCase().equals("stop")); |
| 189 | + |
| 190 | + ccEmailsList.remove(ccEmailsList.size() - 1); //removing "stop" from the list |
| 191 | + } |
| 192 | + |
| 193 | + |
| 194 | + System.out.println("Do you want to add emails in BCC ?\n" |
| 195 | + + "Press 'Y for Yes or 'N' for No."); |
| 196 | + String bccInput = sc.nextLine(); |
| 197 | + String bccInputEmail = ""; |
| 198 | + if(bccInput.equals("Y") || bccInput.equals("y")) { |
| 199 | + do { |
| 200 | + System.out.println("Input an email for BCC, Enter STOP to stop"); |
| 201 | + bccInputEmail = sc.nextLine(); |
| 202 | + bccEmailsList.add(bccInputEmail); |
| 203 | + }while(! bccInputEmail.toLowerCase().equals("stop")); |
| 204 | + |
| 205 | + bccEmailsList.remove(bccEmailsList.size() - 1); //removing "stop" from the list |
| 206 | + } |
| 207 | + |
| 208 | + String attachInput; |
| 209 | + do { |
| 210 | + System.out.println("Do you want to add File Attachments?\n" |
| 211 | + + "Press 'Y for Yes or 'N' for No."); |
| 212 | + attachInput = sc.nextLine(); |
| 213 | + if(attachInput.equals("Y") || attachInput.equals("y")) { |
| 214 | + int fcReturnValue = fileChooser.showOpenDialog(null); |
| 215 | + if(fcReturnValue == JFileChooser.APPROVE_OPTION) { |
| 216 | + files.add(fileChooser.getSelectedFile()); |
| 217 | + } |
| 218 | + } |
| 219 | + } while(! attachInput.toLowerCase().equals("n")); |
| 220 | + } |
| 221 | + |
| 222 | + System.out.println("Enter your email address:"); |
| 223 | + String from = sc.nextLine(); |
| 224 | + |
| 225 | + message = new MimeMessage(session); |
| 226 | + try { |
| 227 | + message.setFrom(new InternetAddress(from)); |
| 228 | + |
| 229 | + message.setSubject(subject); |
| 230 | + message.setSentDate(new Date()); |
| 231 | + |
| 232 | + //If CC Emails were added above, adding them in mail |
| 233 | + if(ccEmailsList.size() > 0) { |
| 234 | + StringBuilder sbCcEmails = new StringBuilder(); |
| 235 | + for(String ccEmail: ccEmailsList) |
| 236 | + sbCcEmails.append(ccEmail + ","); |
| 237 | + |
| 238 | + message.addRecipients(Message.RecipientType.CC, InternetAddress.parse(sbCcEmails.toString())); |
| 239 | + } |
| 240 | + |
| 241 | + //If BCC Emails were added above, adding them in mail |
| 242 | + if(bccEmailsList.size() > 0) { |
| 243 | + StringBuilder sbBccEmails = new StringBuilder(); |
| 244 | + for(String bccEmail: bccEmailsList) |
| 245 | + sbBccEmails.append(bccEmail + ","); |
| 246 | + |
| 247 | + message.addRecipients(Message.RecipientType.BCC, InternetAddress.parse(sbBccEmails.toString())); |
| 248 | + } |
| 249 | + |
| 250 | + |
| 251 | + MimeBodyPart messageBodyPart = new MimeBodyPart(); |
| 252 | + Multipart multipart = new MimeMultipart(); |
| 253 | + |
| 254 | + messageBodyPart.setContent(contentSB.toString(),"text/plain"); |
| 255 | + multipart.addBodyPart(messageBodyPart); |
| 256 | + |
| 257 | + //If Attachments were added above, adding them in mail |
| 258 | + if(files.size() > 0) { |
| 259 | + for(File file: files) { |
| 260 | + MimeBodyPart messageBodyPart_file = new MimeBodyPart(); |
| 261 | + messageBodyPart_file.attachFile(file); |
| 262 | + multipart.addBodyPart(messageBodyPart_file); |
| 263 | + } |
| 264 | + } |
| 265 | + |
| 266 | + String sendPref; |
| 267 | + do { |
| 268 | + System.out.println("Enter 'IND' to mail all recipients individually.\n" |
| 269 | + + "Or enter 'ALL' to include All recipients in a single mail."); |
| 270 | + sendPref = sc.nextLine(); |
| 271 | + |
| 272 | + } while(! (sendPref.toLowerCase().equals("all") || sendPref.toLowerCase().equals("ind"))); |
| 273 | + |
| 274 | + long startTime = System.currentTimeMillis(); |
| 275 | + |
| 276 | + switch(sendPref.toLowerCase()) { |
| 277 | + case ("ind"): |
| 278 | + //Sending emails individually |
| 279 | + for(String recipientEmail: recipientEmails) { |
| 280 | + message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipientEmail)); |
| 281 | + |
| 282 | + message.setContent(multipart); |
| 283 | + Transport.send(message); |
| 284 | + sentCount++; |
| 285 | + } |
| 286 | + break; |
| 287 | + |
| 288 | + case ("all"): |
| 289 | + StringBuilder sbEmailsList = new StringBuilder(); |
| 290 | + for(String recipientEmail: recipientEmails) |
| 291 | + sbEmailsList.append(recipientEmail + ","); |
| 292 | + |
| 293 | + System.out.println("Do you want to hide the recipients Email Address?\n" |
| 294 | + + "Enter 'Y' for YES or 'N' for NO"); |
| 295 | + String allPref = sc.nextLine(); |
| 296 | + |
| 297 | + if(allPref.toLowerCase().equals("y")) { |
| 298 | + message.addRecipients(Message.RecipientType.BCC, InternetAddress.parse(sbEmailsList.toString())); //with BCC recipients name gets hidden |
| 299 | + } |
| 300 | + else { |
| 301 | + message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(sbEmailsList.toString())); |
| 302 | + } |
| 303 | + |
| 304 | + message.setContent(multipart); |
| 305 | + Transport.send(message); |
| 306 | + sentCount++; |
| 307 | + break; |
| 308 | + |
| 309 | + default: System.out.println("Invalid Input! Enter Again."); |
| 310 | + } |
| 311 | + |
| 312 | + long endTime = System.currentTimeMillis(); |
| 313 | + System.out.println("Total Time taken to send mails: " + (endTime-startTime)/1000 + "seconds"); |
| 314 | + |
| 315 | + } |
| 316 | + catch(AddressException ex) { |
| 317 | + ex.printStackTrace(); |
| 318 | + } |
| 319 | + catch(MessagingException ex) { |
| 320 | + ex.printStackTrace(); |
| 321 | + } |
| 322 | + finally { |
| 323 | + sc.close(); |
| 324 | + } |
| 325 | + |
| 326 | + System.out.println("Amount of E-mails sent: " + sentCount); |
| 327 | + |
| 328 | + } |
| 329 | + catch (FileNotFoundException ex) { |
| 330 | + ex.printStackTrace(); |
| 331 | + } |
| 332 | + catch (IOException ex) { |
| 333 | + ex.printStackTrace(); |
| 334 | + } |
| 335 | + } |
| 336 | + |
| 337 | + public static void main(String[] args) { |
| 338 | + new MassMailer_FakeSmtp(); |
| 339 | + } |
| 340 | + |
| 341 | +} |
0 commit comments