SmtpTalkpublic class SmtpTalk extends Object implements SysExitsSMTP talker class, usable standalone (as a SendMail(8) backend)
or inside applications such as JabaDex that need to send mail..
OBSOLETE!! Use javax.mail instead, now that it's available!
Needs more parameterization - bit of a hack to start.
Should use same interface as Mailer (subclass, and override doSend()). |
Fields Summary |
---|
BufferedReader | is | PrintStream | os | private boolean | debug | private String | host |
Constructors Summary |
---|
SmtpTalk(String server)Constructor taking a server hostname as argument.
host = server;
try {
Socket s = new Socket(host, 25);
is = new BufferedReader(
new InputStreamReader(s.getInputStream()));
os = new PrintStream(s.getOutputStream());
} catch (NoRouteToHostException e) {
die(EX_TEMPFAIL, "No route to host " + host);
} catch (ConnectException e) {
die(EX_TEMPFAIL, "Connection Refused by " + host);
} catch (UnknownHostException e) {
die(EX_NOHOST, "Unknown host " + host);
} catch (IOException e) {
die(EX_IOERR, "I/O error setting up socket streams\n" + e);
}
|
Methods Summary |
---|
public void | converse(java.lang.String sender, java.lang.String recipients, java.lang.String subject, java.lang.String body)send one Mail message to one or more recipients via smtp
to server "host".
if (!expect_reply("220")) die(EX_PROTOCOL,
"did not get SMTP greeting");
send_cmd("HELO", "darwinsys.com");
if (!expect_reply("250")) die(EX_PROTOCOL,
"did not ack our HELO");
send_cmd("MAIL", "From:<"+sender+">"); // no spaces!
if (!expect_reply("250")) die(EX_PROTOCOL,
"did not ack our MAIL command");
StringTokenizer st = new StringTokenizer(recipients);
while (st.hasMoreTokens()) {
String r = st.nextToken();
send_cmd("RCPT", "To:<" + r + ">");
if (!expect_reply("250")) die(EX_PROTOCOL,
"didn't ack RCPT " + r);
}
send_cmd("DATA");
if (!expect_reply("354")) die(EX_PROTOCOL,"did not want our DATA!");
send_text("From: " + sender);
send_text("To: " + recipients);
send_text("Subject: " + subject);
send_text("");
send_text(body + "\r");
send_cmd(".");
if (!expect_reply("250")) die(EX_PROTOCOL,"Mail not accepted");
send_cmd("QUIT");
if (!expect_reply("221")) die(EX_PROTOCOL,"Other end not closing down");
| protected void | die(int ret, java.lang.String msg)Convenience routine to print message & exit, like
K&P error(), perl die(1,), ...
throw new SMTPException(ret, msg);
| protected boolean | expect_reply(java.lang.String rspNum)Expect (read and check for) a given reply
String s = null;
try {
s = is.readLine();
} catch(IOException e) {
die(EX_IOERR,"I/O error reading from host " + host + " " + e);
}
if (debug) System.out.println("<<< " + s);
return s.startsWith(rspNum + " ");
| public static void | main(java.lang.String[] argv)A simple main program showing the class in action.
TODO generalize to accept From arg, read msg on stdin
if (argv.length != 2) {
System.err.println("Usage: java SmtpTalk host user");
System.exit(EX_USAGE);
}
try {
SmtpTalk st = new SmtpTalk(argv[0]);
System.out.println("SMTP Talker ready");
st.converse("MAILER-DAEMON@daroad.darwinsys.com",
argv[1], "Test message", "Hello there");
} catch (SMTPException ig) {
System.err.println(ig.getMessage());
System.exit(ig.getCode());
}
| protected void | send_cmd(java.lang.String cmd, java.lang.String oprnd)Send a command with an operand
send_cmd(cmd + " " + oprnd);
| protected void | send_cmd(java.lang.String cmd)
if (debug)
System.out.println(">>> " + cmd);
os.print(cmd + "\r\n");
| public void | send_text(java.lang.String text)Send_text sends the body of the message.
os.print(text + "\r\n");
|
|