/Users/johnr/Desktop/IA pdf Downloads/Criteria__P__-_Coding_Project_Upload_all_2022-05-03/IdanIA/src/idania/Objects/Email.java
  1 /*
  2  * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
  3  * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
  4  */
  5 package idania;
  6 
  7 /**
  8  *
  9  * @author Idan
 10  */
 11 import javax.mail.*;
 12 import javax.mail.internet.InternetAddress;
 13 import javax.mail.internet.MimeMessage;
 14 import java.util.Properties;
 15 
 16 public class Email {
 17     private String [] recipient;
 18     private String subject;
 19     private String body;
 20 
 21     public Email(String[] recipient, String subject, String body) {
 22         this.recipient = recipient;
 23         this.subject = subject;
 24         this.body = body;
 25     }
 26 
 27     public String[] getRecipient() {
 28         return recipient;
 29     }
 30 
 31     public void setRecipient(String[] recipient) {
 32         this.recipient = recipient;
 33     }
 34 
 35     public String getSubject() {
 36         return subject;
 37     }
 38 
 39     public void setSubject(String subject) {
 40         this.subject = subject;
 41     }
 42 
 43     public String getBody() {
 44         return body;
 45     }
 46 
 47     public void setBody(String body) {
 48         this.body = body;
 49     }
 50 
 51     private Session getSession() {
 52         String from = "17142@students.isb.ac.th";
 53         String host = "smtp.gmail.com";
 54 
 55         // Set Email Properties
 56         Properties properties = System.getProperties();
 57         properties.put("mail.smtp.host", host);
 58         properties.put("mail.smtp.port", "465");
 59         properties.put("mail.smtp.ssl.enable", "true");
 60         properties.put("mail.smtp.auth", "true");
 61 
 62         Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
 63 
 64             protected PasswordAuthentication getPasswordAuthentication() {
 65 
 66                 return new PasswordAuthentication(from, "28Idanfcb04");
 67 
 68             }
 69 
 70         });
 71 
 72         return session;
 73     }
 74 
 75     public void sendEmail() {
 76         Session session = this.getSession();
 77         String from = "17142@students.isb.ac.th";
 78 
 79         // Create a MimeMessage object.
 80         MimeMessage message = new MimeMessage(session);
 81 
 82         try {
 83 
 84             // Set the sender email address
 85             message.setFrom(new InternetAddress(from));
 86 
 87             // Adding recipients into message object
 88             for (int i = 0; i < this.getRecipient().length; i++) {
 89                 String to = this.getRecipient()[i];
 90                 message.addRecipient(Message.RecipientType.CC, new InternetAddress(to));
 91             }
 92 
 93             // Set Subject: header field
 94             message.setSubject(this.getSubject());
 95 
 96             // Now set the actual message
 97             message.setText(this.getBody());
 98 
 99             System.out.println("sending...");
100 
101             // Send message
102             Transport.send(message);
103 
104             System.out.println("Sent message successfully....");
105         } catch (MessagingException mex) {
106             mex.printStackTrace();
107         }
108     }
109 }
110