View Javadoc
1   package com.randomnoun.common.email;
2   
3   import jakarta.mail.MessagingException;
4   import jakarta.mail.Session;
5   import jakarta.mail.internet.MimeMessage;
6   
7   /* (c) 2013 randomnoun. All Rights Reserved. This work is licensed under a
8    * BSD Simplified License. (http://www.randomnoun.com/bsd-simplified.html)
9    */
10  
11  public class CustomMimeMessage extends MimeMessage {
12  
13  	private String client;
14  	private String suffix;
15  	private static int id = 0;
16  	
17  	/** Create a MimeMessage. Equivalent to mavax.mail.MimeMessage(Session),
18  	 * but allows a different Message-ID to be set.
19  	 * 
20  	 * Normal Message-IDs look like this:
21  	 * <pre>
22  	 * &lt;28405330.11271144052625.JavaMail.knoxg@filament&gt;
23       *                          -client-.----suffix----
24  	 * </pre>
25  	 * but this class allows different values for client and suffix above.
26  	 * 
27  	 * @param session
28  	 * @param client
29  	 * @param suffix
30  	 */
31  	public CustomMimeMessage(Session session, String client, String suffix) {
32  		super(session);
33  		this.client = client;
34  		this.suffix = suffix;
35  	}
36  
37  	/**
38       * Update the Message-ID header.  This method is called
39       * by the <code>updateHeaders</code> and allows a subclass
40       * to override only the algorithm for choosing a Message-ID.
41       *
42       * @since		JavaMail 1.4
43       */
44      protected void updateMessageID() throws MessagingException {
45      	// setHeader("Message-ID", "<" + UniqueValue.getUniqueMessageIDValue(session) + ">");
46      	setHeader("Message-ID", "<" + getUniqueMessageIDValue(session) + ">");
47      }		
48  	
49      public String getUniqueMessageIDValue(Session session) {
50      	// String suffix = null;
51  
52      	/*InternetAddress addr = InternetAddress.getLocalAddress(ssn);
53      	if (addr != null)
54      	    suffix = addr.getAddress();
55      	else {
56      	    suffix = "javamailuser@localhost"; // worst-case default
57      	}
58      	*/
59  
60      	StringBuffer s = new StringBuffer();
61  
62      	// Unique string is <hashcode>.<id>.<currentTime>.JavaMail.<suffix>
63      	s.append(s.hashCode()).append('.').append(getUniqueId()).append('.').
64      	  append(System.currentTimeMillis()).append('.').
65      	  // append("JavaMail.").
66      	  append(client).
67      	  append(suffix);
68      	return s.toString();
69  
70      }
71      
72      private static synchronized int getUniqueId() {
73      	return id++;
74      }
75  
76      
77  }