package com.kiasoft.util; /* ------------------------------------------------------------------ * Kiasoft, Inc. * http://kiasoft.com * * Copyright (c) 2004-2006 Kiasoft, Inc. All Rights Reserved. * * ------------------------------------------------------------------ */ // JDK classes import java.util.Date; import java.util.Vector; // Turbine classes import org.apache.turbine.util.Log; import org.apache.turbine.util.mail.SimpleEmail; import org.apache.turbine.util.mail.HtmlEmail; import com.kiasoft.util.LocalDate; /** * Email.java * Email provides simple interface to email API provided * by turbine classes */ public class Email { private Recipient _from = null; private String _subject = ""; private String _body = ""; private boolean _showRecipients = false; private Vector _recipient = null; // HTML Elements private String _bgColor = ""; private String _fontColor = ""; /* * Constructor */ public Email() { this._subject = "Turbine Generated Email"; this._recipient = new Vector(); this.setFrom( "noreply@domain.com", "System Message" ); // default yellow and blue this.setBgColor("#ffff00"); this.setFontColor("#0000ff"); } /* * Setters */ public void setSubject( String x ) { this._subject = x; } public void setBgColor( String x ) { this._bgColor = x; } public void setFontColor( String x ) { this._fontColor = x; } public void setFrom( String email, String name ) { this._from = new Recipient( email, name ); } public void setBody( String x ) { this._body = x; } public void setShowRecipients( boolean x ) { this._showRecipients = x; } public void addRecipient( String email, String name ) { Recipient n = new Recipient( email, name ); this.getRecipients().add(n); return; } /* * Getters */ public String getSubject() { return this._subject; } public String getBgColor() { return this._bgColor; } public String getFontColor() { return this._fontColor; } public Recipient getFrom() { return this._from; } public String getBody() { return this._body; } public boolean getShowRecipients() { return this._showRecipients; } public Vector getRecipients() { return this._recipient; } // plain send method will force text message public void send() { this.sendTextEmail(); return; } public void sendTextEmail() { try { Vector recipients = this.getRecipients(); if ( recipients.size() > 0 ) { Date now = new Date(); Recipient from = this.getFrom(); SimpleEmail em = new SimpleEmail(); em.setFrom( from.getEmail(), from.getName() ); em.setSubject( this.getSubject() ); em.setMsg( this.getBody() ); // add each recipient for ( int i = 0; i < recipients.size(); i++ ) { Recipient to = (Recipient) recipients.elementAt(i); if ( this.getShowRecipients() ) { em.addTo( to.getEmail(), to.getName() ); } else { em.addBcc( to.getEmail(), to.getName() ); } } // send the message em.send(); } else { Log.error("No recipients found!"); } } catch ( Exception e ) { Log.error("Error sending text email: " + e.toString() ); } return; } public void sendHtmlEmail() { // =========================================================================== String htmlhead = " " + " " + this.getSubject() + " " + " " + "
" + " "; String htmlfoot = "
"; String htmlMsg = htmlhead + this.getBody() + htmlfoot; // =========================================================================== try { Vector recipients = this.getRecipients(); if ( recipients.size() > 0 ) { Date now = new Date(); Recipient from = this.getFrom(); HtmlEmail em = new HtmlEmail(); em.setFrom( from.getEmail(), from.getName() ); em.setSubject( this.getSubject() ); em.setMsg( htmlMsg ); // add each recipient for ( int i = 0; i < recipients.size(); i++ ) { Recipient to = (Recipient) recipients.elementAt(i); if ( this.getShowRecipients() ) { em.addTo( to.getEmail(), to.getName() ); } else { em.addBcc( to.getEmail(), to.getName() ); } } // send the message em.send(); } else { Log.error("No recipients found!"); } } catch ( Exception e ) { Log.error("Error sending html email: " + e.toString() ); } return; } private static class Recipient { private String name = ""; private String email = ""; public Recipient() { this.email = ""; this.name = ""; } public Recipient( String email, String name ) { this.email = email; this.name = name; } public String getName() { return this.name; } public String getEmail() { return this.email; } } }