Package at.fhj.itm.util

Source Code of at.fhj.itm.util.EmailUtilImpl

package at.fhj.itm.util;

import java.io.UnsupportedEncodingException;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.faces.application.Application;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
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 org.apache.log4j.Logger;

/**
* Class for sending emails to the users.
* @author Hans-Joerg Tagger<br />
*   Patrick Habith
* @version 0.1 - last change Jan. 24, 2011
*/
public class EmailUtilImpl implements EmailUtil
{
    private final Logger logger = Logger.getLogger(EmailUtilImpl.class.getName());
   
    private static final String FROM_ADDRESS = "ebusmod@gmail.com";
    private static final String FROM_PERSONAL = "DriveTogether";
    private static final String MAIL_USER = "ebusmod";
    private static final String MAIL_PWD = "itm08itm08";
    private final Properties props;
   
    public EmailUtilImpl()
    {
  this.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");
    }

    @Override
    public boolean sendRegConfirmation(String email, String username)
    {
  UIViewRoot view = FacesContext.getCurrentInstance().getViewRoot();
  Application app = FacesContext.getCurrentInstance().getApplication();
  ResourceBundle rb = ResourceBundle.getBundle(app.getMessageBundle(), view.getLocale());
  try
  {
      InternetAddress fromAddr = new InternetAddress(FROM_ADDRESS, FROM_PERSONAL);
      InternetAddress toAddr = new InternetAddress(email, username);

      Session session = Session.getDefaultInstance(this.props,
        new javax.mail.Authenticator()
        {
      protected PasswordAuthentication getPasswordAuthentication()
      {
          return new PasswordAuthentication(MAIL_USER, MAIL_PWD);
      }
        });
      MimeMessage msg = new MimeMessage(session);
      msg.setSubject(rb.getString("regConfirmSubject"));
      msg.setText(rb.getString("regConfirmMsg") + rb.getString("regConfirmGreetings"));

      msg.addRecipient(Message.RecipientType.TO, toAddr);

      msg.setFrom(fromAddr);

      Transport.send(msg);
     
      return true;

  } catch (UnsupportedEncodingException e)
  {
      logger.error("Character encoding error in the fromAddress or the " +
          "toAddress of the registration confirmation email!", e);
      return false;
  } catch (MessagingException e)
  {
      logger.error("Error while sending the registration confirmation email!", e);
      return false;
  }
 
    }
   

    @Override
  public boolean sendTripDeleteNotification(String email, String username)
    {
  UIViewRoot view = FacesContext.getCurrentInstance().getViewRoot();
  Application app = FacesContext.getCurrentInstance().getApplication();
  ResourceBundle rb = ResourceBundle.getBundle(app.getMessageBundle(), view.getLocale());
  try
  {
      InternetAddress fromAddr = new InternetAddress(FROM_ADDRESS, FROM_PERSONAL);
      InternetAddress toAddr = new InternetAddress(email, username);

      Session session = Session.getDefaultInstance(this.props,
        new javax.mail.Authenticator()
        {
      protected PasswordAuthentication getPasswordAuthentication()
      {
          return new PasswordAuthentication(MAIL_USER, MAIL_PWD);
      }
        });
      MimeMessage msg = new MimeMessage(session);
      msg.setSubject(rb.getString("delNotifySubject"));
      msg.setText(rb.getString("delNotifyMsg") + rb.getString("delNotifyGreetings"));

      msg.addRecipient(Message.RecipientType.TO, toAddr);

      msg.setFrom(fromAddr);

      Transport.send(msg);
     
      return true;

  } catch (UnsupportedEncodingException e)
  {
      logger.error("Character encoding error in the fromAddress or the " +
          "toAddress of the trip delete notification email!", e);
      return false;
  } catch (MessagingException e)
  {
      logger.error("Error while sending trip delete notification email!", e);
      return false;
  }
 
    }
}
TOP

Related Classes of at.fhj.itm.util.EmailUtilImpl

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.