⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 email.java

📁 基于UDP的可靠邮件系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/***********************************************************************
 * Copyright (c) 2000-2004 The Apache Software Foundation.             *
 * All rights reserved.                                                *
 * ------------------------------------------------------------------- *
 * Licensed under the Apache License, Version 2.0 (the "License"); you *
 * may not use this file except in compliance with the License. You    *
 * may obtain a copy of the License at:                                *
 *                                                                     *
 *     http://www.apache.org/licenses/LICENSE-2.0                      *
 *                                                                     *
 * Unless required by applicable law or agreed to in writing, software *
 * distributed under the License is distributed on an "AS IS" BASIS,   *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or     *
 * implied.  See the License for the specific language governing       *
 * permissions and limitations under the License.                      *
 ***********************************************************************/

package org.hwmhere.email.impl;

// import org.apache.avalon.framework.activity.Disposable;
import org.hwmhere.email.impl.util.RFC2822Headers;
import org.hwmhere.email.mailet.MailAddress;

import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.HashMap;

/**
 * <P>
 * Wraps a MimeMessage adding routing information (from SMTP) and some simple
 * API enhancements.
 * </P>
 * <P>
 * From James version > 2.2.0a8 "mail attributes" have been added. Backward and
 * forward compatibility is supported: messages stored in file repositories
 * <I>without</I> attributes by James version <= 2.2.0a8 will be processed by
 * later versions as having an empty attributes hashmap; messages stored in file
 * repositories <I>with</I> attributes by James version > 2.2.0a8 will be
 * processed by previous versions, ignoring the attributes.
 * </P>
 * 
 * @version CVS $Revision: 1.17.4.6 $ $Date: 2004/03/15 03:54:15 $
 */
public class Email implements Serializable, Cloneable {// /Disposable, Mail {
	/**
	 * We hardcode the serialVersionUID so that from James 1.2 on, Email will be
	 * deserializable (so your mail doesn't get lost)
	 */
	public static final long serialVersionUID = -4289663364703986260L;

	/**
	 * The error message, if any, associated with this mail.
	 */
	private String errorMessage;

	/**
	 * The state of this mail, which determines how it is processed.
	 */
	private String state;

	/**
	 * The MimeMessage that holds the mail data.
	 */
	private byte[] message;

	/**
	 * The sender of this mail.
	 */
	private MailAddress sender;

	/**
	 * The collection of recipients to whom this mail was sent.
	 */
	private Collection recipients;

	/**
	 * The identifier for this mail message
	 */
	private String name;

	/**
	 * The remote host from which this mail was sent.
	 */
	private String remoteHost = "localhost";

	/**
	 * The remote address from which this mail was sent.
	 */
	private String remoteAddr = "127.0.0.1";

	/**
	 * The last time this message was updated.
	 */
	private Date lastUpdated = new Date();

	/**
	 * Attributes added to this Email instance
	 */
	private HashMap attributes;

	/**
	 * A constructor that creates a new, uninitialized Email
	 */
	public Email() {
//		setState(Mail.DEFAULT);
		attributes = new HashMap();
	}

	/**
	 * A constructor that creates a Email with the specified name, sender, and
	 * recipients.
	 * 
	 * @param name
	 *            the name of the Email
	 * @param sender
	 *            the sender for this Email
	 * @param recipients
	 *            the collection of recipients of this Email
	 */
	public Email(String name, MailAddress sender, Collection recipients) {
		this();
		this.name = name;
		this.sender = sender;
		this.recipients = null;

		// Copy the recipient list
		if (recipients != null) {
			Iterator theIterator = recipients.iterator();
			this.recipients = new ArrayList();
			while (theIterator.hasNext()) {
				this.recipients.add(theIterator.next());
			}
		}
	}

	/**
	 * A constructor that creates a Email with the specified name, sender,
	 * recipients, and MimeMessage.
	 * 
	 * @param name
	 *            the name of the Email
	 * @param sender
	 *            the sender for this Email
	 * @param recipients
	 *            the collection of recipients of this Email
	 * @param message
	 *            the MimeMessage associated with this Email
	 */
	public Email(String name, MailAddress sender, Collection recipients,
			byte[] message) {
		this(name, sender, recipients);
		this.setMessage(message);
	}

