package Email;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import java.io.*;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Multipart;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;

public class emailSender {

    public static final HashMap<Integer, ArrayList<MessageToSend>> messagesToSend = new HashMap<>();
    //maps the priority levels to the corresponding emails to be sent - within each priority level

    public static Session objSession;

    private static final String strEmail = "cz3003timecrisis@gmail.com";
    private static final String strPwd = "3003liuyang";

    private static final int intMaxPriority = 3;

    static {
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.starttls.enable", "true");

        objSession = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(strEmail, strPwd);
                    }
                });

        for (int i = 1; i <= intMaxPriority; i++) {
            messagesToSend.put(i, new ArrayList<>());
        }
    }

    public static void getEmailsToBeSent(JSONArray jsonArrayOfMails) throws IOException, ParseException {

        for (Object jsonArrayOfMail : jsonArrayOfMails) {
            JSONObject jsonObject = (JSONObject) jsonArrayOfMail;

            String from = jsonObject.get("from").toString();
            String recipients = jsonObject.get("recipients").toString();
            String subject = jsonObject.get("subject").toString();
            String messageText = jsonObject.get("messageText").toString();
            int priority = Integer.parseInt(jsonObject.get("priority").toString());

            MessageToSend m = new MessageToSend(from, recipients, subject, messageText, priority);
            messagesToSend.get(priority).add(m);
        }

//        System.out.println(messagesToSend);
    }

    public static void dispatchEmail() {

        for (Integer intKey : messagesToSend.keySet()) {
            for (int j = 0; j < messagesToSend.get(intKey).size(); j++) {

                synchronized (messagesToSend) {
                    MessageToSend m = messagesToSend.get(intKey).get(j);
                    String from = m.from;
                    String recipients = m.recipients;
                    String subject = m.subject;
                    String messageText = m.messageText;
                    int priority = m.priority;
                    try {
                        sendEmail(from, recipients, subject, messageText, priority);

                        messagesToSend.get(intKey).remove(j);
                    } catch (MessagingException ex) {
                    }
                }

            }
        }

    }

    public static void sendEmail(String from, String recipients, String subject, String messageText, int priority) throws MessagingException {

        Message message = new MimeMessage(objSession);
        message.setFrom(new InternetAddress(from));

        //Need to test the line below for multiple recipients:
        //InternetAddress[] toAddresses={new InternetAddress(strRecipient)};
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(recipients));

        message.setSubject(subject);
        message.setText(messageText);
        
        MimeBodyPart messageBodyPart = new MimeBodyPart();

        Multipart multipart = new MimeMultipart();

        messageBodyPart = new MimeBodyPart();
        String file = "path of file to be attached";
        String fileName = "report.pdf";
        DataSource source = new FileDataSource(messageText);
        
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(fileName);
        
        multipart.addBodyPart(messageBodyPart);

        message.setContent(multipart);

        Transport.send(message);

    }

}

@@ -374,16 +374,45 @@
 .io.*;%0D%0A
+import java.nio.file.Paths;%0D%0A
 import j
@@ -4223,32 +4223,34 @@
 sageText);%0D%0A
+%0D%0A
         
 %0D%0A        Mi
@@ -4229,34 +4229,73 @@
 xt);%0D%0A%0D%0A        
-%0D%0A
+if (messageText.endsWith(%22.pdf%22)) %7B%0D%0A    
         MimeBody
@@ -4348,16 +4348,20 @@
         
+    
 Multipar
@@ -4398,32 +4398,36 @@
 t();%0D%0A%0D%0A        
+   
+ 
 messageBodyPart 
@@ -4445,24 +4445,28 @@
 odyPart();%0D%0A
+    
         Stri
@@ -4507,32 +4507,36 @@
 ched%22;%0D%0A        
+ 
+   
 String fileName 
@@ -4560,16 +4560,20 @@
         
+   
+ 
 DataSour
@@ -4614,34 +4614,30 @@
 sageText);%0D%0A
-    
+%0D%0A
     
-%0D%0A
         mess
@@ -4690,32 +4690,36 @@
 rce));%0D%0A        
+   
+ 
 messageBodyPart.
@@ -4746,18 +4746,14 @@
 );%0D%0A
-    
+%0D%0A
     
-%0D%0A
     
@@ -4799,32 +4799,36 @@
 rt);%0D%0A%0D%0A        
+   
+ 
 message.setConte
@@ -4838,24 +4838,35 @@
 multipart);%0D
+%0A        %7D%0D
 %0A%0D%0A        T

Send Email with Attachments