📄 mimemessage.java
字号:
return a; } /** * Set the RFC 822 "From" header field. Any existing values are * replaced with the given address. If address is <code>null</code>, * this header is removed. * * @param address the sender of this message * @exception IllegalWriteException if the underlying * implementation does not support modification * of existing values * @exception IllegalStateException if this message is * obtained from a READ_ONLY folder. * @exception MessagingException */ public void setFrom(Address address) throws MessagingException { if (address == null) removeHeader("From"); else setHeader("From", address.toString()); } /** * Set the RFC 822 "From" header field using the value of the * <code>InternetAddress.getLocalAddress</code> method. * * @exception IllegalWriteException if the underlying * implementation does not support modification * of existing values * @exception IllegalStateException if this message is * obtained from a READ_ONLY folder. * @exception MessagingException */ public void setFrom() throws MessagingException { InternetAddress me = InternetAddress.getLocalAddress(session); if (me != null) setFrom(me); else throw new MessagingException("No From address"); } /** * Add the specified addresses to the existing "From" field. If * the "From" field does not already exist, it is created. * * @param addresses the senders of this message * @exception IllegalWriteException if the underlying * implementation does not support modification * of existing values * @exception IllegalStateException if this message is * obtained from a READ_ONLY folder. * @exception MessagingException */ public void addFrom(Address[] addresses) throws MessagingException { addAddressHeader("From", addresses); } /** * Returns the value of the RFC 822 "Sender" header field. * If the "Sender" header field is absent, <code>null</code> * is returned.<p> * * This implementation uses the <code>getHeader</code> method * to obtain the requisite header field. * * @return Address object * @exception MessagingException * @see #headers * @since JavaMail 1.3 */ public Address getSender() throws MessagingException { Address[] a = getAddressHeader("Sender"); if (a == null || a.length == 0) return null; return a[0]; // there can be only one } /** * Set the RFC 822 "Sender" header field. Any existing values are * replaced with the given address. If address is <code>null</code>, * this header is removed. * * @param address the sender of this message * @exception IllegalWriteException if the underlying * implementation does not support modification * of existing values * @exception IllegalStateException if this message is * obtained from a READ_ONLY folder. * @exception MessagingException * @since JavaMail 1.3 */ public void setSender(Address address) throws MessagingException { if (address == null) removeHeader("Sender"); else setHeader("Sender", address.toString()); } /** * This inner class extends the javax.mail.Message.RecipientType * class to add additional RecipientTypes. The one additional * RecipientType currently defined here is NEWSGROUPS. * * @see javax.mail.Message.RecipientType */ public static class RecipientType extends Message.RecipientType { private static final long serialVersionUID = -5468290701714395543L; /** * The "Newsgroup" (Usenet news) recipients. */ public static final RecipientType NEWSGROUPS = new RecipientType("Newsgroups"); protected RecipientType(String type) { super(type); } protected Object readResolve() throws ObjectStreamException { if (type.equals("Newsgroups")) return NEWSGROUPS; else return super.readResolve(); } } /** * Returns the recepients specified by the type. The mapping * between the type and the corresponding RFC 822 header is * as follows: * <pre> * Message.RecipientType.TO "To" * Message.RecipientType.CC "Cc" * Message.RecipientType.BCC "Bcc" * MimeMessage.RecipientType.NEWSGROUPS "Newsgroups" * </pre><br> * * Returns null if the header specified by the type is not found * or if its value is empty. <p> * * This implementation uses the <code>getHeader</code> method * to obtain the requisite header field. * * @param type Type of recepient * @return array of Address objects * @exception MessagingException if header could not * be retrieved * @exception AddressException if the header is misformatted * @see #headers * @see javax.mail.Message.RecipientType#TO * @see javax.mail.Message.RecipientType#CC * @see javax.mail.Message.RecipientType#BCC * @see javax.mail.internet.MimeMessage.RecipientType#NEWSGROUPS */ public Address[] getRecipients(Message.RecipientType type) throws MessagingException { if (type == RecipientType.NEWSGROUPS) { String s = getHeader("Newsgroups", ","); return (s == null) ? null : NewsAddress.parse(s); } else return getAddressHeader(getHeaderName(type)); } /** * Get all the recipient addresses for the message. * Extracts the TO, CC, BCC, and NEWSGROUPS recipients. * * @return array of Address objects * @exception MessagingException * @see javax.mail.Message.RecipientType#TO * @see javax.mail.Message.RecipientType#CC * @see javax.mail.Message.RecipientType#BCC * @see javax.mail.internet.MimeMessage.RecipientType#NEWSGROUPS */ public Address[] getAllRecipients() throws MessagingException { Address[] all = super.getAllRecipients(); Address[] ng = getRecipients(RecipientType.NEWSGROUPS); if (ng == null) return all; // the common case if (all == null) return ng; // a rare case Address[] addresses = new Address[all.length + ng.length]; System.arraycopy(all, 0, addresses, 0, all.length); System.arraycopy(ng, 0, addresses, all.length, ng.length); return addresses; } /** * Set the specified recipient type to the given addresses. * If the address parameter is <code>null</code>, the corresponding * recipient field is removed. * * @param type Recipient type * @param addresses Addresses * @exception IllegalWriteException if the underlying * implementation does not support modification * of existing values * @exception IllegalStateException if this message is * obtained from a READ_ONLY folder. * @exception MessagingException * @see #getRecipients */ public void setRecipients(Message.RecipientType type, Address[] addresses) throws MessagingException { if (type == RecipientType.NEWSGROUPS) { if (addresses == null || addresses.length == 0) removeHeader("Newsgroups"); else setHeader("Newsgroups", NewsAddress.toString(addresses)); } else setAddressHeader(getHeaderName(type), addresses); } /** * Set the specified recipient type to the given addresses. * If the address parameter is <code>null</code>, the corresponding * recipient field is removed. * * @param type Recipient type * @param addresses Addresses * @exception AddressException if the attempt to parse the * addresses String fails * @exception IllegalWriteException if the underlying * implementation does not support modification * of existing values * @exception IllegalStateException if this message is * obtained from a READ_ONLY folder. * @exception MessagingException * @see #getRecipients * @since JavaMail 1.2 */ public void setRecipients(Message.RecipientType type, String addresses) throws MessagingException { if (type == RecipientType.NEWSGROUPS) { if (addresses == null || addresses.length() == 0) removeHeader("Newsgroups"); else setHeader("Newsgroups", addresses); } else setAddressHeader(getHeaderName(type), InternetAddress.parse(addresses)); } /** * Add the given addresses to the specified recipient type. * * @param type Recipient type * @param addresses Addresses * @exception IllegalWriteException if the underlying * implementation does not support modification * of existing values * @exception IllegalStateException if this message is * obtained from a READ_ONLY folder. * @exception MessagingException */ public void addRecipients(Message.RecipientType type, Address[] addresses) throws MessagingException { if (type == RecipientType.NEWSGROUPS) { String s = NewsAddress.toString(addresses); if (s != null) addHeader("Newsgroups", s); } else addAddressHeader(getHeaderName(type), addresses); } /** * Add the given addresses to the specified recipient type. * * @param type Recipient type * @param addresses Addresses * @exception AddressException if the attempt to parse the * addresses String fails * @exception IllegalWriteException if the underlying * implementation does not support modification * of existing values * @exception IllegalStateException if this message is * obtained from a READ_ONLY folder. * @exception MessagingException * @since JavaMail 1.2 */ public void addRecipients(Message.RecipientType type, String addresses) throws MessagingException { if (type == RecipientType.NEWSGROUPS) { if (addresses != null && addresses.length() != 0) addHeader("Newsgroups", addresses); } else addAddressHeader(getHeaderName(type), InternetAddress.parse(addresses)); } /** * Return the value of the RFC 822 "Reply-To" header field. If * this header is unavailable or its value is absent, then * the <code>getFrom</code> method is called and its value is returned. * * This implementation uses the <code>getHeader</code> method * to obtain the requisite header field. * * @exception MessagingException * @see #headers */ public Address[] getReplyTo() throws MessagingException { Address[] a = getAddressHeader("Reply-To"); if (a == null) a = getFrom(); return a; } /** * Set the RFC 822 "Reply-To" header field. If the address * parameter is <code>null</code>, this header is removed. * * @exception IllegalWriteException if the underlying * implementation does not support modification * of existing values * @exception IllegalStateException if this message is * obtained from a READ_ONLY folder. * @exception MessagingException */ public void setReplyTo(Address[] addresses) throws MessagingException { setAddressHeader("Reply-To", addresses); } // Convenience method to get addresses private Address[] getAddressHeader(String name) throws MessagingException { String s = getHeader(name, ","); return (s == null) ? null : InternetAddress.parseHeader(s, strict); } // Convenience method to set addresses private void setAddressHeader(String name, Address[] addresses) throws MessagingException { String s = InternetAddress.toString(addresses); if (s == null) removeHeader(name); else setHeader(name, s); } private void addAddressHeader(String name, Address[] addresses) throws MessagingException { String s = InternetAddress.toString(addresses); if (s == null) return; addHeader(name, s); } /** * Returns the value of the "Subject" header field. Returns null * if the subject field is unavailable or its value is absent. <p> * * If the subject is encoded as per RFC 2047, it is decoded and * converted into Unicode. If the decoding or conversion fails, the * raw data is returned as is. <p> * * This implementation uses the <code>getHeader</code> method * to obtain the requisite header field. * * @return Subject * @exception MessagingException
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -