Report success or failure in deliverMail().

git-svn-id: https://samskivert.googlecode.com/svn/trunk@721 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
shaper
2002-05-02 01:13:35 +00:00
parent d34c1a1011
commit f462d967fa
@@ -1,5 +1,5 @@
// //
// $Id: MailUtil.java,v 1.4 2001/08/11 22:43:28 mdb Exp $ // $Id: MailUtil.java,v 1.5 2002/05/02 01:13:35 shaper Exp $
// //
// samskivert library - useful routines for java programs // samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne // Copyright (C) 2001 Michael Bayne
@@ -23,7 +23,9 @@ package com.samskivert.net;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import org.apache.regexp.*; import org.apache.regexp.RE;
import org.apache.regexp.RESyntaxException;
import com.samskivert.Log; import com.samskivert.Log;
/** /**
@@ -37,34 +39,43 @@ import com.samskivert.Log;
public class MailUtil public class MailUtil
{ {
/** /**
* Delivers the supplied mail message. The message should already * Delivers the supplied mail message and returns whether the mail
* contain the mail headers and sendmail will figure everything out * delivery attempt was successful. The message should already contain
* from those. At a minimum, these headers should be included: * the mail headers and sendmail will figure everything out from
* those. At a minimum, these headers should be included:
* *
* <pre> * <pre>
* From: (sender) * From: (sender)
* To: (recipient) * To: (recipient)
* Subject: (subject) * Subject: (subject)
* </pre> * </pre>
*
* Note that this method will block until the sendmail process has
* completed all of its business.
*
* @return true if the mail was delivered successfully, false if not.
*/ */
public static void deliverMail (String message) public static boolean deliverMail (String message)
throws IOException throws IOException
{ {
Process p = Runtime.getRuntime().exec("sendmail -t"); Process p = Runtime.getRuntime().exec(SENDMAIL_COMMAND);
PrintWriter writer = new PrintWriter(p.getOutputStream()); PrintWriter writer = new PrintWriter(p.getOutputStream());
writer.print(message); writer.print(message);
writer.flush(); writer.flush();
writer.close(); writer.close();
// some day we should read the response from the mailer to check // make sure sendmail and the process both exit cleanly
// for errors and also check the Process object for a valid return try {
// code. for now we're flying blind! return (p.waitFor() == 0);
} catch (InterruptedException ie) {
return false;
}
} }
/** /**
* Checks the supplied address against a regular expression that (for * Checks the supplied address against a regular expression that (for
* the most part) matches only valid email addresses. It's not * the most part) matches only valid email addresses. It's not
* perfect, but the ommissions and inclusions are so obscure that we * perfect, but the omissions and inclusions are so obscure that we
* can't be bothered to worry about them. * can't be bothered to worry about them.
*/ */
public static boolean isValidAddress (String address) public static boolean isValidAddress (String address)
@@ -78,6 +89,9 @@ public class MailUtil
System.out.println(address + ": " + isValidAddress(address)); System.out.println(address + ": " + isValidAddress(address));
} }
/** Command used to execute the program with which mail is sent. */
protected static final String SENDMAIL_COMMAND = "sendmail -t";
/** Originally formulated by lambert@nas.nasa.gov. */ /** Originally formulated by lambert@nas.nasa.gov. */
protected static final String EMAIL_REGEX = "^([-A-Za-z0-9_.!%+]+@" + protected static final String EMAIL_REGEX = "^([-A-Za-z0-9_.!%+]+@" +
"[-a-zA-Z0-9]+(\\.[-a-zA-Z0-9]+)*\\.[-a-zA-Z0-9]+)$"; "[-a-zA-Z0-9]+(\\.[-a-zA-Z0-9]+)*\\.[-a-zA-Z0-9]+)$";