package sos.net;
import java.io.*;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.text.DecimalFormat;
import java.text.FieldPosition;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.*;
import javax.mail.internet.*;
import sos.settings.SOSSettings;
import sos.util.*;
/**
*
* @version $Id: SOSMail.java 2884 2007-08-07 09:41:42Z gb $
*/
public class SOSMail {
abstract class My_data_source implements DataSource
{
final String name;
final String content_type;
public My_data_source( File new_filename, String content_type )
{
this.name = new_filename.getName();
this.content_type = content_type;
}
public String getContentType() { return content_type; }
public String getName () { return name; }
public OutputStream getOutputStream()
{
throw new RuntimeException( getClass().getName() + " hat keinen OutputStream" );
}
}
//--------------------------------------------------------------------------------class File_data_source
// Eigene Klasse f�r Attachments, damit close m�glich ist.
class File_data_source extends My_data_source
{
final File file;
public File_data_source( File file, String content_type )
{
super( file, content_type );
this.file = file;
}
public InputStream getInputStream() throws IOException
{
FileInputStream f = new FileInputStream( file );
file_input_streams.add( f ); // wird von Message.close() geschlossen
return f;
}
}
/** Attribut: host:
* - Wird im Konstruktor als Parameter �bergeben.
* - Kann �ber Settings-Objekt gesetzt werden.
* - mit .setHost kann Host gesetzt werden. Es wird dann eine auch eine neue Message-id vergeben.
*/
protected String host="";
/** Attribut: port: Default=25
* - Wird im Konstruktor als Parameter �bergeben.
* - Kann �ber Settings-Objekt gesetzt werden.
* - mit .setPort kann Port gesetzt werden. Es wird dann eine auch eine neue Message-id vergeben.
*/
protected String port="25";
/** Attribut: user:
* Zur Verwendung bei SMTP_Server, die eine Autentifizierung verlangen
* - Wird im Konstruktor als Parameter �bergeben.
* - Kann �ber Settings-Objekt gesetzt werden.
* - mit .setUser kann User gesetzt werden. Es wird dann eine auch eine neue Message-id vergeben.
*/
protected String user="";
/** Attribut: password:
* Zur Verwendung bei SMTP_Server, die eine Autentifizierung verlangen
* - Wird im Konstruktor als Parameter �bergeben.
* - Kann �ber Settings-Objekt gesetzt werden.
* - mit .setPassword kann User gesetzt werden. Es wird dann eine auch eine neue Message-id vergeben.
*/
protected String password="";
/** Attribut: timeout: Default=5000
*
* - Wird im Konstruktor als Parameter �bergeben.
* - Kann �ber Settings-Objekt gesetzt werden. Dort werden Sekunden angegeben
* - mit .setTimeout kann Timeout gesetzt werden. Es wird dann eine auch eine neue Message-id vergeben.
*/
protected int timeout=5000;
/** Attribut: subject
*
* Kann Variable enthalten, die substituiert werden z.B. &(var).
* Siehe Methode substitute
*
*
*/
protected String subject="";
protected String from="";
protected String fromName="";
protected String replyTo="";
protected String queueDir="";
protected String body = "";
protected String alternativeBody = "";
protected String language = "de";
protected String dateFormat = "dd.MM.yyyy";
protected String datetimeFormat = "dd.MM.yyyy HH:mm";
protected HashMap dateFormats = new HashMap();
protected HashMap datetimeFormats = new HashMap();
/** Attribut: charSet: iso-8859-1, us-ascii f�r Attachments. Wenn leer
Wird charset verwendet.
*/
protected String attachmentCharset = "iso-8859-1";
/** Attribut: charSet: iso-8859-1, us-ascii f�r Body.*/
protected String charset = "iso-8859-1";
protected String alternativeCharset = "iso-8859-1";
/** Mime-Type der Nachricht: text/plain, text/html etc.*/
protected String contentType = "text/plain";
protected String alternativeContentType = "text/html";
/** Attribut: encoding: Codierung des Nachrichteninhalts (Quoted-Printable, Base64) */
protected String encoding = "7bit";
protected String alternativeEncoding = "7bit";
/** Attribut: encoding: Codierung des Anhanges (Quoted-Printable, Base64) */
protected String attachmentEncoding = "Base64";
/** Mime-Type der Nachricht: application/octet-stream, application/pdf etc. */
protected String attachmentContentType = "application/octet-stream";
/** recipient Liste */
protected LinkedList toList = new LinkedList();
/** cc Liste */
protected LinkedList ccList = new LinkedList();
/** bcc Liste */
protected LinkedList bccList = new LinkedList();
/** attachment Liste */
protected TreeMap attachmentList = new TreeMap();
/** Muster f�r eMail-Texte */
protected Properties templates = new Properties();
/** sos settings object */
protected SOSSettings sosSettings = null;
/** Tabelle f�r Einstellungen */
protected String tableSettings = "SETTINGS";
/** Tabelle f�r eMail Auftr�ge und Historie */
public static String tableMails = "MAILS";
/** Tabelle f�r eMail Anh�nge */
public static String tableMailAttachments = "MAIL_ATTACHMENTS";
/** Sequenzname der IDs f�r die MAILS Tablle*/
public static String mailsSequence = "MAILS_ID_SEQ";
/** Applikationsname f�r eMail-Einstellungen */
protected String applicationMail = "email";
/** Sektionsname f�r eMail-Einstellungen */
protected String sectionMail = "mail_server";
/** Applikationsname f�r eMail-Templates in Settings */
protected String applicationMailTemplates = "email_templates";
/** Sektionsname f�r eMail-Templates in Settings */
protected String sectionMailTemplates = "mail_templates";
/** Applikationsname f�r eMail-Scripts in Settings */
protected String applicationMailScripts = "email";
/** Sektionsname f�r eMail-Scripts in Settings */
protected String sectionMailScripts = "mail_start_scripts_factory";
/** Applikationsname f�r eMail-Templates der Document Factory in Settings */
protected String applicationMailTemplatesFactory = "email_templates_factory";
/** Sektionsname f�r eMail-Templates der Document Factory in Settings */
protected String sectionMailTemplatesFactory = "mail_templates";
/** Email als byteArray verf�gbar machen */
private boolean sendToOutputStream=false;
private byte[] messageBytes;
private MimeMessage message = null;
private SOSMailAuthenticator authenticator = null;
private ArrayList file_input_streams = new ArrayList(); // Alle offenen Attachments, werden von close() geschlossen
protected SOSLogger sosLogger=null;
private ByteArrayOutputStream raw_email_byte_stream = null;
private String lastError="";
private boolean changed=false;
private String queuePattern="yyyy-MM-dd.HHmmss.S";
private String queuePraefix="sos.";
private String lastGeneratedFileName="";
private String loadedMessageId="";
private boolean messageReady=false;
private int priority=-1;
private Session session=null;
// Konstanten f�r Priorit�ten (in Settings und MAILS Tabelle)
public static final int PRIORITY_HIGHEST = 1;
public static final int PRIORITY_HIGH = 2;
public static final int PRIORITY_NORMAL = 3;
public static final int PRIORITY_LOW = 4;
public static final int PRIORITY_LOWEST = 5;
//Depricated
private String filename;
//--------------------------------------------------------------------------------Konstruktor
/**
* Konstruktor
* @param host string Hostname oder IP-Adresse des Mail-Servers
* Wird verwendet bei smtp-server ohne Autentifierung
* Standardport 25 wird verwendet
* @throws java.lang.Exception
*/
public SOSMail(String host) throws Exception {
if (host != null) {
this.host = host;
}
this.init();
}
/**
* Konstruktor
* @param host String Hostname oder IP-Adresse des Mail-Servers
* @param user String Name des SMTP-Benutzers
* @param pass String Kennwort des SMTP-Benutzers
* Wird verwendet bei smtp-server mit Autentifierung
* Standardport 25 wird verwendet
*
* @throws java.lang.Exception
*/
public SOSMail(String host_, String user_, String password_)
throws Exception {
if (host_ != null) {
this.host = host_;
}
if (user_ != null) {
this.user = user_;
}
if (password_ != null) {
this.password = password_;
}
this.init();
}
/**
* Konstruktor
* @param host String Hostname oder IP-Adresse des Mail-Servers
* @param user String Name des SMTP-Benutzers
* @param pass String Kennwort des SMTP-Benutzers
* Wird verwendet bei smtp-server mit Autentifierung ,wenn der Port festgelegt werden soll
*
* @throws java.lang.Exception
*/
public SOSMail(String host_, String port_, String user_, String password_)
throws Exception {
if (host_ != null) {
this.host = host_;
}
if (port_ != null) {
this.port = port_;
}
if (user_ != null) {
this.user = user_;
}
if (password_ != null) {
this.password = password_;
}
this.init();
}
/**
* Konstruktor
* @param sosSettings SOSSettings Einstellungen aus Profile Settings oder Connection Settings
* @throws java.lang.Exception
*/
public SOSMail(SOSSettings sosSettings)
throws Exception {
this.getSettings(sosSettings);
this.init();
}
/**
* Konstruktor
* @param sosSettings SOSSettings Einstellungen aus Profile Settings oder Connection Settings
* @param language Sprache f�r Einstellungen
* @throws java.lang.Exception
*/
public SOSMail(SOSSettings sosSettings, String language)
throws Exception {
this.getSettings(sosSettings, language);
this.init();
}
/**
* Initialisierungen
* -ruft initMessage()
* -ruf initLanguage()
* Wird im Konstruktor gerufen.
*
* @throws java.lang.Exception
*/
//--------------------------------------------------------------------------------init
public void init() throws Exception {
this.dateFormats.put( "de", "dd.MM.yyyy");
this.dateFormats.put( "en", "MM/dd/yyyy");
this.datetimeFormats.put( "de", "dd.MM.yyyy HH:mm");
this.datetimeFormats.put( "en", "MM/dd/yyyy HH:mm");
this.initLanguage();
this.initMessage();
clearRecipients() ;
clearAttachments();
switch(this.priority) {
case PRIORITY_HIGHEST: this.setPriorityHighest(); break;
case PRIORITY_HIGH: this.setPriorityHigh(); break;
case PRIORITY_LOW: this.setPriorityLow(); break;
case PRIORITY_LOWEST: this.setPriorityLowest(); break;
default: this.setPriorityNormal(); break;
}
}
//--------------------------------------------------------------------------------initMessage
/**
* Initialisierungen
* -Autorisierung f�r smtp-Server
* -Anlegen des Message-Objekts
* -L�schen der Empf�nger- und der Attachmentliste
* Wenn ein sosmail-Objekt mehrmach wiederverwendet werden sollen , muss f�r jede Nachricht
* .init() gerufen.
*
* @throws java.lang.Exception
*/
public void initMessage() throws Exception {
createMessage(createSession());
}
//--------------------------------------------------------------------------------createSession
/**
* Initialisierungen
* -Autorisierung f�r smtp-Server
* -Anlegen des Message-Objekts
* -L�schen der Empf�nger- und der Attachmentliste
* Wenn ein sosmail-Objekt mehrmach wiederverwendet werden sollen , muss f�r jede Nachricht
* .init() gerufen.
*
* @throws java.lang.Exception
*/
public Session createSession() throws Exception {
Properties props = System.getProperties();
props.put("mail.host", this.host);
props.put("mail.port", this.port);
props.put("mail.smtp.timeout",String.valueOf(timeout));
props.put( "mail.transport.protocol", "smtp");
props.put( "mail.smtp.class", "com.sun.mail.SMTPTransport");
if (!this.user.equals("") && !this.password.equals("")){
props.put("mail.smtp.auth", "true");
authenticator = new SOSMailAuthenticator(this.user,this.password);
session = Session.getInstance(props, authenticator);
}else{
authenticator = new SOSMailAuthenticator(this.user,this.password);
session = Session.getInstance(props, authenticator);
}
return session;
}
//--------------------------------------------------------------------------------createMessage
public void createMessage(Session session) throws Exception {
message = new MimeMessage(session);
}
//--------------------------------------------------------------------------------initLanguage
/**
* Initialisierungen
* -Datumsformat setzen
*
* @throws java.lang.Exception
*/
public void initLanguage() throws Exception {
if ( this.dateFormats.containsKey(this.getLanguage()) && this.datetimeFormats.containsKey(this.getLanguage()) ) {
this.setDateFormat( this.dateFormats.get(this.getLanguage()).toString());
this.setDatetimeFormat( this.datetimeFormats.get(this.getLanguage()).toString());
} else {
this.setDateFormat( this.dateFormats.get("de").toString() );
this.setDatetimeFormat( this.datetimeFormats.get("de").toString() );
}
}
//--------------------------------------------------------------------------------log
public void log(String s,int level) throws Exception {
s = "SOSMail." + s;
// if (sosLogger != null && Math.abs(logLevel) >= Math.abs(level)){
if (sosLogger != null ){
switch(level) {
case SOSLogger.DEBUG1: sosLogger.debug1(s); break;
case SOSLogger.DEBUG2: sosLogger.debug2(s); break;
case SOSLogger.DEBUG3: sosLogger.debug3(s); break;
case SOSLogger.DEBUG4: sosLogger.debug4(s); break;
case SOSLogger.DEBUG5: sosLogger.debug5(s); break;
case SOSLogger.DEBUG6: sosLogger.debug6(s); break;
case SOSLogger.DEBUG7: sosLogger.debug7(s); break;
case SOSLogger.DEBUG8: sosLogger.debug8(s); break;
case SOSLogger.DEBUG9: sosLogger.debug9(s); break;
case SOSLogger.INFO: sosLogger.info(s); break;
case SOSLogger.WARN: sosLogger.warn(s); break;
case SOSLogger.ERROR: sosLogger.error(s); break;
}
}
}
//--------------------------------------------------------------------------------getSettings
/**
* Einstellungen f�r eMail-Versand aus Settings
*
* @param sosSettings SOSSettings Einstellungen aus Profile Settings oder Connection Settings
* @param language Sprache fr Einstellungen
* @throws java.lang.Exception
*/
private void getSettings(SOSSettings sosSettings, String language) throws Exception {
if ( language != null ) { this.setLanguage(language); }
this.getSettings(sosSettings);
}
//--------------------------------------------------------------------------------getEntry
private String getEntry(String val, Properties entries,String key){
String erg=val; //Do nothing if not exist
if (entries.containsKey(key) ) {
if ( entries.getProperty(key).length() > 0 )
erg = entries.getProperty(key);
}
return erg;
}
//--------------------------------------------------------------------------------getSettings
/**
* Einstellungen fr eMail-Versand aus Settings
*
* @param sosSettings SOSSettings Einstellungen aus Profile Settings oder Connection Settings
* @throws java.lang.Exception
*/
private void getSettings(SOSSettings sosSettings) throws Exception {
if (sosSettings == null)
throw new Exception(
SOSClassUtil.getMethodName() + ": missing settings object.");
this.sosSettings = sosSettings;
Properties entries = this.sosSettings.getSection(this.applicationMail, this.sectionMail);
if (entries.size() == 0)
throw new Exception(
SOSClassUtil.getMethodName()
+ ": missing settings entries in section \"" + this.sectionMail + "\".");
this.host = getEntry(this.host,entries,"host");
this.port = getEntry(this.port,entries,"port");
this.user = getEntry(this.user,entries,"smtp_user");
this.password = getEntry(this.password,entries,"smtp_pass");
this.from = getEntry(this.from,entries,"from");
this.from = getEntry(this.from,entries,"mail_from");
this.fromName = getEntry(this.fromName,entries,"from_name");
this.fromName = getEntry(this.fromName,entries,"mail_from_name");
this.replyTo = getEntry(this.replyTo,entries,"reply_to");
this.replyTo = getEntry(this.replyTo,entries,"mail_reply_to");
this.queueDir = getEntry(this.queueDir,entries,"queue_directory");
this.queueDir = getEntry(this.queueDir,entries,"mail_queue_directory");
String priorityStr = new String("1");
priority = Integer.parseInt(getEntry(priorityStr,entries,"priority"));
this.language = getEntry(this.language,entries,"language");
this.subject = getEntry(this.subject,entries,"subject");
this.contentType = getEntry(this.contentType,entries,"content_type");
this.charset = getEntry(this.charset,entries,"charset");
this.encoding = getEntry(this.encoding,entries,"encoding");
this.attachmentEncoding = getEntry(this.attachmentEncoding,entries,"attachment_encoding");
this.attachmentEncoding = getEntry(this.attachmentEncoding,entries,"file_encoding");
if (entries.getProperty("smtp_timeout") != null) {
if ( entries.getProperty("smtp_timeout").length() > 0 )
this.timeout = 1000*Integer.parseInt(entries.getProperty("smtp_timeout"));
}
if (this.from == null && entries.containsKey("mail_from")) {
if ( entries.getProperty("mail_from").length() > 0 )
this.from = entries.getProperty("mail_from");
}
}
//--------------------------------------------------------------------------------getTemplates
/**
* Muster f�r eMail-Texte
*
* @param sosSettings SOSSettings Einstellungen aus Profile Settings oder Connection Settings
* @throws java.lang.Exception
*/
public boolean getTemplates(SOSSettings sosSettings, String language) throws Exception {
if ( language != null ) this.setLanguage(language);
return this.getTemplates(sosSettings);
}
//--------------------------------------------------------------------------------getTemplates
/**
* Muster f�r eMail-Texte
*
* @param sosSettings SOSSettings Einstellungen aus Profile Settings oder Connection Settings
* @throws java.lang.Exception
*/
public boolean getTemplates(SOSSettings sosSettings) throws Exception {
if (sosSettings == null)
throw new Exception(
SOSClassUtil.getMethodName() + ": missing settings object.");
this.sosSettings = sosSettings;
this.templates = this.sosSettings.getSection(this.getApplicationMailTemplates(),
this.getSectionMailTemplates() + "_" + language);
if (this.templates.size() == 0)
throw new Exception(
SOSClassUtil.getMethodName()
+ ": missing settings entries for application \"" + this.applicationMailTemplates + "\" in section \"" + this.sectionMailTemplates + "\".");
return true;
}
//--------------------------------------------------------------------------------substituteSubject
/**
*
* @param template Name des Templates
* @param replacements HashMap mit Name/Wert-Paaren, der Name wird im Template durch den Wert ersetzt
* @param nl2br NewLines durch HTML Breaks ersetzen
* @throws java.lang.Exception
*/
public String substituteSubject(String template, HashMap replacements)
throws Exception {
if (!this.templates.containsKey(template + "_subject")) {
throw (new Exception("substituteSubject(): template does not exist: " + template + "_subject" ));
}
return substitute( this.templates.get(template + "_subject").toString(), replacements, false );
}
//--------------------------------------------------------------------------------substituteBody
/**
*
* @param template Name des Templates
* @param replacements HashMap mit Name/Wert-Paaren, der Name wird im Template durch den Wert ersetzt
* @param nl2br NewLines durch HTML Breaks ersetzen
* @throws java.lang.Exception
*/
public String substituteBody(String template, HashMap replacements, boolean nl2br)
throws Exception {
if (!this.templates.containsKey(template + "_body")) {
throw (new Exception("substituteBody(): template does not exist: " + template + "_body" ));
}
return substitute( this.templates.get(template + "_body").toString(), replacements, nl2br );
}
//--------------------------------------------------------------------------------substituteBody
/**
*
* @param template Name des Templates
* @param replacements HashMap mit Name/Wert-Paaren, der Name wird im Template durch den Wert ersetzt
* @param nl2br NewLines durch HTML Breaks ersetzen
* @throws java.lang.Exception
*/
public String substituteBody(String template, HashMap replacements )
throws Exception {
if (! this.templates.containsKey(template + "_body")) {
throw (new Exception("substituteBody(): template does not exist: " + template + "_body" ));
}
return substitute( this.templates.get(template + "_body").toString(), replacements, false);
}
//-------------------------------------------------------------------------------------substitute
/**
*
* @param content String mit beliebigem Inhalt
* @param replacements HashMap mit Name/Wert-Paaren, der Name wird im content durch den Wert ersetzt
* @param nl2br NewLines durch HTML Breaks ersetzen
* @throws java.lang.Exception
* @deprecated
*/
public String substitute(String content, HashMap replacements, boolean nl2br)
throws Exception {
Object key = null;
Object value = null;
if (this.getLanguage().equalsIgnoreCase("de")) {
this.setDateFormat("dd.MM.yyyy");
this.setDatetimeFormat("dd.MM.yyyy HH:mm");
} else if (this.getLanguage().equalsIgnoreCase("en")) {
this.setDateFormat("MM/dd/yyyy");
this.setDatetimeFormat("MM/dd/yyyy HH:mm");
}
content = content.replaceAll( "&\\(date\\)", SOSDate.getCurrentDateAsString(this.getDateFormat()) );
content = content.replaceAll( "&\\(datetime\\)", SOSDate.getCurrentTimeAsString(this.getDatetimeFormat()) );
content = content.replaceAll( "&\\#\\(date\\)", SOSDate.getCurrentDateAsString(this.getDateFormat()) );
content = content.replaceAll( "&\\#\\#\\(datetime\\)", SOSDate.getCurrentTimeAsString(this.getDatetimeFormat()) );
if (nl2br) {
content = content.replaceAll( "\n", "
");
}
if ( replacements != null ) {
Iterator keys = replacements.keySet().iterator();
while( keys.hasNext() ) {
key = keys.next();
if ( key != null ) {
value = replacements.get(key.toString());
if (value != null ) {
try {
content = content.replaceAll( "&\\#\\(" + key.toString() + "\\)", SOSDate.getDateAsString(SOSDate.getDate(value.toString()), this.getDateFormat()) );
content = content.replaceAll( "&\\#\\#\\(" + key.toString() + "\\)", SOSDate.getDateTimeAsString(SOSDate.getDate(value.toString()), this.getDatetimeFormat()) );
} catch (Exception ex) {} // ignore this error: replacement is not convertible to date
Locale defaultLocale = Locale.getDefault();
try {
double doubleValue = Double.parseDouble(value.toString());
if (this.getLanguage().equalsIgnoreCase("de")) {
Locale.setDefault(Locale.GERMAN);
} else if (this.getLanguage().equalsIgnoreCase("en")) {
Locale.setDefault(Locale.US);
}
DecimalFormat formatter = new DecimalFormat("#,###.00");
content = content.replaceAll( "&\\$\\(" + key.toString() + "\\)", formatter.format(doubleValue).toString() );
} catch (Exception ex) {
} finally {
Locale.setDefault(defaultLocale);
}
content = content.replaceAll( "&\\(" + key.toString() + "\\)", value.toString() );
}
}
}
}
// remove all variables that were not substituted
content = content.replaceAll( "&\\#\\(.*\\)", "");
content = content.replaceAll( "&\\#\\#\\(.*\\)", "");
content = content.replaceAll( "&\\$\\(.*\\)", "");
return content.replaceAll( "&\\(.*\\)", "");
}
//--------------------------------------------------------------------------------addRecipient
/**
*
* @param recipient eMail-Adresse des Empf�ngers
* @throws java.lang.Exception
*/
public void addRecipient(String recipient) throws Exception {
String token="";
warn ("addRecipient",recipient);
if (recipient == null)
throw new Exception(
SOSClassUtil.getMethodName() + ": recipient has no value.");
recipient = recipient.replace(',',';');
StringTokenizer t = new StringTokenizer(recipient, ";");
while (t.hasMoreTokens() ) {
token = t.nextToken();
if (!toList.contains(token)){
this.toList.add(token);
}
log(SOSClassUtil.getMethodName() + "-->" + token,SOSLogger.DEBUG9);
}
changed=true;
}
//--------------------------------------------------------------------------------addCC
/**
*
* @param recipient eMail-Adresse des cc Empf�ngers
* @throws java.lang.Exception
*/
public void addCC(String recipient) throws Exception {
String token="";
warn ("addCC",recipient);
if (recipient == null)
throw new Exception(
SOSClassUtil.getMethodName()
+ ": CC recipient has no value.");
recipient = recipient.replace(',',';');
StringTokenizer t = new StringTokenizer(recipient, ";");
while (t.hasMoreTokens() ) {
token = t.nextToken();
if (!toList.contains(token)){
this.ccList.add(token);
log(SOSClassUtil.getMethodName() + "-->" + token,SOSLogger.DEBUG9);
}else{
log(SOSClassUtil.getMethodName() + "--> Ignored:" + token,SOSLogger.DEBUG9);
}
}
changed=true;
}
//--------------------------------------------------------------------------------addBCC
/**
*
* @param recipient eMail-Adresse des bcc Empf�ngers
* @throws java.lang.Exception
*/
public void addBCC(String recipient) throws Exception {
String token="";
warn ("addBCC",recipient);
if (recipient == null)
throw new Exception(
SOSClassUtil.getMethodName()
+ ": BCC recipient has no value.");
recipient = recipient.replace(',',';');
StringTokenizer t = new StringTokenizer(recipient, ";");
while (t.hasMoreTokens() ) {
token = t.nextToken();
if (!ccList.contains(token) && !toList.contains(token)){
this.bccList.add(token);
log(SOSClassUtil.getMethodName() + "-->" + token,SOSLogger.DEBUG9);
}else{
log(SOSClassUtil.getMethodName() + "--> Ignored:" + token,SOSLogger.DEBUG9);
}
}
changed=true;
}
//--------------------------------------------------------------------------------closeAttachments
private void closeAttachments() throws Exception {
Exception exception = null;
for( int i = 0; i < file_input_streams.size(); i++ )
{
try
{
((FileInputStream)file_input_streams.get(i)).close();
}
catch( Exception x ) { if( exception == null ) exception = x; }
}
if( exception != null ) throw exception;
}
// --------------------------------------------------------------------------------addAttachment
/**
*
* @param filename Datei fr Anhang
* @throws java.lang.Exception
*/
public void addAttachment(SOSMailAttachment att) throws Exception {
warn ("addAttachment",att.getFile().getAbsolutePath());
this.attachmentList.put(att.getFile().getAbsolutePath(), att);
changed=true;
}
//--------------------------------------------------------------------------------addAttachment
/**
*
* @param filename Datei fr Anhang
* @throws java.lang.Exception
*/
public void addAttachment(String filename) throws Exception {
warn ("addAttachment",filename);
File f = new File(filename);
SOSMailAttachment att = new SOSMailAttachment(this,f);
this.attachmentList.put(filename, att);
changed=true;
}
//--------------------------------------------------------------------------------addAttachment
/**
*
* @param filename Datei fr Anhang
* @param contentType Content-Type dieses Anhangs
* @throws java.lang.Exception
*/
public void addAttachment(String filename, String params) throws Exception {
String name="";
String value="";
String token="";
int counter=0;
warn ("addAttachment",filename + "(" +params +")");
StringTokenizer t = new StringTokenizer(params, ",");
File f = new File(filename);
SOSMailAttachment att = new SOSMailAttachment(this,f);
while (t.hasMoreTokens()) {
token = t.nextToken();
StringTokenizer vv = new StringTokenizer(token, "=");
if (vv.countTokens()==1){
name = "content-type";
value = vv.nextToken();
counter +=1;
}else{
name = vv.nextToken().trim();
try{
value = vv.nextToken().trim();
}catch (NoSuchElementException e){
value="";
}
}
if (name.equalsIgnoreCase("content-type")){
att.setContentType(value);
}
else if (name.equalsIgnoreCase("charset")){
att.setCharset(value);
}
else if (name.equalsIgnoreCase("encoding")){
att.setEncoding(value);
}else{
throw new Exception("USING of .addAttachment is wrong. ==> " + params + ", rigth using is: [content-type-value],[content-type=],[charset=],[encoding=]");
}
if (counter > 1 ){
throw new Exception("USING of .addAttachment is wrong. ==> " + params + ", rigth using is: [content-type-value],[content-type=],[charset=],[encoding=]");
}
}
this.attachmentList.put(filename, att);
changed=true;
}
//--------------------------------------------------------------------------------add_file
private void add_file( SOSMailAttachment att ) throws Exception
{
if( !att.getFile().exists() ) throw new Exception( "Datei "+ att.getFile().getAbsolutePath() + " fehlt" );
MimeBodyPart attachment = new MimeBodyPart();
DataSource data_source = new File_data_source( att.getFile(), att.getContentType() );
DataHandler data_handler = new DataHandler( data_source );
attachment.setDataHandler( data_handler );
attachment.setFileName ( att.getFile().getName() );
//Charset des Attachments setzen, wenn content_type text/ ist
if (att.getContentType().startsWith("text/")){
// Charset macht nur Sinn bei text/
String s = "";
FileReader fr = new FileReader( att.getFile() );
for ( int c; ( c = fr.read() ) != -1; )
s += ( (char) c );
attachment.setText(s,att.getCharset());
fr.close();
}
Object m = message.getContent();
if( !( m instanceof MimeMultipart ) ) throw new RuntimeException( getClass().getName() + "mime_message.getContent() liefert nicht MimeMultiPart" );
((MimeMultipart)m).addBodyPart( attachment );
attachment.setHeader("Content-Transfer-Encoding", att.getEncoding());
}
//--------------------------------------------------------------------------------loadFile
public void loadFile(File messageFile) throws Exception {
FileInputStream messageInputStream= null;
try {
messageInputStream = new FileInputStream(messageFile);
message = new MimeMessage( createSession(), messageInputStream );
loadedMessageId = message.getMessageID();
raw_email_byte_stream = new ByteArrayOutputStream();
message.writeTo( raw_email_byte_stream );
messageBytes = raw_email_byte_stream.toByteArray();
messageReady=true;
}
catch( Exception x ){
throw new Exception( "Fehler beim Lesen der eMail. " + messageFile );
}
finally{
if (messageInputStream != null){
messageInputStream.close();
}
}
}
public void unloadMessage(){
messageReady = false;
loadedMessageId = "";
message=null;
}
//--------------------------------------------------------------------------------send
/**
* Nachricht versenden
*
* @throws java.lang.Exception
*/
public boolean send() throws Exception {
return sendJavaMail();
}
//--------------------------------------------------------------------------------send
/**
* Nachricht vorbereiten: Nicht senden
*
* @throws java.lang.Exception
*/
public boolean send(boolean send) throws Exception {
if (send){
return send();
}else{
return prepareJavaMail();
}
}
//--------------------------------------------------------------------------------sendJavaMail
/**
* Nachricht mit JavaMail-Funktionen versenden
*
* @throws java.lang.Exception
*/
private boolean sendJavaMail() throws Exception {
try {
prepareJavaMail();
String sTO = this.getRecipientsAsString();
String logMessage = SOSClassUtil.getMethodName() +
"-->" + "sending email:" +
" host:port=" + host + ":" + port +
" to=" + sTO;
String sCC = this.getCCsAsString();
if (!sCC.equals("")){
logMessage += " sCC=" + sCC;
}
String sBCC = this.getBCCsAsString();
if (!sBCC.equals("")){
logMessage += " sBCC=" + sBCC;
}
log(logMessage,SOSLogger.INFO);
log(SOSClassUtil.getMethodName() + "-->" + "Subject=" + this.subject,SOSLogger.DEBUG6);
log (SOSClassUtil.getMethodName() + "-->" + dumpHeaders(),SOSLogger.DEBUG6);
log (SOSClassUtil.getMethodName() + "-->" + dumpMessageAsString(false),SOSLogger.DEBUG9);
// --------------------------------------------------------------
if (!sendToOutputStream){
// Hier kann nicht die statische Methode Transport.send verwendet werden, da diese ein
// implizites saveChanges macht. Das f�hrt zu einer neuen Vergabe einer Message-Id, so dass diese
// m�glicherweise nicht mit der aus dumpMessageToFile �bereinstimmt.
Transport t = session.getTransport("smtp");
System.setProperty("mail.smtp.port", this.port);
t.connect(this.host,this.user,this.password);
t.sendMessage(message,message.getAllRecipients());
t.close();
raw_email_byte_stream = new ByteArrayOutputStream();
message.writeTo( raw_email_byte_stream );
messageBytes = raw_email_byte_stream.toByteArray();
changed = true;
}
// --------------------------------------------------------------
return true;
} catch (javax.mail.AuthenticationFailedException ee) {
this.lastError = "AuthenticationFailedException while connecting to " + host + ":" + port + " " + user + "/********" ;
dumpMessageToFile(true);
return false;
} catch (javax.mail.MessagingException e) {
// ist ein Fehler, bei dem es lohnt, zwischenzuspeichern?
if ((queueDir.length()>0 &&
e.getMessage().startsWith("Could not connect to SMTP host") ||
e.getMessage().startsWith("Unknown SMTP host") ||
e.getMessage().startsWith("Read timed out") ||
e.getMessage().startsWith("Exception reading response"))
){
this.lastError = e.getMessage() + " ==> "+ host + ":" + port + " " + user + "/********" ;
dumpMessageToFile(true);
return false;
}else{
throw new Exception(
SOSClassUtil.getMethodName() + ": error occurred on send: " + e.toString() );
}
} catch (SocketTimeoutException e){
if (queueDir.length()>0 ){
this.lastError = e.getMessage() + " ==> "+ host + ":" + port + " " + user + "/********" ;
dumpMessageToFile(true);
return false;
}else{
throw new Exception(
SOSClassUtil.getMethodName() + ": error occurred on send: " + e.toString() );
}
}
catch (Exception e) {
throw new Exception(
SOSClassUtil.getMethodName() + ": error occurred on send: " + e.toString() );
}
}
//--------------------------------------------------------------------------------haveAlternative
private boolean haveAlternative(){
return (!this.alternativeBody.equals("") && this.attachmentList.isEmpty());
}
//--------------------------------------------------------------------------------prepareJavaMail
/**
* Nachricht mit JavaMail-Funktionen versenden
*
* @throws java.lang.Exception
*/
protected boolean prepareJavaMail() throws Exception {
try {
if (messageReady ){
message.saveChanges();
return true;
}
if (!changed ){
return true;
}
changed = false;
if (this.getContentType().equals("text/html")) {
this.body = this.body.replaceAll("\\\\n", "
");
}else {
this.body = this.body.replaceAll("\\\\n", "\n");
}
String t = "";
if (this.toList.isEmpty()) {
throw new Exception(
SOSClassUtil.getMethodName() + ": no recipient specified.");
}
if (this.from == null || this.from.length() == 0) {
throw new Exception(
SOSClassUtil.getMethodName() + ": no sender specified.");
}
if ( this.fromName != null && this.fromName.length() > 0 ) {
message.setFrom( new InternetAddress(this.from, this.fromName) );
} else {
message.setFrom( new InternetAddress(this.from) );
}
message.setSentDate( new Date() );
if (this.replyTo != null && this.replyTo.length() > 0) {
InternetAddress fromAddrs[] = new InternetAddress[1];
fromAddrs[0] = new InternetAddress(this.replyTo);
message.setReplyTo( fromAddrs );
}
if (!this.toList.isEmpty()) {
InternetAddress toAddrs[] = new InternetAddress[toList.size()];
int i = 0;
for (ListIterator e = this.toList.listIterator(); e.hasNext();) {
t = e.next().toString();
toAddrs[i++] = new InternetAddress(t);
}
message.setRecipients(MimeMessage.RecipientType.TO, toAddrs );
}
if (!this.ccList.isEmpty()) {
InternetAddress toAddrs[] = new InternetAddress[ccList.size()];
int i = 0;
for (ListIterator e = this.ccList.listIterator(); e.hasNext();) {
t = e.next().toString();
toAddrs[i++] = new InternetAddress(t);
}
message.setRecipients(MimeMessage.RecipientType.CC, toAddrs );
}
if (!this.bccList.isEmpty()) {
InternetAddress toAddrs[] = new InternetAddress[bccList.size()];
int i = 0;
for (ListIterator e = this.bccList.listIterator(); e.hasNext(); ) {
t = e.next().toString();
toAddrs[i++] = new InternetAddress(t);
}
message.setRecipients(MimeMessage.RecipientType.BCC, toAddrs );
}
if (this.subject != null) {
message.setSubject(this.subject);
}
// send the attachments
if (! this.attachmentList.isEmpty() || !this.alternativeBody.equals("")) {
// Multipart nur bei Attachments!
// send the body
MimeBodyPart bodypart = null;
MimeBodyPart alternativeBodypart = null;
MimeMultipart multipart = null;
if (this.haveAlternative()){
multipart = new MimeMultipart("alternative");
}else{
multipart = new MimeMultipart();
}
bodypart = new MimeBodyPart();
if (contentType.startsWith("text/")){
bodypart.setContent( this.body,this.contentType + ";charset= " + this.charset);
}else{
bodypart.setContent( this.body,this.contentType);
}
multipart.addBodyPart(bodypart);
// Alternativer Body gesetzt? Nur wenn keine Attachments vorhanden!!!
if (this.haveAlternative()){
alternativeBodypart = new MimeBodyPart();
if (contentType.startsWith("text/")){
alternativeBodypart.setContent( this.alternativeBody,this.alternativeContentType + ";charset= " + this.alternativeCharset);
}else{
alternativeBodypart.setContent( this.alternativeBody,this.alternativeContentType);
}
multipart.addBodyPart(alternativeBodypart);
}
message.setContent(multipart);
// Encoding nur f�r Bodypart setzen
bodypart.setHeader("Content-Transfer-Encoding", encoding);
if (alternativeBodypart != null) {
alternativeBodypart.setHeader("Content-Transfer-Encoding", alternativeEncoding);
}
for (Iterator i = this.attachmentList.values().iterator(); i.hasNext();) {
SOSMailAttachment attachment = (SOSMailAttachment) i.next();
String content_type = attachment.getContentType();
if( content_type == null ) throw new Exception( "content_type ist null" );
log(SOSClassUtil.getMethodName() + "-->" + "Attachment=" + attachment.getFile(),SOSLogger.DEBUG6);
add_file( attachment );
}
}else{
message.setHeader("Content-Transfer-Encoding", encoding);
//Wenn content_type des body <> text_plain, dann content_type setzen
if (contentType.startsWith("text/")){
message.setContent( this.body,this.contentType + "; charset=" + this.charset);
}else{
message.setContent( this.body,this.contentType);
}
}
message.saveChanges();
closeAttachments();
return true;
} catch (Exception e) {
throw new Exception(
SOSClassUtil.getMethodName() + ": error occurred on send: " + e.toString() );
} finally {
}
}
//--------------------------------------------------------------------------------dumpHeaders
public String dumpHeaders() throws IOException, MessagingException{
String s = "";
for (Enumeration e = message.getAllHeaders(); e.hasMoreElements(); )
{ Header header = (Header) e.nextElement();
s += "\n" + header.getName() + ": " + header.getValue();
}
return s;
}
// --------------------------------------------------------------------------------messageRemoveAttachments
private ByteArrayOutputStream messageRemoveAttachments() throws Exception{
ByteArrayOutputStream raw_email_byte_stream_without_attachment = new ByteArrayOutputStream();
// Attachments entfernen
MimeMessage mm = new MimeMessage(message);
Object mmpo=mm.getContent();
if ( ( mmpo instanceof MimeMultipart ) ){
MimeMultipart mmp= (MimeMultipart) mmpo;
if (mm.isMimeType("text/plain")) {
} else if (mm.isMimeType("multipart/*")) {
mmp=(MimeMultipart)mm.getContent();
for (int i = 1; i < mmp.getCount(); i++) {
BodyPart part = mmp.getBodyPart(i);
mmp.removeBodyPart(i);
i--;
}
}
//
mm.setContent(mmp);
mm.saveChanges();
}
mm.writeTo(raw_email_byte_stream_without_attachment);
return raw_email_byte_stream_without_attachment;
}
//--------------------------------------------------------------------------------dumpMessageAsString
public String dumpMessageAsString() throws Exception{
return dumpMessageAsString(false);
}
//--------------------------------------------------------------------------------dumpMessageToFile
private void dumpMessageToFile(boolean withAttachment) throws Exception{
Date d = new Date();
StringBuffer bb = new StringBuffer();
SimpleDateFormat s = new SimpleDateFormat(queuePattern);
FieldPosition fp = new FieldPosition(0);
StringBuffer b = s.format(d,bb,fp);
lastGeneratedFileName = queueDir + "/" + queuePraefix + b + ".email";
File f = new File(lastGeneratedFileName);
while (f.exists()){
b = s.format(d,bb,fp);
lastGeneratedFileName = queueDir + "/" + queuePraefix + b + ".email";
f = new File(lastGeneratedFileName);
}
dumpMessageToFile(f, withAttachment);
}
//--------------------------------------------------------------------------------dumpMessageToFile
public void dumpMessageToFile(String filename, boolean withAttachment) throws Exception{
dumpMessageToFile(new File(filename), withAttachment);
}
//--------------------------------------------------------------------------------dumpMessageToFile
public void dumpMessageToFile(File file, boolean withAttachment) throws Exception{
try{
this.prepareJavaMail();
File myFile = new File(file.getAbsolutePath() + "~");
FileOutputStream out= new FileOutputStream(myFile, true);
out.write(dumpMessage(withAttachment));
out.close();
String newFilename = myFile.getAbsolutePath().substring(0,myFile.getAbsolutePath().length()-1);
File f = new File(newFilename);
f.delete();
myFile.renameTo(f);
}catch (Exception e){
throw new Exception(SOSClassUtil.getMethodName() + ": error occurred on dump: " + e.toString() );
}
}
//--------------------------------------------------------------------------------dumpMessageAsString
public String dumpMessageAsString(boolean withAttachment) throws Exception{
byte[] bytes;
ByteArrayOutputStream raw_email_byte_stream_without_attachment = null;
this.prepareJavaMail();
if (!withAttachment){
raw_email_byte_stream_without_attachment = messageRemoveAttachments();
}
raw_email_byte_stream = new ByteArrayOutputStream();
message.writeTo( raw_email_byte_stream );
if (withAttachment || raw_email_byte_stream_without_attachment==null ){
bytes = raw_email_byte_stream.toByteArray();
}else{
bytes = raw_email_byte_stream_without_attachment.toByteArray();
}
String s = new String (bytes);
return s;
}
public byte[] dumpMessage() throws Exception{
return dumpMessage(true);
}
//--------------------------------------------------------------------------------dumpMessage
public byte[] dumpMessage(boolean withAttachment) throws Exception{
byte[] bytes;
ByteArrayOutputStream raw_email_byte_stream_without_attachment = null;
this.prepareJavaMail();
if (!withAttachment){
raw_email_byte_stream_without_attachment = messageRemoveAttachments();
}
raw_email_byte_stream = new ByteArrayOutputStream();
message.writeTo( raw_email_byte_stream );
if (withAttachment || raw_email_byte_stream_without_attachment==null ){
bytes = raw_email_byte_stream.toByteArray();
}else{
bytes = raw_email_byte_stream_without_attachment.toByteArray();
}
return bytes;
}
//--------------------------------------------------------------------------------Getter/Setter
public LinkedList getRecipients(){
return toList;
}
// Liefert die Recipients aus dem Message-Objekt, wenn das Message-Objekt mit loadFile erzeugt wurde.
// Sonst werden die mit addRecipients hinzugef�gten Empf�nger geliefert.
public String getRecipientsAsString() throws MessagingException{
String s = " ";
if (messageReady){
Address[] addresses = message.getRecipients(MimeMessage.RecipientType.TO);
if (addresses != null){
for (int i = 0;i -1 && name.indexOf('>') > -1 ) {
return name;
} else {
return '<' + name + '>';
}
}
/**
* setzt den Timeout fr den Verbindungsaufbau zum Mail-Server
* @param timeout fr Verbindungsaufbau zum Mail-Server (inaktiv)
*/
public void setTimeout(int timeout) throws Exception {
this.timeout = timeout;
this.initMessage();
}
/**
* liefert den Timeout fr den Verbindungsaufbau zum Mail-Server
*/
public int getTimeout() {
return this.timeout;
}
/**
* liefert die Sprache fr eMail-Mustertexte und Datums-/Zeitformate
*/
public String getLanguage() {
return this.language;
}
/**
* setzt die Sprache fr eMail-Mustertexte und Datums-/Zeitformate
* @param language Sprache fr eMail-Muster (en, de, default:de)
*/
public void setLanguage(String language) throws Exception {
this.language = language;
this.initLanguage();
}
/**
* liefert das Datumsformat fr Datumsersetzungen im Text der eMail
*/
public String getDateFormat() {
return this.dateFormat;
}
/**
* setzt das Datumsformat fr Datumsersetzungen im Text der eMail
* @param dateFormat Datumsformat, z.B. dd.MM.yyyy
*/
public void setDateFormat(String dateFormat) {
this.dateFormat = dateFormat;
}
/**
* liefert das Datums- und Zeitformat fr Datumsersetzungen im Text der eMail
*/
public String getDatetimeFormat() {
return this.datetimeFormat;
}
/**
* setzt das Datums- und Zeitformat fr Datumsersetzungen im Text der eMail
* @param datetimeFormat Datumsformat, z.B. dd.MM.yyyy HH:mm:ss
*/
public void setDatetimeFormat(String datetimeFormat) {
this.datetimeFormat = datetimeFormat;
}
/**
* setzt das Encoding der eMail
* @param encoding
*/
public void setEncoding(String encoding) {
this.encoding = encoding;
warn ("encoding",encoding);
changed=true;
}
/**
* liefert das Encoding der eMail
*/
public String getEncoding() {
return this.encoding;
}
/**
* setzt das Character-Set der eMail
* @param charset
*/
public void setCharset(String charset) {
this.charset = charset;
warn ("charset",charset);
changed=true;
}
/**
* liefert das Character-Set der eMail
*/
public String getCharset() {
return this.charset;
}
/**
* setzt den Content-Type der eMail
* @param contentType Content-Type der eMail
*/
public void setContentType(String contentType) {
this.contentType = contentType;
warn ("contentType",contentType);
changed=true;
}
/**
* liefert den Content-Type der eMail
*/
public String getContentType() {
return this.contentType;
}
/**
* setzt den Content-Type fr Attachments
* @param attachmentContentType Default fr Content-Type der Attachments
*/
public void setAttachmentContentType(String attachmentContentType) {
this.attachmentContentType = attachmentContentType;
warn ("attachmentContentType",attachmentContentType);
changed=true;
}
/**
* liefert den Content-Type fr Attachments
*/
public String getAttachmentContentType() {
return this.attachmentContentType;
}
/**
* @return Returns the host.
*/
public String getHost() {
return host;
}
/**
* @return Returns the port.
*/
public String getPort() {
return port;
}
/**
* setzt den Namen des Verzeichnisses f�r zwischengespeicherte eMails
* @param queueDir String Verzeichnis zum Zwischenspeichern von eMails
*/
public void setQueueDir(String queueDir) {
this.queueDir = queueDir;
}
/**
* liefert den Namen des Verzeichnisses f�r zwischengespeicherte eMails
*/
public String getQueueDir() {
return this.queueDir;
}
/**
* setzt den Inhalt des Betreffs
* @param subject Betreff
*/
public void setSubject(String subject) {
this.subject = subject;
warn ("subject",subject);
changed=true;
}
/**
* liefert den Inhalt des Betreffs
*/
public String getSubject() {
return this.subject;
}
/**
* setzt die eMail-Adresse des Absenders
* @param from Absender der eMail
*/
public void setFrom(String from) {
this.from = from;
warn ("from",from);
changed=true;
}
/**
* liefert die eMail-Adresse des Absenders
*/
public String getFrom() {
return this.from;
}
/**
* liefert den Namen des Absenders
*/
public String getFromName() {
return this.fromName;
}
/**
* setzt den Namen des Absenders
* @param fromName Absender der eMail
*/
public void setFromName(String fromName) {
this.fromName = fromName;
warn ("fromName",fromName);
changed=true;
}
/**
* setzt die eMail-Adresse f�r R�ckantworten
* @param replyTo Empf�nger f�r R�ckantworten
*/
public void setReplyTo(String replyTo) {
this.replyTo = replyTo;
warn ("replyTo",replyTo);
changed=true;
}
/**
* liefert die eMail-Adresse f�r R�ckantworten
*/
public String getReplyTo() {
return this.replyTo;
}
public void setBody(String body) {
this.body = body;
warn ("body",body);
changed=true;
}
/**
* liefert den Inhalt des eMail-Texts
*/
public String getBody() {
return this.body;
}
/**
* setzt den Namen der Applikation fr eMail-Einstellungen in der Tabelle der Einstellungen
* @param applicationMail Name der Applikation fr eMail-Einstellungen in der Einstellungstabelle
*/
public void setApplicationMail(String applicationMail) {
this.applicationMail = applicationMail;
}
/**
* liefert den Namen der Applikation fr eMail-Einstellungen in der Tabelle der Einstellungen
*/
public String getApplicationMail() {
return this.applicationMail;
}
/**
* setzt den Namen der Sektion fr eMail-Einstellungen in der Tabelle der Einstellungen
* @param sectionMail Name der Sektion fr eMail-Einstellungen in der Einstellungstabelle
*/
public void setSectionMail(String sectionMail) {
this.sectionMail = sectionMail;
}
/**
* liefert den Namen der Sektion fr eMail-Einstellungen in der Tabelle der Einstellungen
*/
public String getSectionMail() {
return this.sectionMail;
}
/**
* setzt den Namen der Applikation fr Mustertexte in der Tabelle der Einstellungen
* @param applicationMailTemplates Name der Applikation fr eMail-Muster in der Einstellungstabelle
*/
public void setApplicationMailTemplates(String applicationMailTemplates) {
this.applicationMailTemplates = applicationMailTemplates;
}
/**
* liefert den Namen der Applikation fr Mustertexte in der Tabelle der Einstellungen
*/
public String getApplicationMailTemplates() {
return this.applicationMailTemplates;
}
/**
* setzt den Namen der Applikation fr eMail Vorlagen der Factory in der Tabelle der Einstellungen
* @param applicationMailTemplates Name der Applikation fr eMail Vorlagen der Factory in der Einstellungstabelle
*/
public void setApplicationMailTemplatesFactory(String applicationMailTemplates) {
this.applicationMailTemplatesFactory = applicationMailTemplates;
}
/**
* liefert den Namen der Applikation f�r eMail Vorlagen der Factory in der Tabelle der Einstellungen
*/
public String getApplicationMailTemplatesFactory() {
return this.applicationMailTemplatesFactory;
}
/**
* setzt den Namen der Sektion fr Mustertexte in der Tabelle der Einstellungen
* @param sectionMailTemplates Name der Sektion fr eMail-Muster in der Einstellungstabelle
*/
public void setSectionMailTemplates(String sectionMailTemplates) {
this.sectionMailTemplates = sectionMailTemplates;
}
/**
* liefert den Namen der Sektion fr Mustertexte in der Tabelle der Einstellungen
*/
public String getSectionMailTemplates() {
return this.sectionMailTemplates;
}
/**
* setzt den Namen der Sektion f�r eMail Vorlagen der Factory in der Tabelle der Einstellungen
* @param sectionMailTemplates Name der Sektion f�r eMail Vorlagen der Factory in der Einstellungstabelle
*/
public void setSectionMailTemplatesFactory(String sectionMailTemplatesFactory) {
this.sectionMailTemplatesFactory = sectionMailTemplatesFactory;
}
/**
* liefert den Namen der Sektion f�r eMail Vorlagen der Factory in der Tabelle der Einstellungen
*/
public String getSectionMailTemplatesFactory() {
return this.sectionMailTemplatesFactory;
}
/**
* setzt den Namen der Sektion f�r Scripte in der Tabelle der Einstellungen
* @param sectionMailTemplates Name der Sektion f�r eMail-Scripte in der Einstellungstabelle
*/
public void setSectionMailScripts(String sectionMailScripts) {
this.sectionMailScripts = sectionMailScripts;
}
/**
* liefert den Namen der Sektion f�r eMail Scripte in der Tabelle der Einstellungen
*/
public String getSectionMailScripts() {
return this.sectionMailScripts;
}
/**
* setzt den Namen der Applikation f�r Scripte in der Tabelle der Einstellungen
* @param sectionMailTemplates Name der Applikation f�r eMail-Scripte in der Einstellungstabelle
*/
public void setApplicationMailScripts(String applicationMailScripts) {
this.applicationMailScripts = applicationMailScripts;
}
/**
* liefert den Namen der Applikation f�r eMail Scripte in der Tabelle der Einstellungen
*/
public String getApplicationMailScripts() {
return this.applicationMailScripts;
}
/**
* liefert den Namen der Tabelle mit Einstellungen
* @param tableSettings
*/
public void setTableSettings(String tableSettings) {
this.tableSettings = tableSettings;
}
/**
* liefert den Namen der Tabelle mit Einstellungen
*/
public String getTableSettings() {
return this.tableSettings;
}
/**
* @return Returns the messageBytes.
*/
public byte[] getMessageBytes() {
return messageBytes;
}
/**
* Email nicht senden sondern als bytes [] verf�gbar machen (message_bytes)
* @param sendToOutputStream The sendToOutputStream to set.
*/
public void setSendToOutputStream(boolean sendToOutputStream) {
this.sendToOutputStream = sendToOutputStream;
}
/**
* @param attachmentEncoding The attachmentEncoding to set.
*/
public void setattachmentEncoding(String attachmentEncoding) {
this.attachmentEncoding = attachmentEncoding;
warn ("attachmentEncoding",attachmentEncoding);
changed=true;
}
/**
* @return Returns the message.
*/
public MimeMessage getMessage() {
return message;
}
/**
* @return Returns the lastError.
*/
public String getLastError() {
return lastError;
}
/**
* @return Returns the attachmentEncoding.
*/
public String getAttachmentEncoding() {
return attachmentEncoding;
}
/**
* @return Returns the attachmentCharset.
*/
public String getAttachmentCharset() {
return attachmentCharset;
}
/**
* @param attachmentCharset The attachmentCharset to set.
*/
public void setAttachmentCharset(String attachmentCharset) {
this.attachmentCharset = attachmentCharset;
warn ("attachmentCharset",attachmentCharset);
changed=true;
}
/**
* @param attachmentEncoding The attachmentEncoding to set.
*/
public void setAttachmentEncoding(String attachmentEncoding) {
this.attachmentEncoding = attachmentEncoding;
warn ("attachmentEncoding",attachmentEncoding);
changed=true;
}
public void setHost(String host) throws Exception {
this.host = host;
this.initMessage();
}
public void setPassword(String password) throws Exception {
this.password = password;
this.initMessage();
}
public void setUser(String user) throws Exception {
this.user = user;
this.initMessage();
}
public void setPort(String port) throws Exception {
this.port = port;
this.initMessage();
}
public void setPriorityHighest() throws MessagingException{
message.setHeader("X-Priority","1 (Highest)");
message.setHeader("X-MSMail-Priority","Highest");
message.setHeader("Priority","urgent");
changed=true;
}
public void setPriorityHigh() throws MessagingException{
message.setHeader("X-Priority","2 (High)");
message.setHeader("X-MSMail-Priority","Highest");
message.setHeader("Priority","urgent");
changed=true;
}
public void setPriorityNormal() throws MessagingException{
message.setHeader("X-Priority","3 (Normal)");
message.setHeader("X-MSMail-Priority","Normal");
message.setHeader("Priority","normal");
changed=true;
}
public void setPriorityLow() throws MessagingException{
message.setHeader("X-Priority","4 (Low)");
message.setHeader("X-MSMail-Priority","Low");
message.setHeader("Priority","non-urgent");
changed=true;
}
public void setPriorityLowest() throws MessagingException{
message.setHeader("X-Priority","5 (Lowest)");
message.setHeader("X-MSMail-Priority","Low");
message.setHeader("Priority","non-urgent");
changed=true;
}
public void setAlternativeBody(String alternativeBody) {
this.alternativeBody = alternativeBody;
}
public void setAlternativeCharset(String alternativeCharset) {
this.alternativeCharset = alternativeCharset;
}
public void setAlternativeContentType(String alternativeContentType) {
this.alternativeContentType = alternativeContentType;
}
public String getQueuePraefix() {
return queuePraefix;
}
public String getLastGeneratedFileName() {
return lastGeneratedFileName;
}
public void setQueuePraefix(String queuePraefix) {
this.queuePraefix = queuePraefix;
}
public String getLoadedMessageId() {
return loadedMessageId;
}
/**
* @param sosLogger The sosLogger to set.
*/
public void setSOSLogger(SOSLogger sosLogger) {
this.sosLogger = sosLogger;
}
private void warn(String n,String v) {
if (this.messageReady){
try {
log("...setting of " + n+"="+v + " will not be used. Loaded Message will be sent unchanged.", SOSStandardLogger.WARN);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* Nachricht mit nativen Mail-Funktionen versenden
*
* @throws java.lang.Exception
* @deprecated Not further supported
*/
public String sendNative() throws Exception {
Socket socket = null;
String boundary = "DataSeparatorString";
StringBuffer sb = new StringBuffer("");
try {
if (this.host == null || this.host.length() == 0) {
throw new Exception(
SOSClassUtil.getMethodName() + ": host has no value.");
}
if (this.port == null || this.port.length() == 0) {
throw new Exception(
SOSClassUtil.getMethodName() + ": port has no value.");
}
if (this.toList.isEmpty()) {
throw new Exception(
SOSClassUtil.getMethodName() + ": no recipient specified.");
}
if (this.from == null || this.from.length() == 0) {
throw new Exception(
SOSClassUtil.getMethodName() + ": no sender specified.");
}
socket = new Socket(this.host, Integer.parseInt(this.port));
BufferedReader in = new BufferedReader
(new InputStreamReader(socket.getInputStream(), "8859_1"));
BufferedWriter out = new BufferedWriter
(new OutputStreamWriter(socket.getOutputStream(), "8859_1"));
sendLine(in, out, "HELO " + this.host);
if ( this.fromName != null && this.fromName.length() > 0
&& this.from != null && this.from.length() > 0 ) {
sendLine(in, out, "MAIL FROM: " + this.fromName + getQuotedName(this.from));
} else if ( this.from != null && this.from.length() > 0 ){
sendLine(in, out, "MAIL FROM: " + getQuotedName(this.from));
}
// if (!this.toList.isEmpty()) {
// for (Enumeration e = this.toList.elements(); e.hasMoreElements();) {
// sendLine(in, out, "RCPT TO: " + getQuotedName(e.nextElement().toString()) );
// }
// }
sendLine(in, out, "DATA");
sendLine(out, "MIME-Version: 1.0");
if ( this.fromName != null && this.fromName.length() > 0
&& this.from != null && this.from.length() > 0 ) {
sendLine(out, "From: " + this.fromName + getQuotedName(this.from));
} else if ( this.from != null && this.from.length() > 0 ){
sendLine(out, "From: " + getQuotedName(this.from));
}
if (this.replyTo != null && this.replyTo.length() > 0) {
sendLine(out, "Reply-To: " + getQuotedName(this.replyTo));
}
if (!this.toList.isEmpty()) {
sb = new StringBuffer("");
for (ListIterator e = this.toList.listIterator(); e.hasNext();) {
sb.append(getQuotedName(e.next().toString()));
if (e.hasNext()) {
sb.append(",");
}
}
sendLine(out, "To: " + sb);
}
if (!this.ccList.isEmpty()) {
sb = new StringBuffer("");
for (ListIterator e = this.ccList.listIterator(); e.hasNext();) {
sb.append(getQuotedName(e.next().toString()));
if (e.hasNext()) {
sb.append(",");
}
}
sendLine(out, "Cc: " + sb);
}
if (!this.bccList.isEmpty()) {
sb = new StringBuffer("");
for (ListIterator e = this.bccList.listIterator(); e.hasNext(); ) {
sb.append(getQuotedName(e.next().toString()));
if (e.hasNext()) {
sb.append(",");
}
}
sendLine(out, "Bcc: " + sb);
}
if (this.subject != null) {
sendLine(out, "Subject: " + this.subject);
}
sendLine(out, "Content-Type: multipart/mixed; boundary=\"" + boundary + "\"");
sendLine(out, "\r\n--" + boundary);
// send the body
if (this.contentType != null && this.contentType.length() > 0) {
sendLine(out, "Content-Type: text/html; charset=\"" + this.charset + "\"");
}
if (this.encoding != null) {
sendLine(out, "Content-Transfer-Encoding: " + this.encoding);
}
sendLine(out, "\r\n" + this.body + "\r\n\r\n");
// send the attachments
if (!this.attachmentList.isEmpty()) {
for (Iterator i = this.attachmentList.values().iterator();
i.hasNext();
) {
String[] attachment = new String[2];
attachment = (String[]) i.next();
sendLine(out, "\r\n--" + boundary );
sendLine(out, "Content-Type: " + attachment[1] + "; name=\"" + new File(attachment[0]).getName() + "\"");
sendLine(out, "Content-Disposition: attachment; filename=\"" + attachment[0] + "\"");
sendLine(out, "Content-Transfer-Encoding: " + this.attachmentEncoding + "\r\n");
SOSMimeBase64.encode(attachment[0], out);
}
}
sendLine(out, "\r\n\r\n--" + boundary + "--\r\n");
sendLine(in, out, ".");
sendLine(in, out, "QUIT");
} catch (Exception e) {
throw new Exception(
SOSClassUtil.getMethodName() + ": error occurred on send: " + e.toString() );
} finally {
if ( socket != null ) { socket.close(); }
}
return sb.toString();
}
/**
* Nachricht versenden mit hostWare
*
* @deprecated Not further supported
* @throws java.lang.Exception
*/
public String sendHostware() throws Exception {
sos.hostware.File file = new sos.hostware.File();
StringBuffer sb;
try {
sb = new StringBuffer("-out mail ");
if (this.host != null && this.port != null) {
sb.append("-smtp=" + this.host + ":" + this.port + " ");
} else if (this.host != null) {
sb.append("-smtp=" + this.host + " ");
}
if ( this.fromName != null && this.fromName.length() > 0
&& this.from != null && this.from.length() > 0 ) {
sb.append("-from='" + this.fromName + "<" + this.from + ">' ");
} else if ( this.from != null && this.from.length() > 0 ){
sb.append("-from='" + this.from + "' ");
}
if (!this.toList.isEmpty()) {
sb.append("-to='");
for (ListIterator e = this.toList.listIterator();
e.hasNext();
) {
sb.append(e.next());
if (e.hasNext()) {
sb.append(",");
}
}
sb.append("' ");
}
if (!this.ccList.isEmpty()) {
sb.append("-cc='");
for (ListIterator e = this.ccList.listIterator();
e.hasNext();
) {
sb.append(e.next());
if (e.hasNext()) {
sb.append(",");
}
}
sb.append("' ");
}
if (!this.bccList.isEmpty()) {
sb.append("-bcc='");
for (ListIterator e = this.bccList.listIterator();
e.hasNext();
) {
sb.append(e.next());
if (e.hasNext()) {
sb.append(",");
}
}
sb.append("' ");
}
if (this.replyTo != null) {
sb.append("-reply-to='" + this.replyTo + "' ");
}
if (this.subject != null) {
sb.append("-subject='" + this.subject + "' ");
}
if (this.filename != null) {
sb.append("-filename='" + this.filename + "' ");
}
if (this.encoding != null) {
sb.append("-encoding='" + this.encoding + "' ");
}
// not supported
if (this.contentType != null && this.attachmentList.isEmpty()) {
sb.append("-content-type='" + this.contentType + "' ");
}
// if (this.autoDequeue ) {
// automatic dequeueing not supported
// sb.append("-dequeue-mail ");
// }
if (this.queueDir != null) {
sb.append("-queue-dir='" + this.queueDir + "' ");
}
if (!this.attachmentList.isEmpty()) {
for (Iterator i = this.attachmentList.values().iterator();
i.hasNext();
) {
String[] attachment = new String[2];
attachment = (String[]) i.next();
sb.append(
"-attach='(-content-type="
+ attachment[1]
+ " "
+ attachment[0]
+ ")' ");
}
}
file.open(sb.toString());
file.put_line(this.body);
file.close();
} catch (Exception e) {
throw new Exception(
SOSClassUtil.getMethodName() + ": error occurred on send: " + e.toString() );
} finally {
if (file != null)
file.destruct();
}
return sb.toString();
}
public static void main(String[] args) throws Exception {
// SOSMail sosMail = new SOSMail("dod.sos","25","sos","sos");
SOSMail sosMail = new SOSMail("smtp.sos");
sosMail.setPriorityLowest();
sosMail.setQueueDir("c:/");
sosMail.setFrom("xyz@sos-berlin.com");
// sosMail.setContentType("text/plain");
sosMail.setEncoding("8bit");
sosMail.setattachmentEncoding("Base64");
//sosMail.addRecipient("xyz@sos-berlin.com");
// sosMail.setCharset("us-ascii");
sosMail.setSubject("Betreff");
sosMail.setReplyTo("xyz@sos-berlin.com");
String s = "Hello\\nWorld";
sosMail.setBody(s);
sosMail.addRecipient("xyz@sos-berlin.com");
//sosMail.addAttachment("c:\\windows\\sos.ini");
// sosMail.setAlternativeBody("Nachricht2");
// sosMail.dumpMessageToFile(new File("c:/3.msg"),true);
SOSStandardLogger sosLogger = new SOSStandardLogger(SOSStandardLogger.DEBUG9);
sosMail.setSOSLogger(sosLogger);
sosMail.setPriorityLowest();
//sosMail.getMessage().setHeader("Return-Path","<>");
//sosMail.getMessage().setHeader("X-SOSMAIL-delivery-counter","1");
if (!sosMail.send()){
sosMail.log(SOSClassUtil.getMethodName() + "-->" + sosMail.getLastError(),SOSLogger.WARN);
}else{
//sosMail.dumpMessageToFile(new File("c:/4.msg"),false);
//sosMail.log("==>Original-Message-id Vergleiche 4.msg:" + sosMail.getMessage().getMessageID(),SOSStandardLogger.INFO);
}
sosMail.clearRecipients();
}
}