1 package net.jaekl.qd.util;
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
6 import java.io.PrintWriter;
7 import java.net.InetAddress;
8 import java.net.Socket;
9 import java.net.UnknownHostException;
10 import java.util.ArrayList;
11 import java.util.List;
13 public class SendMail {
14 private static final String RESP_220 = "220"; // server ready
15 private static final String RESP_221 = "221"; // goodbye
16 private static final String RESP_250 = "250"; // success
17 private static final String RESP_251 = "251"; // mail address change (we don't support this)
18 private static final String RESP_354 = "354"; // OK, proceed with DATA transmission
27 ArrayList<String> m_to;
28 ArrayList<String> m_cc;
29 ArrayList<String> m_bcc;
30 ArrayList<MimePart> m_part;
36 m_smtpHost = "localhost";
38 m_from = "noreply@localhost";
40 m_to = new ArrayList<String>();
41 m_cc = new ArrayList<String>();
42 m_bcc = new ArrayList<String>();
43 m_part = new ArrayList<MimePart>();
46 public void setSmtpHost(String hostName) { m_smtpHost = hostName; }
47 public void setSmtpPort(int portNum) { m_smtpPort = portNum; }
48 public void setFrom(String addr) { m_from = addr; }
49 public void setSubject(String subject) { m_subject = subject; }
50 public void addTo(String addr) { m_to.add(addr); }
51 public void addCc(String addr) { m_cc.add(addr); }
52 public void addBcc(String addr) { m_bcc.add(addr); }
53 public void addPart(MimePart part) { m_part.add(part); }
55 public void send() throws MailException
58 Socket sock = openSocket(m_smtpHost, m_smtpPort);
59 PrintWriter pw = new PrintWriter(sock.getOutputStream(), true);
60 BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
65 catch (IOException exc) {
66 throw new MailException(exc);
70 Socket openSocket(String host, int port) throws UnknownHostException, IOException {
71 return new Socket(host, port);
74 String getHostName() throws UnknownHostException {
75 if (null == m_hostName) {
76 m_hostName = InetAddress.getLocalHost().getHostName();
81 void send(PrintWriter pw, BufferedReader br) throws MailException
87 line = readResponse(br);
88 validateResponse("", RESP_220, line);
90 cmd = "HELO " + getHostName();
91 line = sendLine(pw, br, cmd);
92 validateResponse(cmd, RESP_250, line);
94 cmd = "MAIL FROM: " + m_from;
95 line = sendLine(pw, br, cmd);
96 validateResponse(cmd, RESP_250, line);
98 sendRcpt(pw, br, m_to);
99 sendRcpt(pw, br, m_cc);
100 sendRcpt(pw, br, m_bcc);
103 line = sendLine(pw, br, cmd);
104 validateResponse(cmd, RESP_354, line);
106 line = sendData(pw, br);
107 validateResponse(cmd, RESP_250, line);
110 line = sendLine(pw, br, cmd);
111 validateResponse(cmd, RESP_221, line);
113 catch (IOException exc) {
114 throw new MailException(exc);
118 // Send the DATA content of the email
119 String sendData(PrintWriter pw, BufferedReader br) throws MailException, IOException
121 sendLine(pw, "From: " + m_from);
122 sendAddressee(pw, "To", m_to);
123 sendAddressee(pw, "Cc", m_cc);
124 sendAddressee(pw, "Bcc", m_bcc);
125 sendLine(pw, "Subject: " + m_subject);
132 String result = sendLine(pw, br, "\r\n.");
136 void sendMimeHeaders(PrintWriter pw)
138 sendLine(pw, "MIME-Version: 1.0");
139 sendLine(pw, "Content-Type: multipart/mixed; boundary=" + getBoundary());
142 void sendMimeParts(PrintWriter pw) {
143 for (MimePart part : m_part) {
144 sendLine(pw, "--" + getBoundary());
145 sendLine(pw, "Content-Type: " + part.getMimeType()); // TODO: Add support for encodings
147 sendLine(pw, part.getContent());
150 sendLine(pw, "--" + getBoundary() + "--");
153 boolean isBoundaryOk(String candidate) {
154 for (MimePart part : m_part) {
155 if (part.getContent().contains(candidate)) {
164 if (null != m_boundary) {
168 StringBuilder sb = new StringBuilder("snip_snip");
169 if (isBoundaryOk(sb.toString())) {
170 return sb.toString();
177 if (isBoundaryOk(sb.toString())) {
178 m_boundary = sb.toString();
190 void sendAddressee(PrintWriter pw, String designator, List<String> addrs) throws MailException
192 for (String addr : addrs) {
193 sendLine(pw, designator + ": " + addr);
197 void validateResponse(String cmd, String expected, String actual) throws MailException
199 if (! actual.startsWith(expected)) {
200 throw new MailException(cmd, expected, actual);
204 void sendRcpt(PrintWriter pw, BufferedReader br, List<String> addrs) throws IOException, MailException
206 for (String to : addrs) {
207 String cmd = "RCPT TO: " + to;
208 String line = sendLine(pw, br, cmd);
209 if (line.startsWith(RESP_251)) {
210 throw new MailException("Apology: no support for 251 mail forwarding response to RCPT command. Mail will not be delivered: " + line);
212 if (!line.startsWith(RESP_250)) {
213 throw new MailException(cmd, RESP_250, line);
219 // RFC 5321 specifies that all response codes are three digits
220 // at the start of a new line.
221 // These digits may optionally be followed by a space and/or text.
222 // If the response is spread over more than one line, then all but
223 // the last line will have a '-' immediately after the 3rd digit.
225 // This function returns true iff. this line is a response code
226 // that is not continued on the next line.
227 boolean isFinalResponseCode(String line)
233 String trimmed = line.trim();
235 if (trimmed.length() < 3) {
238 if (! ( (Character.isDigit(trimmed.charAt(0)))
239 && (Character.isDigit(trimmed.charAt(1)))
240 && (Character.isDigit(trimmed.charAt(2))) ) )
245 if (3 == trimmed.length()) {
249 if (' ' == trimmed.charAt(3)) {
253 if ('-' == trimmed.charAt(3)) {
254 return false; // This is a response code, but it's continued on the next line
257 // This doesn't look like a response code
261 int responseCode(String line)
263 // RFC 5321 specifies that all response codes are three digits
264 // at the start of a new line.
265 // These digits may optionally be followed by a space and/or text.
267 if ((null == line) || (line.length() < 3)) {
271 return Integer.parseInt(line.substring(0, 3));
274 String readResponse(BufferedReader br) throws IOException
278 while (null != (line = br.readLine())) {
279 if (isFinalResponseCode(line)) {
286 void sendLine(PrintWriter pw, String line)
288 pw.write(line + "\r\n");
292 String sendLine(PrintWriter pw, BufferedReader br, String line) throws IOException
295 return readResponse(br);