package astrology.findyourfate.com;

import javax.activation.DataHandler;
   
import javax.activation.DataSource;   
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;   
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;   
import javax.mail.Session;   
import javax.mail.Transport;   
import javax.mail.internet.InternetAddress;   
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;   
import javax.mail.internet.MimeMultipart;

import java.io.ByteArrayInputStream;   
import java.io.IOException;   
import java.io.InputStream;   
import java.io.OutputStream;   
import java.security.Security;   
import java.util.Properties;   
public class GMailSender extends javax.mail.Authenticator {   
    private String mailhost = "smtp.gmail.com"; 
    private Session session;
    boolean sessionDebug = true;
    private String format; 
    private String subject; 
    private String user;
    private String password;
    private String imagepath;

    static {   
        Security.addProvider(new com.provider.JSSEProvider());   
    }  
    public GMailSender(String user, String format, String subject) throws MessagingException   {   
    	 this.user = user;   
         this.format = format;  
         this.subject=subject;
      

         Properties props = new Properties();   
       
         props.setProperty("mail.host", mailhost);  
         props.setProperty("mail.transport.protocol.", "smtp");   
         props.put("mail.smtp.auth", "true");  
         props.put("mail.smtp.", "true");
         props.put("mail.smtp.port", "465");  
         props.put("mail.smtp.socketFactory.fallback", "false");
         props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");   
         props.put("mail.smtp.socketFactory.port", "465");       
         props.setProperty("mail.smtp.quitwait", "false");   

         session = Session.getDefaultInstance(props, null);  
         session.setDebug(sessionDebug);
          
         
         Message msg = new MimeMessage(session); 
      
         msg.setFrom(new InternetAddress(user));
      
         InternetAddress[] address = { new InternetAddress("analyst@findyourfate.com") };
         InternetAddress[] address2 = { new InternetAddress("support@findyourfate.com") };
         InternetAddress[] address3 = { new InternetAddress(user) };
         
         msg.setRecipients(Message.RecipientType.TO, address);
         msg.setRecipients(Message.RecipientType.CC, address2);
         msg.setRecipients(Message.RecipientType.BCC, address3);
        msg.setSubject(subject);
         msg.setContent(format,"text/html"); 
 
         //msg.setContent(message, "text/html");  
     	Transport transport = session.getTransport("smtp");
     	
        transport.connect(mailhost,"support@findyourfate.com","fyf@123$%");

         try {
             
             transport.sendMessage(msg, msg.getAllRecipients());
                   }
      
         catch (Exception err) {
          System.out.println("Error" + err.getMessage());
      
         }
         transport.close();
     }   

   public GMailSender(String user, String format, String subject, String imagepath) throws MessagingException   {   
        this.user = user;   
        this.format = format;  
        this.subject=subject;
        this.imagepath=imagepath;

        Properties props = new Properties();   
      
        props.setProperty("mail.host", mailhost);  
        props.setProperty("mail.transport.protocol.", "smtp");   
        props.put("mail.smtp.auth", "true");  
        props.put("mail.smtp.", "true");
        props.put("mail.smtp.port", "465");  
        props.put("mail.smtp.socketFactory.fallback", "false");
        props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");   
        props.put("mail.smtp.socketFactory.port", "465");       
        props.setProperty("mail.smtp.quitwait", "false");   

        session = Session.getDefaultInstance(props, null);  
        session.setDebug(sessionDebug);
       
        Message msg = new MimeMessage(session); 
      msg.setFrom(new InternetAddress(user));
      InternetAddress[] address = { new InternetAddress("analyst@findyourfate.com") };
        InternetAddress[] address2 = { new InternetAddress("support@findyourfate.com") };
        InternetAddress[] address3 = { new InternetAddress(user) };
        msg.setRecipients(Message.RecipientType.TO, address);
        msg.setRecipients(Message.RecipientType.CC, address2);
        msg.setRecipients(Message.RecipientType.BCC, address3);
       msg.setSubject(subject); 
       // msg.setContent(format,"text/html"); 
        
        BodyPart messageBodyPart = new MimeBodyPart(); 
        messageBodyPart.setContent(format,"text/html"); 
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);
   
        messageBodyPart = new MimeBodyPart();
        String fileName = imagepath; 
        DataSource source = new FileDataSource(fileName);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(fileName);
        multipart.addBodyPart(messageBodyPart);
        msg.setContent(multipart,"text/html"); 
     
    	Transport transport = session.getTransport("smtp");
    	transport.connect(mailhost,"support@findyourfate.com","fyf@123$%");
      
        try {
     
            transport.sendMessage(msg, msg.getAllRecipients());
           // out.println("sent");
            //WasEmailSent = true; // assume it was sent
     
        }
     
        catch (Exception err) {
     
            //WasEmailSent = false; // assume it's a fail
     
           System.out.println("Error" + err.getMessage());
     
        }
        transport.close();
     
    }   

	protected PasswordAuthentication getPasswordAuthentication() {   
        return new PasswordAuthentication(user, password);   
    }   

    public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {   
        try{
        MimeMessage message = new MimeMessage(session);   
        DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));   
        message.setSender(new InternetAddress(sender));   
        message.setSubject(subject);   
        message.setDataHandler(handler);   
        if (recipients.indexOf(',') > 0)   
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));   
        else  
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));   
        Transport.send(message);   
        }catch(Exception e){

        }
    }   

    public class ByteArrayDataSource implements DataSource {   
        private byte[] data;   
        private String type;   

        public ByteArrayDataSource(byte[] data, String type) {   
            super();   
            this.data = data;   
            this.type = type;   
        }   

        public ByteArrayDataSource(byte[] data) {   
            super();   
            this.data = data;   
        }   

        public void setType(String type) {   
            this.type = type;   
        }   

        public String getContentType() {   
            if (type == null)   
                return "application/octet-stream";   
            else  
                return type;   
        }   

        public InputStream getInputStream() throws IOException {   
            return new ByteArrayInputStream(data);   
        }   

        public String getName() {   
            return "ByteArrayDataSource";   
        }   

        public OutputStream getOutputStream() throws IOException {   
            throw new IOException("Not Supported");   
        }   
    }   
}  