Created
September 1, 2014 15:53
-
-
Save mss/ff5fe4adaca16f53fe1c to your computer and use it in GitHub Desktop.
JavaMail 1.4.2 Message-ID generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* [...] | |
* | |
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. | |
* | |
* The contents of this file are subject to the terms of either the GNU | |
* General Public License Version 2 only ("GPL") or the Common Development | |
* and Distribution License("CDDL") (collectively, the "License"). [...] | |
*/ | |
package javax.mail.internet; | |
// [...] | |
public class InternetAddress extends Address implements Cloneable { | |
// [...] | |
/** | |
* Return an InternetAddress object representing the current user. | |
* The entire email address may be specified in the "mail.from" | |
* property. If not set, the "mail.user" and "mail.host" properties | |
* are tried. If those are not set, the "user.name" property and | |
* <code>InetAddress.getLocalHost</code> method are tried. | |
* Security exceptions that may occur while accessing this information | |
* are ignored. If it is not possible to determine an email address, | |
* null is returned. | |
* | |
* @param session Session object used for property lookup | |
* @return current user's email address | |
*/ | |
public static InternetAddress getLocalAddress(Session session) { | |
String user=null, host=null, address=null; | |
try { | |
if (session == null) { | |
user = System.getProperty("user.name"); | |
host = InetAddress.getLocalHost().getHostName(); | |
} else { | |
address = session.getProperty("mail.from"); | |
if (address == null) { | |
user = session.getProperty("mail.user"); | |
if (user == null || user.length() == 0) | |
user = session.getProperty("user.name"); | |
if (user == null || user.length() == 0) | |
user = System.getProperty("user.name"); | |
host = session.getProperty("mail.host"); | |
if (host == null || host.length() == 0) { | |
InetAddress me = InetAddress.getLocalHost(); | |
if (me != null) | |
host = me.getHostName(); | |
} | |
} | |
} | |
if (address == null && user != null && user.length() != 0 && | |
host != null && host.length() != 0) | |
address = user + "@" + host; | |
if (address != null) | |
return new InternetAddress(address); | |
} catch (SecurityException sex) { // ignore it | |
} catch (AddressException ex) { // ignore it | |
} catch (UnknownHostException ex) { } // ignore it | |
return null; | |
} | |
// [...] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* [...] | |
* | |
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. | |
* | |
* The contents of this file are subject to the terms of either the GNU | |
* General Public License Version 2 only ("GPL") or the Common Development | |
* and Distribution License("CDDL") (collectively, the "License"). [...] | |
*/ | |
package javax.mail.internet; | |
// [...] | |
class UniqueValue { | |
// [...] | |
/** | |
* Get a unique value for use in a Message-ID. | |
* | |
* This implementation generates it by concatenating a newly | |
* created object's <code>hashCode()</code>, a global ID | |
* (incremented on every use), the current | |
* time (in milliseconds), the string "JavaMail", and | |
* this user's local address generated by | |
* <code>InternetAddress.getLocalAddress()</code>. | |
* (The address defaults to "javamailuser@localhost" if | |
* <code>getLocalAddress()</code> returns null.) | |
* | |
* @param ssn Session object used to get the local address | |
* @see javax.mail.internet.InternetAddress | |
*/ | |
public static String getUniqueMessageIDValue(Session ssn) { | |
String suffix = null; | |
InternetAddress addr = InternetAddress.getLocalAddress(ssn); | |
if (addr != null) | |
suffix = addr.getAddress(); | |
else { | |
suffix = "javamailuser@localhost"; // worst-case default | |
} | |
StringBuffer s = new StringBuffer(); | |
// Unique string is <hashcode>.<id>.<currentTime>.JavaMail.<suffix> | |
s.append(s.hashCode()).append('.').append(getUniqueId()).append('.'). | |
append(System.currentTimeMillis()).append('.'). | |
append("JavaMail."). | |
append(suffix); | |
return s.toString(); | |
} | |
/** | |
* Ensure ID is unique by synchronizing access. | |
* XXX - Could use AtomicInteger.getAndIncrement() in J2SE 5.0. | |
*/ | |
private static synchronized int getUniqueId() { | |
return id++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Unique string is