Source code: org/altara/mars/plugin/mailnotify/SimpleSmtpClient.java
1 /* MARS Mail Notification Plugin
2 Copyright (C) 2002 Leapfrog Research & Development, LLC
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, it is available at
16 http:///www.gnu.org/copyleft/gpl.html, or by writing to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19 */
20
21 package org.altara.mars.plugin.mailnotify;
22
23 import org.altara.mars.*;
24 import org.altara.mars.engine.*;
25 import java.util.*;
26 import java.net.*;
27 import java.io.*;
28 import org.apache.oro.text.regex.*;
29
30 public class SimpleSmtpClient extends SendExpectClient {
31
32 private static final long SMTP_TIMEOUT = 120000;
33 private static final int SMTP_PORT = 25;
34
35 protected static Perl5Compiler recompiler = new Perl5Compiler();
36 private Perl5Pattern failpat;
37
38 public SimpleSmtpClient(InetAddress server) {
39 super(server,SMTP_PORT,SMTP_TIMEOUT);
40
41 try {
42 failpat = (Perl5Pattern)recompiler.compile("^[45]\\d{2}");
43 } catch (MalformedPatternException ex) {
44 throw new RuntimeException("error in smtp client: "+
45 "regex compile failed");
46 }
47 }
48
49 public Status sendMessage(String from, String to,
50 String subject, String message) {
51 // clear the script
52 clear();
53 // say hello
54 expect("220",failpat);
55 send("HELO ");
56 send(new SendLocalHostname());
57 send("\n");
58 expect("250",failpat);
59 // set from address
60 send("MAIL From: <"+from+">\n");
61 expect("250",failpat);
62 // send recipient list
63 StringTokenizer totok = new StringTokenizer(to,",");
64 while (totok.hasMoreTokens()) {
65 String nextto = totok.nextToken();
66 send("RCPT To: <"+nextto+">\n");
67 expect("250",failpat);
68 }
69 // send message
70 send("DATA\n");
71 expect("354",failpat);
72 send("From: "+from+"\n");
73 send("To: "+to+"\n");
74 send("Subject: "+subject+"\n\n");
75 send(message);
76 send("\n.\n");
77 expect("250",failpat);
78 // clean up
79 send("QUIT\n");
80 expect("221",failpat);
81 // now run the script
82 return runScript();
83 }
84 }