From cb5259f0dc8bec81c3f2ea1f24f58045c4385b35 Mon Sep 17 00:00:00 2001 From: eric Date: Thu, 15 Jan 2004 16:56:02 +0000 Subject: [PATCH] Ok, lets just fall back to trying the explicit path of where sendmail should if we don't find it in the env or otherwise choke horribly when we just try to use 'sendmail' by itself. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1371 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../src/java/com/samskivert/net/MailUtil.java | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/projects/samskivert/src/java/com/samskivert/net/MailUtil.java b/projects/samskivert/src/java/com/samskivert/net/MailUtil.java index eea9dc80..df6809f3 100644 --- a/projects/samskivert/src/java/com/samskivert/net/MailUtil.java +++ b/projects/samskivert/src/java/com/samskivert/net/MailUtil.java @@ -1,5 +1,5 @@ // -// $Id: MailUtil.java,v 1.6 2003/08/15 22:35:57 mdb Exp $ +// $Id: MailUtil.java,v 1.7 2004/01/15 16:56:02 eric Exp $ // // samskivert library - useful routines for java programs // Copyright (C) 2001 Michael Bayne @@ -58,7 +58,14 @@ public class MailUtil public static boolean deliverMail (String message) throws IOException { - Process p = Runtime.getRuntime().exec(SENDMAIL_COMMAND); + Process p; + try { + p = Runtime.getRuntime().exec(SENDMAIL_COMMAND); + } catch (Exception e) { + // try try again, with the full path. Environment be damned! + p = Runtime.getRuntime().exec(FULL_SENDMAIL_COMMAND); + } + PrintWriter writer = new PrintWriter(p.getOutputStream()); writer.print(message); writer.flush(); @@ -83,15 +90,13 @@ public class MailUtil return _emailre.matcher(address).matches(); } - public static void main (String[] args) - { - String address = "mdb@samskivert.com"; - 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"; + /** Full path to the command to execute the program to send mail. */ + protected static final String FULL_SENDMAIL_COMMAND = + "/usr/sbin/sendmail -t"; + /** Originally formulated by lambert@nas.nasa.gov. */ protected static final String EMAIL_REGEX = "^([-A-Za-z0-9_.!%+]+@" + "[-a-zA-Z0-9]+(\\.[-a-zA-Z0-9]+)*\\.[-a-zA-Z0-9]+)$"; @@ -106,4 +111,16 @@ public class MailUtil Log.logStackTrace(pse); } } + + public static void main (String[] args) + { + String msg = "To: eric@threerings.com, eric@radiantenergy.org\n" + + "Subject: test\n\n" + + "Body of message is here. Yiza!\n"; + try { + MailUtil.deliverMail(msg); + } catch (Exception e) { + System.err.println("Le Booch Sending Mail [exception=" + e + "]."); + } + } }