diff src/fschmidt/util/mail/MailAddress.java @ 68:00520880ad02

add fschmidt source
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 05 Oct 2025 17:24:15 -0600
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/fschmidt/util/mail/MailAddress.java	Sun Oct 05 17:24:15 2025 -0600
@@ -0,0 +1,112 @@
+/*
+Copyright (c) 2008  Franklin Schmidt <fschmidt@gmail.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+package fschmidt.util.mail;
+
+import java.util.regex.Pattern;
+
+public class MailAddress {
+
+    public static final Pattern EMAIL_PATTERN =
+            Pattern.compile("\\b(\\w[-+~.\\w]*)@[-\\w]+(\\.[-\\w]+)*\\.[a-zA-Z]+\\b");
+
+    private final String addrSpec;
+    private final String displayName;
+
+    public MailAddress(String addrSpec) {
+        this(addrSpec,null);
+    }
+
+    public MailAddress(String addrSpec,String displayName) {
+        this.addrSpec = addrSpec;
+        this.displayName = displayName;
+    }
+
+    public String getAddrSpec() {
+        return addrSpec;
+    }
+
+    public String getDisplayName() {
+        return displayName;
+    }
+
+    public String toString() {
+        return displayName==null ? addrSpec : displayName+" <"+addrSpec+">";
+    }
+
+    public boolean equals(Object o) {
+        if( !(o instanceof MailAddress) )
+            return false;
+        MailAddress ma = (MailAddress)o;
+        return addrSpec.equals(ma.addrSpec);
+    }
+
+    public int hashCode() {
+        return addrSpec.hashCode();
+    }
+
+    public boolean isValid() {
+        return EMAIL_PATTERN.matcher(addrSpec).matches() && addrSpec.indexOf("..") == -1;
+    }
+
+    // Test Code ----------------------------------------------------------------------------------
+
+    public static void main(String[] args) {
+        String[] valid = {
+			"none@none.net",
+			"none-owner@none.net",
+			"none+owner@none.net",
+			"none-the-owner@none.net.us",
+			"none.owner@none.net.us",
+			"fwd+lists~2B1264539414801-166331~40in.nabble.com+pharo-project-request~40lists.gforge.inria.fr+-679136591@in.nabble.com"
+		};
+
+        String[] invalid = {
+//			"none-@none.net",  // not sure
+			"none...@none.net",
+			"none@none.net-",
+			"...none@none.net.us...",
+			".-.@---",
+			"none@none+plus.com",
+			"none @none.net",
+			"none@ none.net",
+			"abc.example.com",
+//			"abc.@example.com",  // not sure
+			"abc..123@example.com",
+			"a@b@c@example.com",
+			"()[]\\;:,<>@example.com"
+		};
+
+        System.out.println("--- Valid (Only TRUE)");
+        printEmailValidation(valid);
+
+        System.out.println("--- Invalid (Only FALSE)");
+        printEmailValidation(invalid);
+    }
+
+    private static void printEmailValidation(String[] valid) {
+        for (String s : valid) {
+            MailAddress mailAddress = new MailAddress(s);
+            System.out.println(s + " (" + mailAddress.isValid() + ')');
+        }
+    }
+}