/* * The contents of this file are subject to the Netscape 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/NPL/ * * 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 Netscape Messaging Access SDK Version 3.5 code, * released on or about June 15, 1998. * * The Initial Developer of the Original Code is Netscape Communications * Corporation. Portions created by Netscape are * Copyright (C) 1998 Netscape Communications Corporation. All * Rights Reserved. * * Contributor(s): ______________________________________. */ /* * Copyright (c) 1997 and 1998 Netscape Communications Corporation * (http://home.netscape.com/misc/trademarks.html) */ package netscape.messaging.convenience; import java.io.*; import java.lang.*; import java.util.*; import netscape.messaging.smtp.*; import netscape.messaging.mime.*; /** * The IMTransport class implements Java Convenience APIs. *
This class provides a convenient interface for building, * encoding, and sending messages. It combines the functionality * of MIME and SMTP. * @author Prasad Yendluri */ public class IMTransport { /** * Content (primary) type Text. */ public static final int TEXT = 0; /** * Content (primary) type Audio. */ public static final int AUDIO = 1; /** * Content (primary) type Image. */ public static final int IMAGE = 2; /** * Content (primary) type Video. */ public static final int VIDEO = 3; /** * Content (primary) type Application. */ public static final int APPLICATION = 4; /** * Base64 Transfer Encoding. */ public static final int BASE64 = 0; /** * Quoted Printable Transfer Encoding. */ public static final int QP = 1; /** * No Transfer Encoding. */ public static final int NONE = 2; /** * Content disposition INLINE */ public static final int INLINE = 1; /** * Content disposition ATTACHMENT */ public static final int ATTACHMENT = 2; /** * Default constructor for the IMTransport class. */ //public IMTransport () //{ // // Do any initialization //} /** * Connects to the SMTP transport at the specified host and submits the message. *
NOTE: The MIME Message itself can be created using the Netscape MIME API or * by any other means. * @param host Name of the host to connect to. * @param sender Sender of the message. * @param recipients Email addresses of the recipients of the message, separated by commas or spaces. * @param MIMEMessageStream Input-Stream to the message in MIME canonical form. * @return List of recipients to whom the message could not be submitted. * @exception IMException If the message could not be sent due to invalid host * or other causes. * @see #sendDocuments */ public static String [] sendMessage (String host, String sender, String recipients, InputStream MIMEMessageStream) throws IMException { Vector l_rejrecips; SMTPClient l_SMTPclnt; IMSMTPSink l_smtpSink; String[] l_addrs=null; if (sender == null || sender.length() == 0) { throw new IMException ("Invalid null sender"); } if (host == null || host.length() == 0) { throw new IMException ("Invalid null host"); } if (MIMEMessageStream == null) { throw new IMException ("Invalid InputStream"); } if (recipients != null) l_addrs = parseAddrs (recipients); if (l_addrs == null || l_addrs.length <= 0) { throw new IMException ("Invalid recipients"); } if (recipients != null) l_addrs = parseAddrs (recipients); if (l_addrs == null || l_addrs.length <= 0) { throw new IMException ("Invalid recipients"); } try { l_smtpSink = new IMSMTPSink(); l_SMTPclnt = new SMTPClient (l_smtpSink); l_SMTPclnt.connect (host); if (!l_smtpSink.tryProcessResponses(l_SMTPclnt)) { throw new IMException ("Unable to connect to SMTP host " + host); } l_SMTPclnt.mailFrom (sender, null); if (!l_smtpSink.tryProcessResponses(l_SMTPclnt)) { throw new IMException ("sender rejected by server. Resp Code = " + l_smtpSink.m_failCode); } boolean someFailed = false; boolean allFailed = true; l_rejrecips = new Vector(); for (int i = 0, len = l_addrs.length; i < len; i++) { l_SMTPclnt.rcptTo (l_addrs[i], null); if (!l_smtpSink.tryProcessResponses(l_SMTPclnt)) { someFailed = true; l_rejrecips.addElement (l_addrs[i]); } else allFailed = false; } if (allFailed) { throw new IMException ("Server rejected all recipients"); } l_SMTPclnt.setChunkSize (8192); l_SMTPclnt.data (); if (!l_smtpSink.tryProcessResponses(l_SMTPclnt)) { throw new IMException ("Unable to intiate Data transfer with server. Resp Code = " + l_smtpSink.m_failCode); } l_SMTPclnt.send (MIMEMessageStream); if (!l_smtpSink.tryProcessResponses(l_SMTPclnt)) { throw new IMException ("Data transfer failure! Resp Code = " + l_smtpSink.m_failCode); } l_SMTPclnt.quit(); if (!l_smtpSink.tryProcessResponses(l_SMTPclnt)) { // Ignore! //showFailPopUp("Error!", "Unbale to quit from server! Server code = " // + l_smtpSink.m_failCode); } if (someFailed == true) { if (l_rejrecips == null) return null; String [] rejrecips = new String [l_rejrecips.size()]; for (int i=0; i < l_rejrecips.size(); i++) { rejrecips [i] = (String) l_rejrecips.elementAt (i); } return (rejrecips); } else { return null; } } catch (IOException e) { throw new IMException (e.getMessage()); } } protected static String [] sendMessage2 (String host, String sender, String recipients, PipedInputStream MIMEMessageStream) throws IMException { Vector l_rejrecips; SMTPClient l_SMTPclnt; IMSMTPSink l_smtpSink; String[] l_addrs=null; if (sender == null || sender.length() == 0) { throw new IMException ("Invalid null sender"); } if (host == null || host.length() == 0) { throw new IMException ("Invalid null host"); } if (MIMEMessageStream == null) { throw new IMException ("Invalid InputStream"); } if (recipients != null) l_addrs = parseAddrs (recipients); if (l_addrs == null || l_addrs.length <= 0) { throw new IMException ("Invalid recipients"); } if (recipients != null) l_addrs = parseAddrs (recipients); if (l_addrs == null || l_addrs.length <= 0) { throw new IMException ("Invalid recipients"); } try { l_smtpSink = new IMSMTPSink(); l_SMTPclnt = new SMTPClient (l_smtpSink); l_SMTPclnt.connect (host); if (!l_smtpSink.tryProcessResponses(l_SMTPclnt)) { throw new IMException ("Unable to connect to SMTP host " + host); } l_SMTPclnt.mailFrom (sender, null); if (!l_smtpSink.tryProcessResponses(l_SMTPclnt)) { throw new IMException ("sender rejected by server. Resp Code = " + l_smtpSink.m_failCode); } boolean someFailed = false; boolean allFailed = true; l_rejrecips = new Vector(); for (int i = 0, len = l_addrs.length; i < len; i++) { l_SMTPclnt.rcptTo (l_addrs[i], null); if (!l_smtpSink.tryProcessResponses(l_SMTPclnt)) { someFailed = true; l_rejrecips.addElement (l_addrs[i]); } else allFailed = false; } if (allFailed) { throw new IMException ("Server rejected all recipients"); } l_SMTPclnt.data (); if (!l_smtpSink.tryProcessResponses(l_SMTPclnt)) { throw new IMException ("Unable to intiate Data transfer with server. Resp Code = " + l_smtpSink.m_failCode); } l_SMTPclnt.send (MIMEMessageStream); if (!l_smtpSink.tryProcessResponses(l_SMTPclnt)) { throw new IMException ("Data transfer failure! Resp Code = " + l_smtpSink.m_failCode); } l_SMTPclnt.quit(); if (!l_smtpSink.tryProcessResponses(l_SMTPclnt)) { // Ignore! //showFailPopUp("Error!", "Unbale to quit from server! Server code = " // + l_smtpSink.m_failCode); } if (someFailed == true) { if (l_rejrecips == null) return null; String [] rejrecips = new String [l_rejrecips.size()]; for (int i=0; i < l_rejrecips.size(); i++) { rejrecips [i] = (String) l_rejrecips.elementAt (i); } return (rejrecips); } else { return null; } } catch (IOException e) { throw new IMException (e.getMessage()); } } /** * Builds a MIME message with the specified parameters. Connects to the SMTP transport * at the specified host and submits the message. If the message has more than one * attachment, it is sent as MIME multipart/mixed type message. *
NOTE: This method facilitates mailing documents by mail-enabling an otherwise * mail-ignorant application. For any other uses or more sophisticated needs, * use the sendMessage() method in association with the Netscape MIME API * or the other Messaging APIs provided by Netscape. *
NOTE: If the fUseTempFiles parameter is set to true, sendDocuments uses * temporary intermediate files for some internal processing. This mode provides better * performance. If temporary files should not be created, set this flag to false. * @param host Name of the host to connect to. * @param sender Sender of the message. * @param recipients Email addresses of the recipients of the message, separated by commas or spaces. * @param subject Subject of the Message. Can be null. * @param msgHeaderNames Additional RFC-822 header names. * @param msgHeaderValues Values corresponding to the headers in the msgHeaderNames parameter. * @param fUseTempFiles Whether to use temporary intermediate files for some * internal processing. Values: *