📄 mimemessage.java
字号:
else setDataHandler(new DataHandler(o, type)); } /** * Convenience method that sets the given String as this * part's content, with a MIME type of "text/plain". If the * string contains non US-ASCII characters. it will be encoded * using the platform's default charset. The charset is also * used to set the "charset" parameter.<p> * * Note that there may be a performance penalty if * <code>text</code> is large, since this method may have * to scan all the characters to determine what charset to * use. <p> * * If the charset is already known, use the * <code>setText</code> method that takes the charset parameter. * * @param text the text content to set * @exception MessagingException if an error occurs * @see #setText(String text, String charset) */ public void setText(String text) throws MessagingException { setText(text, null); } /** * Convenience method that sets the given String as this part's * content, with a MIME type of "text/plain" and the specified * charset. The given Unicode string will be charset-encoded * using the specified charset. The charset is also used to set * the "charset" parameter. * * @param text the text content to set * @param charset the charset to use for the text * @exception MessagingException if an error occurs */ public void setText(String text, String charset) throws MessagingException { MimeBodyPart.setText(this, text, charset, "plain"); } /** * Convenience method that sets the given String as this part's * content, with a primary MIME type of "text" and the specified * MIME subtype. The given Unicode string will be charset-encoded * using the specified charset. The charset is also used to set * the "charset" parameter. * * @param text the text content to set * @param charset the charset to use for the text * @param subtype the MIME subtype to use (e.g., "html") * @exception MessagingException if an error occurs * @since JavaMail 1.4 */ public void setText(String text, String charset, String subtype) throws MessagingException { MimeBodyPart.setText(this, text, charset, subtype); } /** * This method sets the Message's content to a Multipart object. * * @param mp The multipart object that is the Message's content * @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 setContent(Multipart mp) throws MessagingException { setDataHandler(new DataHandler(mp, mp.getContentType())); mp.setParent(this); } /** * Get a new Message suitable for a reply to this message. * The new Message will have its attributes and headers * set up appropriately. Note that this new message object * will be empty, i.e., it will <strong>not</strong> have a "content". * These will have to be suitably filled in by the client. <p> * * If <code>replyToAll</code> is set, the new Message will be addressed * to all recipients of this message. Otherwise, the reply will be * addressed to only the sender of this message (using the value * of the <code>getReplyTo</code> method). <p> * * The "Subject" field is filled in with the original subject * prefixed with "Re:" (unless it already starts with "Re:"). * The "In-Reply-To" header is set in the new message if this * message has a "Message-Id" header. The <code>ANSWERED</code> * flag is set in this message. * * The current implementation also sets the "References" header * in the new message to include the contents of the "References" * header (or, if missing, the "In-Reply-To" header) in this message, * plus the contents of the "Message-Id" header of this message, * as described in RFC 2822. * * @param replyToAll reply should be sent to all recipients * of this message * @return the reply Message * @exception MessagingException */ public Message reply(boolean replyToAll) throws MessagingException { MimeMessage reply = createMimeMessage(session); /* * Have to manipulate the raw Subject header so that we don't lose * any encoding information. This is safe because "Re:" isn't * internationalized and (generally) isn't encoded. If the entire * Subject header is encoded, prefixing it with "Re: " still leaves * a valid and correct encoded header. */ String subject = getHeader("Subject", null); if (subject != null) { if (!subject.regionMatches(true, 0, "Re: ", 0, 4)) subject = "Re: " + subject; reply.setHeader("Subject", subject); } Address a[] = getReplyTo(); reply.setRecipients(Message.RecipientType.TO, a); if (replyToAll) { Vector v = new Vector(); // add my own address to list InternetAddress me = InternetAddress.getLocalAddress(session); if (me != null) v.addElement(me); // add any alternate names I'm known by String alternates = null; if (session != null) alternates = session.getProperty("mail.alternates"); if (alternates != null) eliminateDuplicates(v, InternetAddress.parse(alternates, false)); // should we Cc all other original recipients? String replyallccStr = null; if (session != null) replyallccStr = session.getProperty("mail.replyallcc"); boolean replyallcc = replyallccStr != null && replyallccStr.equalsIgnoreCase("true"); // add the recipients from the To field so far eliminateDuplicates(v, a); a = getRecipients(Message.RecipientType.TO); a = eliminateDuplicates(v, a); if (a != null && a.length > 0) { if (replyallcc) reply.addRecipients(Message.RecipientType.CC, a); else reply.addRecipients(Message.RecipientType.TO, a); } a = getRecipients(Message.RecipientType.CC); a = eliminateDuplicates(v, a); if (a != null && a.length > 0) reply.addRecipients(Message.RecipientType.CC, a); // don't eliminate duplicate newsgroups a = getRecipients(RecipientType.NEWSGROUPS); if (a != null && a.length > 0) reply.setRecipients(RecipientType.NEWSGROUPS, a); } String msgId = getHeader("Message-Id", null); if (msgId != null) reply.setHeader("In-Reply-To", msgId); /* * Set the References header as described in RFC 2822: * * The "References:" field will contain the contents of the parent's * "References:" field (if any) followed by the contents of the parent's * "Message-ID:" field (if any). If the parent message does not contain * a "References:" field but does have an "In-Reply-To:" field * containing a single message identifier, then the "References:" field * will contain the contents of the parent's "In-Reply-To:" field * followed by the contents of the parent's "Message-ID:" field (if * any). If the parent has none of the "References:", "In-Reply-To:", * or "Message-ID:" fields, then the new message will have no * "References:" field. */ String refs = getHeader("References", " "); if (refs == null) { // XXX - should only use if it contains a single message identifier refs = getHeader("In-Reply-To", " "); } if (msgId != null) { if (refs != null) refs = MimeUtility.unfold(refs) + " " + msgId; else refs = msgId; } if (refs != null) reply.setHeader("References", MimeUtility.fold(12, refs)); try { setFlags(answeredFlag, true); } catch (MessagingException mex) { // ignore it } return reply; } // used above in reply() private static final Flags answeredFlag = new Flags(Flags.Flag.ANSWERED); /** * Check addrs for any duplicates that may already be in v. * Return a new array without the duplicates. Add any new * addresses to v. Note that the input array may be modified. */ private Address[] eliminateDuplicates(Vector v, Address[] addrs) { if (addrs == null) return null; int gone = 0; for (int i = 0; i < addrs.length; i++) { boolean found = false; // search the vector for this address for (int j = 0; j < v.size(); j++) { if (((InternetAddress)v.elementAt(j)).equals(addrs[i])) { // found it; count it and remove it from the input array found = true; gone++; addrs[i] = null; break; } } if (!found) v.addElement(addrs[i]); // add new address to vector } // if we found any duplicates, squish the array if (gone != 0) { Address[] a; // new array should be same type as original array // XXX - there must be a better way, perhaps reflection? if (addrs instanceof InternetAddress[]) a = new InternetAddress[addrs.length - gone]; else a = new Address[addrs.length - gone]; for (int i = 0, j = 0; i < addrs.length; i++) if (addrs[i] != null) a[j++] = addrs[i]; addrs = a; } return addrs; } /** * Output the message as an RFC 822 format stream. <p> * * Note that, depending on how the messag was constructed, it may * use a variety of line termination conventions. Generally the * output should be sent through an appropriate FilterOutputStream * that converts the line terminators to the desired form, either * CRLF for MIME compatibility and for use in Internet protocols, * or the local platform's line terminator for storage in a local * text file. <p> * * This implementation calls the <code>writeTo(OutputStream, * String[])</code> method with a null ignore list. * * @exception IOException if an error occurs writing to the stream * or if an error is generated by the * javax.activation layer. * @exception MessagingException * @see javax.activation.DataHandler#writeTo */ public void writeTo(OutputStream os) throws IOException, MessagingException { writeTo(os, null); } /** * Output the message as an RFC 822 format stream, without * specified headers. If the <code>saved</code> flag is not set, * the <code>saveChanges</code> method is called. * If the <code>modified</code> flag is not * set and the <code>content</code> array is not null, the * <code>content</code> array is written directly, after * writing the appropriate message headers. * * @exception javax.mail.MessagingException * @exception IOException if an error occurs writing to the stream * or if an error is generated by the * javax.activation layer. * @see javax.activation.DataHandler#writeTo */ public void writeTo(OutputStream os, String[] ignoreList) throws IOException, MessagingException { if (!saved) saveChanges(); if (modified) { MimeBodyPart.writeTo(this, os, ignoreList); return; } // Else, the content is untouched, so we can just output it // First, write out the header Enumeration hdrLines = getNonMatchingHeaderLines(ignoreList); LineOutputStream los = new LineOutputStream(os); while (hdrLines.hasMoreElements()) los.writeln((String)hdrLines.nextElement()); // The CRLF separator between header and content los.writeln(); // Finally, the content. if (content == null) { // call getContentStream to give subclass a chance to // provide the data on demand InputStream is = getContentStream(); // now copy the data to the output stream byte[] buf = new byte[8192]; int len; while ((len = is.read(buf)) > 0) os.write(buf, 0, len); is.close(); buf = null; } else { os.write(content); } os.flush(); } /** * Get all the headers for this header_name. Note that certain * headers may be encoded as per RFC 2047 if they contain * non US-ASCII characters and these should be decoded. <p> * * This implementation obtains the headers from the * <code>headers</code> InternetHeaders object. * * @param name name of header * @return array of headers * @exception MessagingException * @see javax.mail.internet.MimeUtility */ public String[] getHeader(String name) throws MessagingException { return headers.getHeader(name); } /** * Get all the headers for this header name, returned as a single * String, with headers separated by the delimiter. If the * delimiter is <code>null</code>, only the first header is * returned. * * @param name the name of this header * @param delimiter separator between values * @return the value fields for all headers with * this name * @exception MessagingException */ public String getHeader(String name, String delimiter) throws MessagingException { return headers.getHeader(name, delimiter); } /** * Set the value for
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -