www.pudn.com > mailfetcher2.rar > SmtpMailer.java
package mfetcher;
//
// SmtpMailer
//
//
// Copyright (C) 1999 John Mettraux
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
import java.io.*;
import java.net.*;
import java.util.StringTokenizer;
/**
* Instantiate an SmtpMailer class to keep in touch with a SMTP server.
* <br>Use sendMail() methods to send your messages.
* <br>At each call of a sendMail() method, connection is established to server, messages
* are sent and then connection is closed.
*
* <p>You can set any reply address you want ;-)
* <br>but don't forget that route details are added
* to message header by each MTA (Mail Transfer Agent) it goes through.
*
* @author John Mettraux
*/
public class SmtpMailer
{
//
// DATAS
public static PrintWriter commLog = null;
private String smtpServer;
private int smtpPort;
private String sourceName; // "from:" field
private PrintWriter toServer;
private BufferedReader fromServer;
private int targets;
String version = null;
//
// CONSTRUCTORS
/**
* Easy constructor. Port defaults to smtp's well known 25
*
* @param smtpServer host running a smtp daemon for mail delivery
* @param sourceName email address of the message sender
*/
public SmtpMailer (String smtpServer, String sourceName)
{
this(smtpServer, 25, sourceName);
}
/**
* Full constructor, you can set port.
*
* @param smtpServer host running a smtp daemon for mail delivery
* @param port which port on server runs the smtp daemon
* @param sourceName email address of the message sender
*/
public SmtpMailer (String smtpServer, int port, String sourceName)
{
this.smtpServer = smtpServer;
this.smtpPort = port;
this.sourceName = sourceName;
targets = 0;
}
//
// METHODS
private void send (String cmd)
{
if (toServer == null)
return;
if (commLog != null) {
commLog.println(cmd);
commLog.flush();
}
toServer.print(cmd+"\r\n");
toServer.flush();
}
private void sendData (String cmd)
{
if (toServer == null)
return;
if (commLog != null) {
//commLog.println(cmd);
//commLog.flush();
}
toServer.print(cmd+"\r\n");
//toServer.flush();
}
private String getResponse ()
//throws IOException
{
String line;
do {
try {
line = fromServer.readLine();
}
catch (IOException e) {
line = null;
}
if (commLog != null) {
if (line==null)
commLog.println("<null>");
else
commLog.println(line);
commLog.flush();
}
} while ((line != null) &amt;&amt; (line.charAt(3)=='-'));
return line;
}
private void throwSmtpException (String res, String[] messageBody)
throws IOException, MessageException
{
fromServer.close();
toServer.close();
throw new MessageException(res, messageBody);
}
//
// PUBLIC METHODS
/**
* Call this method just after sendmail()
* it will return the nb of targets (to + cc + bcc)
* that the message has been sent to
*
* @return the number of targets
*/
public int getTargets ()
{
return targets;
}
public void sendMail (String recipient, String[] messageBody)
throws IOException, MessageException
{
sendMail(new String[] {recipient}, messageBody);
}
public void sendMail
(String[] recipients,
String[] messageBody)
throws IOException, MessageException
{
try {
targets = 0;
//
// connect to smtpServer
Socket s = new Socket(smtpServer, smtpPort);
fromServer = new BufferedReader(new InputStreamReader(s.getInputStream()));
toServer = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
//
// get header
String res = getResponse();
//
// HELO
send("HELO "+smtpServer);
//send("EHLO "+smtpServer);
res = getResponse();
if (!res.startsWith("250"))
throwSmtpException("HELO "+smtpServer+"\n-> "+res, messageBody);
//
// MAIL FROM
String from = extractFrom(sourceName);
if (from == null)
from = "MailFetcher";
send("MAIL FROM:<"+from+">");
res = getResponse();
if (!res.startsWith("250"))
throwSmtpException("MAIL FROM:<"+from+">\n-> "+res, messageBody);
//
// RCPT TO
for (int i=0; i<recipients.length; i++) {
send("RCPT TO:<"+recipients[i]+">");
res = getResponse();
if (res.startsWith("550")) {
//
// 500 <xxx> user unknown
} else {
targets++;
}
}
if (targets == 0) {
//
// no targets available...
throwSmtpException("No targets available...", messageBody);
}
//
// DATA
send("DATA");
res = getResponse();
if (!res.startsWith("354"))
throwSmtpException("DATA\n-> "+res, messageBody);
//
// APPEND TO MESSAGE HEADER
//
// X-Mailer
sendData("X-mailer: "+version);
//
// SEND MESSAGE BODY &amt; HEADER
for (int i=0; i<messageBody.length; i++) {
String line = messageBody[i];
if (line.length() > 0 &amt;&amt; line.charAt(0) == '.' &amt;&amt;
(line.length() == 1 || line.charAt(1) != '.'))
line = "."+line;
// doubles '.' at beginning of lines, so that SMTP target
// won't believe it is the end of the message
sendData(line);
}
send(".");
res = getResponse();
if (!res.startsWith("250"))
throwSmtpException(res, messageBody);
//
// QUIT
send("QUIT");
getResponse();
//
// release connection
fromServer.close();
toServer.close();
s.close();
} catch (Exception e) {
throw new MessageException(e.toString(),messageBody);
}
}
private String extractFrom (String rawFrom)
//
// just get the email address
// jim <jim@nada.com> becomes jim@nada.com
// $*<$+>$* $2 ???
//
{
StringTokenizer st = new StringTokenizer(rawFrom, " \"<>()'[]");
while(st.hasMoreTokens()) {
String f = st.nextToken();
if (f.indexOf('@') != -1) {
if (f.startsWith("mailto:"))
return f.substring(7);
return f;
}
}
return null;
}
public void setVersion (String version)
{
this.version = version;
}
}