	/**
	 * A constructor which will attempt to obtain sender and recipients from the
	 * headers of the byte[] supplied.
	 * 
	 * @param message -
	 *            a byte[] from which to construct a Mail
	 */
//	public Email(byte[] message) throws MessagingException {
//        this();
//        MailAddress sender = getReturnPath(message);
//        Collection recipients = null;
//        Address[] addresses = message.getRecipients(byte[].RecipientType.TO);
//        if (addresses != null) {
//            recipients = new ArrayList();
//            for (int i = 0; i < addresses.length; i++) {
//                try {
//                    recipients.add(new MailAddress(new InternetAddress(addresses[i].toString(), false)));
//                } catch (ParseException pe) {
//                    // RFC 2822 section 3.4 allows To: fields without <>
//                    // Let's give this one more try with <>.
//                    try {
//                        recipients.add(new MailAddress("<" + new InternetAddress(addresses[i].toString()).toString() + ">"));
//                    } catch (ParseException _) {
//                        throw new MessagingException("Could not parse address: " + addresses[i].toString() + " from " + message.getHeader(RFC2822Headers.TO, ", "), pe);
//                    }
//                }
//            }
//        }
//        this.name = message.toString();
//        this.sender = sender;
//        this.recipients = recipients;
//        this.setMessage(message);
//    }

	/**
	 * Gets the MailAddress corresponding to the existing "Return-Path" of
	 * <I>message</I>. If missing or empty returns <CODE>null</CODE>,
	 */
//	private MailAddress getReturnPath(byte[] message) throws MessagingException {
//		MailAddress mailAddress = null;
//		String[] returnPathHeaders = message
//				.getHeader(RFC2822Headers.RETURN_PATH);
//		String returnPathHeader = null;
//		if (returnPathHeaders != null) {
//			returnPathHeader = returnPathHeaders[0];
//			if (returnPathHeader != null) {
//				returnPathHeader = returnPathHeader.trim();
//				if (!returnPathHeader.equals("<>")) {
//					try {
//						mailAddress = new MailAddress(new InternetAddress(
//								returnPathHeader, false));
//					} catch (ParseException pe) {
//						throw new MessagingException(
//								"Could not parse address: "
//										+ returnPathHeader
//										+ " from "
//										+ message.getHeader(
//												RFC2822Headers.RETURN_PATH,
//												", "), pe);
//					}
//				}
//			}
//		}
//		return mailAddress;
//	}

	/**
	 * Duplicate the Email.
	 * 
	 * @return a Email that is a duplicate of this one
	 */
	public Email duplicate() {
		return duplicate(name);
	}

	/**
	 * Duplicate the Email, replacing the mail name with the one passed in as an
	 * argument.
	 * 
	 * @param newName
	 *            the name for the duplicated mail
	 * 
	 * @return a Email that is a duplicate of this one with a different name
	 */
	public Email duplicate(String newName) {
		try {
			Email newMail = new Email(newName, sender, recipients, getMessage());
			newMail.setRemoteHost(remoteHost);
			newMail.setRemoteAddr(remoteAddr);
			newMail.setLastUpdated(lastUpdated);
			newMail.setAttributesRaw((HashMap) attributes.clone());
			return newMail;
		} catch (Exception me) {
			// Ignored. Return null in the case of an error.
		}
		return null;
	}

	/**
	 * Get the error message associated with this Email.
	 * 
	 * @return the error message associated with this Email
	 */
	public String getErrorMessage() {
		return errorMessage;
	}

	/**
	 * Get the byte[] associated with this Email.
	 * 
	 * @return the byte[] associated with this Email
	 */
	public byte[] getMessage(){
		return message;
	}

	/**
	 * Set the name of this Email.
	 * 
	 * @param name
	 *            the name of this Email
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Get the name of this Email.
	 * 
	 * @return the name of this Email
	 */
	public String getName() {
		return name;
	}

	/**
	 * Get the recipients of this Email.
	 * 
	 * @return the recipients of this Email
	 */
	public Collection getRecipients() {
		return recipients;
	}

	/**
	 * Get the sender of this Email.
	 * 
	 * @return the sender of this Email
	 */
	public MailAddress getSender() {
		return sender;
	}

	/**
	 * Get the state of this Email.
	 * 
	 * @return the state of this Email
	 */
	public String getState() {
		return state;
	}

	/**
	 * Get the remote host associated with this Email.
	 * 
	 * @return the remote host associated with this Email
	 */
	public String getRemoteHost() {
		return remoteHost;
	}

	/**
	 * Get the remote address associated with this Email.
	 * 
	 * @return the remote address associated with this Email
	 */
	public String getRemoteAddr() {
		return remoteAddr;
	}

	/**
	 * Get the last updated time for this Email.
	 * 
	 * @return the last updated time for this Email
	 */
	public Date getLastUpdated() {
		return lastUpdated;
	}

	/**
	 * <p>
	 * Return the size of the message including its headers. byte[].getSize()
	 * method only returns the size of the message body.
	 * </p>
	 * 
	 * <p>
	 * Note: this size is not guaranteed to be accurate - see Sun's
	 * documentation of byte[].getSize().
	 * </p>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -