/* * [EmailAddress.java] * * Summary: Allows you to take email adresses apart into their component parts. * * Copyright: (c) 2002-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.7 2007-08-21 */ package com.mindprod.bulk; import javax.mail.internet.InternetAddress; import java.io.UnsupportedEncodingException; import static java.lang.System.*; /** * Allows you to take email adresses apart into their component parts. *

* This class does not validate the format of the address. * For that see the EmailSyntaxValidator class. * Not currently used for anything. * * @author Roedy Green, Canadian Mind Products * @version 1.7 2007-08-21 * @since 2002 */ public final class EmailAddress { /** * the computer email address e.g. g.brown@oberon.ark.com, always lower case. */ private final String email; /** * The human name e.g. "Roedy Green" */ private final String personal; /** * public constructor * * @param email complete computer email address, e.g. g.brown@oberon.ark.com * @param personal human name, e.g. "Mr. George & Mrs. Mary Brown" Strange characters allowed. They will eventuall * be dealt with. */ @SuppressWarnings( { "SameParameterValue" } ) private EmailAddress( String email, String personal ) { this.email = email.trim().toLowerCase(); this.personal = personal.trim(); } /** * Return fancy email address that combines computer and personal name e.g. ""Mr. George & Mrs. Mary Brown" * * * @return combined email address, null if address can't be combined. */ private String getCombinedAddress() { try { return new InternetAddress( email, personal ).toUnicodeString(); } catch ( UnsupportedEncodingException e ) { return null; } } /** * the domain part of the email address. * * @return Everything after the @, e.g. oberon.ark.com possibly null for a malformed address. */ private String getDomain() { int lastAt = email.lastIndexOf( '@' ); if ( lastAt < 0 ) { return null; } return email.substring( lastAt + 1 ); } /** * Get the computer email address: e.g. g.brown@oberon.ark.com * * @return return the complete computer address. without the attached human name. */ private String getEmail() { return email; } /** * Get the name part of the email address. * * @return everything before the @ e.g. g.brown. possibly null for a malformed address. */ private String getName() { int atplace = email.indexOf( '@' ); if ( atplace < 0 ) { return null; } return email.substring( 0, atplace ); } /** * Get the human name, e.g. "Mr. George & Mrs. Mary Brown" * * @return Returns the human name. */ private String getPersonal() { return personal; } /** * Get the Top Level Domain e.g. com net edu * * @return last part of the domain name, without the dot. possibly null for a malformed address. */ private String getTLD() { int lastDot = email.lastIndexOf( '.' ); if ( lastDot < 0 ) { return null; } return email.substring( lastDot + 1 ); } /** * Test driver * * @param args not used */ public static void main( String[] args ) { EmailAddress e = new EmailAddress( "g.brown@oberon.ark.com", "Mr. George & Mrs. Mary Brown" ); out.println( e.getEmail() ); out.println( e.getName() ); out.println( e.getDomain() ); out.println( e.getTLD() ); out.println( e.getPersonal() ); out.println( e.getCombinedAddress() ); } }