import java.util.*;
import java.net.*;
//import msql.*;
import java.io.*;
/**
* *** New in Version 2: ***
* - Stream Input to Mailobject
* - Add MIME Attachements to mail object
*
* SMTP Mailing Object. Allows sending mails to any smtpd. Possibilities to set varios detail information.
* Also, there is detailed exception handling for paranoids. Implements complete SMTP handshake.
* Following Mail structure generated to the smtp server is something like:
*
* HELO* For example: ** MAIL FROM: * RCPT TO: * DATA * From: name_of_from * Sender: name_of_sender * To: name_of_to * Reply-To: name_of_replyto * cc: name_of_cc * cc: name_of_cc (...there can be many cc's...) * bcc: name_of_bcc * bcc: name_of_bcc (...there can be many bcc's...) * (...one delimiting empty line ...) * Text_of_mail * . (the final dot) * QUIT *
* ... * try { * SMTPmail mail = new SMTPmail("whateverhost.wherever.com"); * mail.setFrom("Bob Schulze", "bob@sleepers.com"); * // Brackets are not neccesary here * mail.setTo("Whoever listens", "listener@taxoffice.gov"); * // Subsequent calls will append to the receivers' list * mail.setSubject("Found something..."); * mail.addReplyTo("White House","president@whitehouse.gov") * mail.addFile("/home/incometax.txt"); * mail.sendMail(); * } catch (SMTPException e) { * System.out.println("Something went wrong with your income tax: " + e.getMessage()); * } * ... * ** * Check also: RFC822 (Message Format)
* $Log: SMTPmail.java,v $ * Revision 1.3 1997/06/09 12:14:06 bob * addFile linefeed bug corrected. * * Revision 1.2 1997/06/06 12:28:24 bob * Initial Version: * - Standard Features for general purpose mailing * - Exceptions built in ** @author Bob Schulze */ public class SMTPmail { // global vars String smtp_host = "localhost" ; String s_from_name = "unknown"; String s_from_adr = "unknown@localhost"; String s_sender_name = ""; String s_sender_adr = ""; String s_subject = ""; String s_auth_name = "jSMTPmail"; // all targets are Vectors Vector s_to_name = new Vector(); // To Vector s_to_adr = new Vector(); Vector s_reply_name = new Vector(); // Reply-To Vector s_reply_adr = new Vector(); Vector s_cc_name = new Vector(); // CC Vector s_cc_adr = new Vector(); Vector s_bcc_name = new Vector(); // BCC Vector s_bcc_adr = new Vector(); // text StringBuffer s_text = new StringBuffer(); StringBuffer s_any = new StringBuffer(); // other Socket s = null; PrintStream pout; DataInputStream pin; // debug and verify boolean do_verify = true; boolean do_debug = false; int time_out = 20; //is time times 200 millies private void create_all_vars() { s_to_name.addElement("unknown"); s_to_adr.addElement("root@localhost"); s_reply_name.addElement("unknown"); s_reply_adr.addElement("root@localhost"); s_cc_name.addElement("unknown"); s_cc_adr.addElement("root@localhost"); s_bcc_name.addElement("unknown"); s_bcc_adr.addElement("root@localhost"); } private StringBuffer read_line ( DataInputStream fin ) throws IOException { // read one line get File in Stream StringBuffer line = new StringBuffer(""); char b; while (fin.available() > 0) { b = (char) fin.read(); if ( b == '\n' ) { // one line read break; } line.append(b); } return ( line); } private StringBuffer read_line_f ( FileInputStream fin ) throws IOException { // read one line get File in Stream StringBuffer line = new StringBuffer(""); char b; while (fin.available() > 0) { b = (char) fin.read(); if ( b == '\n' ) { // one line read break; } line.append(b); } return ( line); } /** * Creates a Mail Object that does the whole thing. * @param host The Host name known to the Runtime environment. Use getCodeBase() to find out. * @return The object */ public SMTPmail(String host) { smtp_host = host; create_all_vars(); } /** * Creates a Mail Object that does the whole thing. It will try to connect to the local host's smtpd. * @return The object */ public SMTPmail() { create_all_vars(); } /** * Sets the From Section. Subsequent calls will override. * @param real_name The real name of the User * @param address The address of the User * @return Nothing */ public void setFrom (String real_name, String address) { s_from_name = real_name ; s_from_adr = address; } /** * With verify == true, jSMTPmail will check each response from smtpd carefully. Strongly recommended. Otherwise, smtpd might miss some information and will finally fail delivering the mail. True by default. * @param how true or false * @return Nothing * @see #setTimeOut */ public void setVerify (boolean how) { do_verify = how ; } /** * With debug == true, jSMTPmail will check print out each handshake with smtpd. Useful for debugging or learning. Print out happens only when sendMail is activated. False by default. * @param how true or false * @return Nothing */ public void setDebug (boolean how) { do_debug = how ; } /** * Timeout sets the time, that jSMTPmail waits for the hosts response until it fires a Exception. Use a bigger value on slow hosts. Is set to 20 time 200 ms (4s) by default * @param steps time units of 200 ms, e.g. 20 means 4 seconds * @return Nothing */ public void setTimeOut (int steps) { time_out = steps ; } /** * Sets the To Section. Can be called multiple times in order to Send mail to varios recipients. * @param real_name The real name of the User * @param address The address of the User * @return Nothing */ public void addTo (String real_name, String address) { if ( ((String) s_to_name.elementAt(0)).compareTo("unknown") == 0 ) { // used first time - clean up array s_to_name.removeAllElements(); s_to_adr.removeAllElements(); } s_to_name.addElement(real_name) ; s_to_adr.addElement(address); } /** * Sets the CC Section. Can be called multiple times in order to Send mail to varios recipients. * @param real_name The real name of the User * @param address The address of the User * @return Nothing */ public void addCc (String real_name, String address) { if ( ((String) s_cc_name.elementAt(0)).compareTo("unknown") == 0 ) { // used first time - clean up array s_cc_name.removeAllElements(); s_cc_adr.removeAllElements(); } s_cc_name.addElement(real_name) ; s_cc_adr.addElement(address); } /** * Sets the BCC Section. Can be called multiple times in order to Send mail to varios recipients. * @param real_name The real name of the User * @param address The address of the User * @return Nothing */ public void addBcc (String real_name, String address) { if ( ((String) s_bcc_name.elementAt(0)).compareTo("unknown") == 0 ) { // used first time - clean up array s_bcc_name.removeAllElements(); s_bcc_adr.removeAllElements(); } s_bcc_name.addElement(real_name) ; s_bcc_adr.addElement(address); } /** * Sets the Reply-To Adresses. Can be called multiple times ?? * @param real_name The real name of the User * @param address The address of the User * @return Nothing */ public void addReplyTo (String real_name, String address) { if ( ((String) s_reply_name.elementAt(0)).compareTo("unknown") == 0 ) { // used first time - clean up array s_reply_name.removeAllElements(); s_reply_adr.removeAllElements(); } s_reply_name.addElement(real_name) ; s_reply_adr.addElement(address); } /** * Sets any additional field according to RFC822 like: *
* resent-date = "Resent-Date" ":" date-time * "Resent-To" ":" 1#address * "Resent-cc" ":" 1#address * "Resent-bcc" ":" #address * "Message-ID" ":" msg-id ( msg-id = "<" addr-spec ">" ) * "Resent-Message-ID" ":" msg-id * "In-Reply-To" ":" *(phrase / msg-id) * "References" ":" *(phrase / msg-id) * "Keywords" ":" #phrase * "Subject" ":" *text * "Comments" ":" *text * "Encrypted" ":" 1#2word * extension-field Head with "X-" ** check also: RFC822