MessageInfo.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:9k
源码类别:
Java编程
开发平台:
Java
- /*
- * @(#)MessageInfo.java 1.4 02/04/04
- *
- * Copyright 2001-2002 Sun Microsystems, Inc. All Rights Reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * - Redistribution in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * Neither the name of Sun Microsystems, Inc. or the names of contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * This software is provided "AS IS," without a warranty of any kind. ALL
- * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
- * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
- * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
- * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES
- * SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION
- * OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
- * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
- * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
- * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
- * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS
- * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
- *
- * You acknowledge that Software is not designed, licensed or intended
- * for use in the design, construction, operation or maintenance of any
- * nuclear facility.
- *
- */
- package demo;
- import java.text.*;
- import java.util.*;
- import javax.mail.*;
- import javax.mail.internet.*;
- /**
- * Used to store message information.
- */
- public class MessageInfo {
- private Message message;
- /**
- * Returns the bcc field.
- */
- public String getBcc() throws MessagingException {
- return formatAddresses(
- message.getRecipients(Message.RecipientType.BCC));
- }
- /**
- * Returns the body of the message (if it's plain text).
- */
- public String getBody() throws MessagingException, java.io.IOException {
- Object content = message.getContent();
- if (message.isMimeType("text/plain")) {
- return (String)content;
- } else if (message.isMimeType("multipart/alternative")) {
- Multipart mp = (Multipart)message.getContent();
- int numParts = mp.getCount();
- for (int i = 0; i < numParts; ++i) {
- if (mp.getBodyPart(i).isMimeType("text/plain"))
- return (String)mp.getBodyPart(i).getContent();
- }
- return "";
- } else if (message.isMimeType("multipart/*")) {
- Multipart mp = (Multipart)content;
- if (mp.getBodyPart(0).isMimeType("text/plain"))
- return (String)mp.getBodyPart(0).getContent();
- else
- return "";
- } else
- return "";
- }
- /**
- * Returns the cc field.
- */
- public String getCc() throws MessagingException {
- return formatAddresses(
- message.getRecipients(Message.RecipientType.CC));
- }
- /**
- * Returns the date the message was sent (or received if the sent date
- * is null.
- */
- public String getDate() throws MessagingException {
- Date date;
- SimpleDateFormat df = new SimpleDateFormat("EE M/d/yy");
- if ((date = message.getSentDate()) != null)
- return (df.format(date));
- else if ((date = message.getReceivedDate()) != null)
- return (df.format(date));
- else
- return "";
- }
- /**
- * Returns the from field.
- */
- public String getFrom() throws MessagingException {
- return formatAddresses(message.getFrom());
- }
- /**
- * Returns the address to reply to.
- */
- public String getReplyTo() throws MessagingException {
- Address[] a = message.getReplyTo();
- if (a.length > 0)
- return ((InternetAddress)a[0]).getAddress();
- else
- return "";
- }
- /**
- * Returns the javax.mail.Message object.
- */
- public Message getMessage() {
- return message;
- }
- /**
- * Returns the message number.
- */
- public String getNum() {
- return (Integer.toString(message.getMessageNumber()));
- }
- /**
- * Returns the received date field.
- */
- public String getReceivedDate() throws MessagingException {
- if (hasReceivedDate())
- return (message.getReceivedDate().toString());
- else
- return "";
- }
- /**
- * Returns the sent date field.
- */
- public String getSentDate() throws MessagingException {
- if (hasSentDate())
- return (message.getSentDate().toString());
- else
- return "";
- }
- /**
- * Returns the subject field.
- */
- public String getSubject() throws MessagingException {
- if (hasSubject())
- return message.getSubject();
- else
- return "";
- }
- /**
- * Returns the to field.
- */
- public String getTo() throws MessagingException {
- return formatAddresses(
- message.getRecipients(Message.RecipientType.TO));
- }
- /**
- * Method for checking if the message has attachments.
- */
- public boolean hasAttachments() throws java.io.IOException,
- MessagingException {
- boolean hasAttachments = false;
- if (message.isMimeType("multipart/*")) {
- Multipart mp = (Multipart)message.getContent();
- if (mp.getCount() > 1)
- hasAttachments = true;
- }
- return hasAttachments;
- }
- /**
- * Method for checking if the message has a bcc field.
- */
- public boolean hasBcc() throws MessagingException {
- return (message.getRecipients(Message.RecipientType.BCC) != null);
- }
- /**
- * Method for checking if the message has a cc field.
- */
- public boolean hasCc() throws MessagingException {
- return (message.getRecipients(Message.RecipientType.CC) != null);
- }
- /**
- * Method for checking if the message has a date field.
- */
- public boolean hasDate() throws MessagingException {
- return (hasSentDate() || hasReceivedDate());
- }
- /**
- * Method for checking if the message has a from field.
- */
- public boolean hasFrom() throws MessagingException {
- return (message.getFrom() != null);
- }
- /**
- * Method for checking if the message has the desired mime type.
- */
- public boolean hasMimeType(String mimeType) throws MessagingException {
- return message.isMimeType(mimeType);
- }
- /**
- * Method for checking if the message has a received date field.
- */
- public boolean hasReceivedDate() throws MessagingException {
- return (message.getReceivedDate() != null);
- }
- /**
- * Method for checking if the message has a sent date field.
- */
- public boolean hasSentDate() throws MessagingException {
- return (message.getSentDate() != null);
- }
- /**
- * Method for checking if the message has a subject field.
- */
- public boolean hasSubject() throws MessagingException {
- return (message.getSubject() != null);
- }
- /**
- * Method for checking if the message has a to field.
- */
- public boolean hasTo() throws MessagingException {
- return (message.getRecipients(Message.RecipientType.TO) != null);
- }
- /**
- * Method for mapping a message to this MessageInfo class.
- */
- public void setMessage(Message message) {
- this.message = message;
- }
- /**
- * Utility method for formatting msg header addresses.
- */
- private String formatAddresses(Address[] addrs) {
- if (addrs == null)
- return "";
- StringBuffer strBuf = new StringBuffer(getDisplayAddress(addrs[0]));
- for (int i = 1; i < addrs.length; i++) {
- strBuf.append(", ").append(getDisplayAddress(addrs[i]));
- }
- return strBuf.toString();
- }
- /**
- * Utility method which returns a string suitable for msg header display.
- */
- private String getDisplayAddress(Address a) {
- String pers = null;
- String addr = null;
- if (a instanceof InternetAddress &&
- ((pers = ((InternetAddress)a).getPersonal()) != null)) {
- addr = pers + " "+"<"+((InternetAddress)a).getAddress()+">";
- } else
- addr = a.toString();
- return addr;
- }
- }