From a2f6c52828a5162437410ddfe8b7e4ebef36cfed Mon Sep 17 00:00:00 2001 From: mdb Date: Sat, 26 May 2001 07:08:00 +0000 Subject: [PATCH] Created a utility class for email related stuff. Currently supports (primitive) delivery and address syntax checking. git-svn-id: https://samskivert.googlecode.com/svn/trunk@122 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../src/java/com/samskivert/net/MailUtil.java | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 projects/samskivert/src/java/com/samskivert/net/MailUtil.java diff --git a/projects/samskivert/src/java/com/samskivert/net/MailUtil.java b/projects/samskivert/src/java/com/samskivert/net/MailUtil.java new file mode 100644 index 00000000..c53631ca --- /dev/null +++ b/projects/samskivert/src/java/com/samskivert/net/MailUtil.java @@ -0,0 +1,71 @@ +// +// $Id: MailUtil.java,v 1.1 2001/05/26 07:08:00 mdb Exp $ + +package com.samskivert.net; + +import java.io.IOException; +import java.io.PrintWriter; + +import org.apache.regexp.*; +import com.samskivert.Log; + +/** + * The mail util class encapsulates some utility functions related to + * sending emails. The mail delivery mechanism depends on being able to + * invoke sendmail which probably prevents it from working on + * anything other than Unix, but maybe some day someone will care enough + * to make the code do whatever one does on Windows to deliver mail. I + * don't have the foggiest what that is, so I can't do that currently. + */ +public class MailUtil +{ + /** + * Delivers the supplied mail message. The message should already + * contain the mail headers and sendmail will figure everything out + * from those. At a minimum, these headers should be included: + * + *
+     * From: (sender)
+     * To: (recipient)
+     * Subject: (subject)
+     * 
+ */ + public void deliverMail (String message) + throws IOException + { + Process p = Runtime.getRuntime().exec("sendmail -t"); + PrintWriter writer = new PrintWriter(p.getOutputStream()); + writer.print(message); + writer.flush(); + writer.close(); + + // some day we should read the response from the mailer to check + // for errors and also check the Process object for a valid return + // code. for now we're flying blind! + } + + /** + * Checks the supplied address against a regular expression that (for + * the most part) matches only valid email addresses. It's not + * perfect, but the ommissions and inclusions are so obscure that we + * can't be bothered to worry about them. + */ + public static boolean isValidAddress (String address) + { + return _emailre.match(address); + } + + /** 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]+)$"; + + /** Used to check email addresses for validity. */ + protected static RE _emailre; + static { + try { + _emailre = new RE(EMAIL_REGEX); + } catch (RESyntaxException rese) { + Log.warning("Unable to initialize email regexp?! " + rese); + } + } +}