/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.core.impl;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import com.silverwrist.venice.core.internals.Emailer;
import com.silverwrist.venice.except.EmailException;
import com.silverwrist.venice.except.InternalStateError;

class SimpleEmailer implements Emailer
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private Session session;         // the email session
  private MimeMessage msg;         // the message being composed

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  SimpleEmailer(Properties props, javax.mail.Session session)
  {
    this.session = session;
    msg = new MimeMessage(session);
    try
    { // set the X-Mailer header
      msg.setHeader("X-Mailer",(String)(props.get("com.silverwrist.venice.email.mailer")));

    } // end try
    catch (Exception e)
    { // do nothing - we don't really care about this too much
    } // end catch

    try
    { // set the From address on the message
      msg.setFrom(InternetAddress.getLocalAddress(session));

    } // end try
    catch (MessagingException me)
    { // this should be impossible
      throw new InternalStateError("condition should not apply to message here",me);

    } // end catch

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface Emailer
   *--------------------------------------------------------------------------------
   */

  public void setTo(String to) throws EmailException
  {
    try
    { // create an Internet address and set the recipient to it
      InternetAddress[] address = { new InternetAddress(to) };
      msg.setRecipients(Message.RecipientType.TO,address);

    } // end try
    catch (AddressException e1)
    { // the address was somehow invalid
      throw new EmailException("invalid email recipient address",e1);

    } // end catch
    catch (MessagingException e2)
    { // msg.setRecipients should NOT be throwing a MessageException here!
      throw new InternalStateError("condition should not apply to message here",e2);

    } // end catch

  } // end setTo

  public void setFrom(String from_name, String from_addr) throws EmailException
  {
    try
    { // set the "from" address
      InternetAddress addr = new InternetAddress(from_addr);
      addr.setPersonal(from_name);
      msg.setFrom(addr);

      // make sure the "Sender" address reflects our email address
      InternetAddress sender = InternetAddress.getLocalAddress(session);
      msg.setHeader("Sender",sender.toString());

    } // end try
    catch (AddressException e1)
    { // the address was somehow invalid
      throw new EmailException("invalid email sender address",e1);

    } // end catch
    catch (MessagingException e2)
    { // msg.setRecipients should NOT be throwing a MessageException here!
      throw new InternalStateError("condition should not apply to message here",e2);

    } // end catch
    catch (java.io.UnsupportedEncodingException e3)
    { // we can't really have that happen here, either!
      throw new InternalStateError("condition should not apply to message here",e3);
      
    } // end catch

  } // end setFrom

  public void setSubject(String subject)
  {
    try
    { // set the mail message subject
      msg.setSubject(subject);

    } // end try
    catch (MessagingException e2)
    { // msg.setSubject should NOT be throwing a MessageException here!
      throw new InternalStateError("condition should not apply to message here",e2);

    } // end catch

  } // end setSubject

  public void setText(String text)
  {
    try
    { // set the mail message subject
      msg.setText(text);

    } // end try
    catch (MessagingException e2)
    { // msg.setText should NOT be throwing a MessageException here!
      throw new InternalStateError("condition should not apply to message here",e2);

    } // end catch
    
  } // end setText

  public void send() throws EmailException
  {
    try
    { // attempt to send the transport message
      Transport.send(msg);

    } // end try
    catch (SendFailedException e1)
    { // convert this exception into an EmailException
      throw new EmailException("send of the email message failed: " + e1.getMessage(),e1);

    } // end catch
    catch (MessagingException e2)
    { // msg.setText should NOT be throwing a MessageException here!
      throw new InternalStateError("condition should not apply to message here",e2);

    } // end catch
    
  } // end send

} // end class SimpleEmailer

