📄 mailmessageheaders.java
字号:
* Add a header with the specified name and value to the header list. <p> * * The current implementation knows about the preferred order of most * well-known headers and will insert headers in that order. In * addition, it knows that <code>Received</code> headers should be * inserted in reverse order (newest before oldest), and that they * should appear at the beginning of the headers, preceeded only by * a possible <code>Return-Path</code> header. <p> * * Note that RFC822 headers can only contain US-ASCII characters. * * @param name header name * @param value header value */ public void addHeader(String name, String value) { int pos = headers.size(); boolean addReverse = name.equalsIgnoreCase("Received") || name.equalsIgnoreCase("Return-Path"); if (addReverse) { pos = 0; } for (int i = headers.size() - 1; i >= 0; i--) { MailMessageHeader h = (MailMessageHeader) headers.get(i); if (name.equalsIgnoreCase(h.getName())) { if (addReverse) { pos = i; } else { headers.add(i + 1, new MailMessageHeader(name, value)); return; } } // marker for default place to add new headers if (h.getName().equals(":")) { pos = i; } } headers.add(pos, new MailMessageHeader(name, value)); } /** * Remove all header entries that match the given name * @param name header name */ public void removeHeader(String name) { for (int i = 0; i < headers.size(); i++) { MailMessageHeader h = (MailMessageHeader) headers.get(i); if (name.equalsIgnoreCase(h.getName())) { h.setLine(null); headers.remove(i); } } } /* * The enumeration object used to enumerate an * InternetHeaders object. Can return * either a String or a Header object. */ static class HeaderMatchEnum implements Enumeration { private Iterator e; // enum object of headers List private String names[]; // names to match, or not private boolean match; // return matching headers? private boolean want_line; // return header lines? private MailMessageHeader next_header; // the next header to be returned /* * Constructor. Initialize the enumeration for the entire * List of headers, the set of headers, whether to return * matching or non-matching headers, and whether to return * header lines or Header objects. */ HeaderMatchEnum(List v, String n[], boolean m, boolean l) { e = v.iterator(); names = n; match = m; want_line = l; next_header = null; } /* * Any more elements in this enumeration? */ public boolean hasMoreElements() { // if necessary, prefetch the next matching header, // and remember it. if (next_header == null) { next_header = nextMatch(); } return next_header != null; } /* * Return the next element. */ public Object nextElement() { if (next_header == null) { next_header = nextMatch(); } if (next_header == null) { throw new NoSuchElementException("No more headers"); } MailMessageHeader h = next_header; next_header = null; if (want_line) { return h.getLine(); } else { return new Header(h.getName(), h.getValue()); } } /* * Return the next Header object according to the match * criteria, or null if none left. */ private MailMessageHeader nextMatch() { next: while (e.hasNext()) { MailMessageHeader h = (MailMessageHeader) e.next(); // skip "place holder" headers if (h.getLine() == null) { continue; } // if no names to match against, return appropriately if (names == null) { return match ? null : h; } // check whether this header matches any of the names for (int i = 0; i < names.length; i++) { if (names[i].equalsIgnoreCase(h.getName())) { if (match) { return h; } else // found a match, but we're // looking for non-matches. // try next header. { continue next; } } } // found no matches. if that's what we wanted, return it. if (!match) { return h; } } return null; } } /** * Return all the headers as an Enumeration of * {@link org.jpxx.mail.Message.Header} objects. * * @return Header objects */ public Enumeration getAllHeaders() { return (new HeaderMatchEnum(headers, null, false, false)); } /** * Return all matching {@link org.jpxx.mail.Message.Header} objects. * * @return matching Header objects */ public Enumeration getMatchingHeaders(String[] names) { return (new HeaderMatchEnum(headers, names, true, false)); } /** * Return all non-matching {@link org.jpxx.mail.Message.Header} objects. * * @return non-matching Header objects */ public Enumeration getNonMatchingHeaders(String[] names) { return (new HeaderMatchEnum(headers, names, false, false)); } /** * Return all the header lines as an Enumeration of Strings. */ public Enumeration getAllHeaderLines() { return (getNonMatchingHeaderLines(null)); } /** * Return all matching header lines as an Enumeration of Strings. */ public Enumeration getMatchingHeaderLines(String[] names) { return (new HeaderMatchEnum(headers, names, true, true)); } /** * Return all non-matching header lines */ public Enumeration getNonMatchingHeaderLines(String[] names) { return (new HeaderMatchEnum(headers, names, false, true)); } /** * Check current message line is a mail header or not * @param name header name * @return is header return true, else return false. */ public boolean isHeader(String name) { String headerArray[] = getDefaultMailMessageHeaderNames(); for (int i = 0; i < headerArray.length; i++) { if(headerArray[i].equals(name)) { return true; } } return false; } /** * * @return default headers */ public static String[] getDefaultMailMessageHeaderNames() { String headerNames[] = new String[30]; headerNames[0] = "Return-Path"; headerNames[1] = "Received"; headerNames[2] = "Resent-Date"; headerNames[3] = "Resent-From"; headerNames[4] = "Resent-Sender"; headerNames[5] = "Resent-To"; headerNames[6] = "Resent-Cc"; headerNames[7] = "Resent-Bcc"; headerNames[8] = "Resent-Message-Id"; headerNames[9] = "Date"; headerNames[10] = "From"; headerNames[11] = "Sender"; headerNames[12] = "Reply-To"; headerNames[13] = "To"; headerNames[14] = "Cc"; headerNames[15] = "Bcc"; headerNames[16] = "Message-Id"; headerNames[17] = "In-Reply-To"; headerNames[18] = "References"; headerNames[19] = "Subject"; headerNames[20] = "Comments"; headerNames[21] = "Keywords"; headerNames[22] = "Errors-To"; headerNames[23] = "MIME-Version"; headerNames[24] = "Content-Type"; headerNames[25] = "Content-Transfer-Encoding"; headerNames[26] = "Content-MD5"; headerNames[27] = ":"; headerNames[28] = "Content-Length"; headerNames[29] = "Status"; return headerNames; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -