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

📄 email.java

📁 基于UDP的可靠邮件系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	 * 
	 * @return approximate size of full message including headers.
	 * 
	 * @throws MessagingException
	 *             if a problem occurs while computing the message size
	 */
	public long getMessageSize(){
        // If we have a byte[]Wrapper, then we can ask it for just the
        // message size and skip calculating it
//        if (message instanceof byte[]Wrapper) {
//            byte[]Wrapper wrapper = (byte[]Wrapper) message;
//            return wrapper.getMessageSize();
//        }
//        // SK: Should probably eventually store this as a locally
//        // maintained value (so we don't have to load and reparse
//        // messages each time).
//        long size = message.getSize();
//        Enumeration e = message.getAllHeaderLines();
//        while (e.hasMoreElements()) {
//            size += ((String) e.nextElement()).length();
//        }
        return message.length;
    }

	/**
	 * Set the error message associated with this Email.
	 * 
	 * @param msg
	 *            the new error message associated with this Email
	 */
	public void setErrorMessage(String msg) {
		this.errorMessage = msg;
	}

	/**
	 * Set the byte[] associated with this Email.
	 * 
	 * @param message
	 *            the new byte[] associated with this Email
	 */
	public void setMessage(byte[] message) {
		this.message = message;
	}

	/**
	 * Set the recipients for this Email.
	 * 
	 * @param recipients
	 *            the recipients for this Email
	 */
	public void setRecipients(Collection recipients) {
		this.recipients = recipients;
	}

	/**
	 * Set the sender of this Email.
	 * 
	 * @param sender
	 *            the sender of this Email
	 */
	public void setSender(MailAddress sender) {
		this.sender = sender;
	}

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

	/**
	 * Set the remote address associated with this Email.
	 * 
	 * @param remoteHost
	 *            the new remote host associated with this Email
	 */
	public void setRemoteHost(String remoteHost) {
		this.remoteHost = remoteHost;
	}

	/**
	 * Set the remote address associated with this Email.
	 * 
	 * @param remoteAddr
	 *            the new remote address associated with this Email
	 */
	public void setRemoteAddr(String remoteAddr) {
		this.remoteAddr = remoteAddr;
	}

	/**
	 * Set the date this mail was last updated.
	 * 
	 * @param lastUpdated
	 *            the date the mail was last updated
	 */
	public void setLastUpdated(Date lastUpdated) {
		// Make a defensive copy to ensure that the date
		// doesn't get changed external to the class
		if (lastUpdated != null) {
			lastUpdated = new Date(lastUpdated.getTime());
		}
		this.lastUpdated = lastUpdated;
	}

	/**
	 * Writes the message out to an OutputStream.
	 * 
	 * @param out
	 *            the OutputStream to which to write the content
	 * 
	 * @throws MessagingException
	 *             if the byte[] is not set for this Email
	 * @throws IOException
	 *             if an error occurs while reading or writing from the stream
	 */
	public void writeMessageTo(OutputStream out) throws IOException{
			 out.write(message);
//		if (message != null) {
//			message.writeTo(out);
//		} else {
//			throw new MessagingException("No message set for this Email.");
//		}
	}

//	/**
//	 * Generates a bounce mail that is a bounce of the original message.
//	 * 
//	 * @param bounceText
//	 *            the text to be prepended to the message to describe the bounce
//	 *            condition
//	 * 
//	 * @return the bounce mail
//	 * 
//	 * @throws MessagingException
//	 *             if the bounce mail could not be created
//	 */
//	public Mail bounce(String bounceText) throws MessagingException {
//		// This sends a message to the james component that is a bounce of the
//		// sent message
//		byte[] original = getMessage();
//		byte[] reply = (byte[]) original.reply(false);
//		reply.setSubject("Re: " + original.getSubject());
//		Collection recipients = new HashSet();
//		recipients.add(getSender());
//		InternetAddress addr[] = { new InternetAddress(getSender().toString()) };
//		reply.setRecipients(Message.RecipientType.TO, addr);
//		reply.setFrom(new InternetAddress(getRecipients().iterator().next()
//				.toString()));
//		reply.setText(bounceText);
//		reply.setHeader(RFC2822Headers.MESSAGE_ID, "replyTo-" + getName());
//		return new Email("replyTo-" + getName(), new MailAddress(
//				getRecipients().iterator().next().toString()), recipients,
//				reply);
//	}

	/**
	 * Writes the content of the message, up to a total number of lines, out to
	 * an OutputStream.
	 * 
	 * @param out
	 *            the OutputStream to which to write the content
	 * @param lines
	 *            the number of lines to write to the stream
	 * 
	 * @throws MessagingException
	 *             if the byte[] is not set for this Email
	 * @throws IOException
	 *             if an error occurs while reading or writing from the stream
	 */
//	public void writeContentTo(OutputStream out, int lines) throws IOException
//			 {
//		String line;
//		BufferedReader br;
//		if (message != null) {
//			br = new BufferedReader(new InputStreamReader(message
//					.getInputStream()));
//			while (lines-- > 0) {
//				if ((line = br.readLine()) == null) {
//					break;
//				}
//				line += "\r\n";
//				out.write(line.getBytes());
//			}
//		} else {
//			throw new MessagingException("No message set for this Email.");
//		}
//	}

	// Serializable Methods
	// TODO: These need some work. Currently very tightly coupled to
	// the internal representation.
	/**
	 * Read the Email from an <code>ObjectInputStream</code>.
	 * 
	 * @param in
	 *            the ObjectInputStream from which the object is read
	 * 
	 * @throws IOException
	 *             if an error occurs while reading from the stream
	 * @throws ClassNotFoundException ?
	 * @throws ClassCastException
	 *             if the serialized objects are not of the appropriate type
	 */

	private void readObject(java.io.ObjectInputStream in) throws IOException,
			ClassNotFoundException {
		try {
			Object obj = in.readObject();
			if (obj == null) {
				sender = null;
			} else if (obj instanceof String) {
				sender = new MailAddress((String) obj);
			} else if (obj instanceof MailAddress) {
				sender = (MailAddress) obj;
			}
		} catch (Exception e) {
			throw new IOException("Error parsing sender address: "
					+ e.getMessage());
		}
		recipients = (Collection) in.readObject();
		state = (String) in.readObject();
		errorMessage = (String) in.readObject();
		name = (String) in.readObject();
		remoteHost = (String) in.readObject();
		remoteAddr = (String) in.readObject();
		setLastUpdated((Date) in.readObject());
		
		// the following is under try/catch to be backwards compatible
		// with messages created with James version <= 2.2.0a8
		try {
			attributes = (HashMap) in.readObject();
			message =(byte[]) in.readObject();
		} catch (OptionalDataException ode) {
			if (ode.eof) {
				attributes = new HashMap();
			} else {
				throw ode;
			}
		}
	}

	/**
	 * Write the Email to an <code>ObjectOutputStream</code>.
	 * 
	 * @param in
	 *            the ObjectOutputStream to which the object is written
	 * 
	 * @throws IOException
	 *             if an error occurs while writing to the stream
	 */

	private void writeObject(java.io.ObjectOutputStream out) throws IOException {
		lastUpdated = new Date();
		out.writeObject(sender);
		out.writeObject(recipients);
		out.writeObject(state);
		out.writeObject(errorMessage);
		out.writeObject(name);
		out.writeObject(remoteHost);
		out.writeObject(remoteAddr);
		out.writeObject(lastUpdated);
		out.writeObject(attributes);
		out.writeObject(message);
	}

	// /**
	// * @see org.apache.avalon.framework.activity.Disposable#dispose()
	// */
	// public void dispose() {
	// try {
	// byte[] wrapper = getMessage();
	// if (wrapper instanceof Disposable) {
	// ((Disposable)wrapper).dispose();
	// }
	// } catch (MessagingException me) {
	// // Ignored
	// }
	// }

	/**
	 * This method is necessary, when Mail repositories needs to deal explicitly
	 * with storing Mail attributes as a Serializable Note: This method is not
	 * exposed in the Mail interface, it is for internal use by James only.
	 * 
	 * @return Serializable of the entire attributes collection
	 * @since 2.2.0
	 */
	public HashMap getAttributesRaw() {
		return attributes;
	}

	/**
	 * This method is necessary, when Mail repositories needs to deal explicitly
	 * with retriving Mail attributes as a Serializable Note: This method is not
	 * exposed in the Mail interface, it is for internal use by James only.
	 * 
	 * @return Serializable of the entire attributes collection
	 * @since 2.2.0
	 */
	public void setAttributesRaw(HashMap attr) {
		this.attributes = (attr == null) ? new HashMap() : attr;
	}

	/**
	 * @see org.apache.mailet.Mail#getAttribute(String)
	 * @since 2.2.0
	 */
	public Serializable getAttribute(String key) {
		return (Serializable) attributes.get(key);
	}

	/**
	 * @see org.apache.mailet.Mail#setAttribute(String,Serializable)
	 * @since 2.2.0
	 */
	public Serializable setAttribute(String key, Serializable object) {
		return (Serializable) attributes.put(key, object);
	}

	/**
	 * @see org.apache.mailet.Mail#removeAttribute(String)
	 * @since 2.2.0
	 */
	public Serializable removeAttribute(String key) {
		return (Serializable) attributes.remove(key);
	}

	/**
	 * @see org.apache.mailet.Mail#removeAllAttributes()
	 * @since 2.2.0
	 */
	public void removeAllAttributes() {
		attributes.clear();
	}

	/**
	 * @see org.apache.mailet.Mail#getAttributeNames()
	 * @since 2.2.0
	 */
	public Iterator getAttributeNames() {
		return attributes.keySet().iterator();
	}

	/**
	 * @see org.apache.mailet.Mail#hasAttributes()
	 * @since 2.2.0
	 */
	public boolean hasAttributes() {
		return !attributes.isEmpty();
	}
}

⌨️ 快捷键说明

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