Package com.sahuaro.mail

Source Code of com.sahuaro.mail.EmailSender

package com.sahuaro.mail;

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.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.sahuaro.domain.Contact;
import com.sahuaro.domain.Prospect;
import com.sahuaro.domain.Suggestion;

public class EmailSender<T> {
  private static final String ADMIN_EMAIL = "sahuarocontact@gmail.com";
  private static final String ADMIN_PASS = "sAh*ar0l1a3bsA_";
  private static final String ADMIN_NAME = "Contact - Sahuaro Labs";
  private static final String CONTACT_SUBJECT = "Sahuaro-Software request";
   
  public enum Types  {CONTACT, SUGGESTION};
  private T t;
 
  public EmailSender(T entity){
    this.t = entity;
  }
 
  public boolean sendEmail(String email, String messageText, Prospect prospect){
    Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {

            @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication("ADMIN_EMAIL", "ADMIN_PASS");
                    }
                });
        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(ADMIN_EMAIL));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(email));
            message.setSubject(CONTACT_SUBJECT);
            String content = getContent(messageText, prospect);
            message.setText(content);
            Transport.send(message);
        }catch(AddressException ex){
          return false;
        }catch(MessagingException ex){
          return false;
        }
        return true;
  }
 
  private String getContent(String message, Prospect prospect){
    StringBuilder sb = new StringBuilder();
    if(t instanceof Suggestion){
      sb.append(prospect.getFirstName() + " is trying to suggest you a service.\n");
      sb.append("The prospect info is show below.\n");
      sb.append("First name: " + prospect.getFirstName() + "\n");
      sb.append("Last name: " + prospect.getLastName() + "\n");
      sb.append("Email: " + prospect.getEmail() + "\n");
      sb.append("Message:\n " + message + "\n");
     
    }else if(t instanceof Contact){
      Contact contact = (Contact) t;
      sb.append(prospect.getFirstName() + " is trying to contact you.\n");
      sb.append("The prospect info is show below.\n");
      sb.append("First name: " + prospect.getFirstName() + "\n");
      sb.append("Last name: " + prospect.getLastName() + "\n");
      sb.append("Email: " + prospect.getEmail() + "\n");
      sb.append("Company: " + contact.getCompany() + "\n");
      sb.append("Telephone: " + contact.getTelephone() + "\n");
      sb.append("Message:\n " + message + "\n");
     
    }
    return sb.toString();
  }
}
TOP

Related Classes of com.sahuaro.mail.EmailSender

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.