?? email.java
字號:
* Note: this size is not guaranteed to be accurate - see Sun's * documentation of MimeMessage.getSize(). * </p> * * @return approximate size of full message including headers. * * @throws MessagingException * if a problem occurs while computing the message size */ public long getMessageSize() throws Exception { // If we have a MimeMessageWrapper, then we can ask it for just the // message size and skip calculating it // if (message instanceof MimeMessageWrapper) { // MimeMessageWrapper wrapper = (MimeMessageWrapper) 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 size; 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 MimeMessage associated with this Email. * * @param message * the new MimeMessage 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 Exception * if the MimeMessage 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, Exception { if (message != null) { // message.writeTo(out); out.write(message); } else { throw new Exception("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 Exception * if the bounce mail could not be created */ // public Mail bounce(String bounceText) throws Exception { // // This sends a message to the james component that is a bounce of the // // sent message // MimeMessage original = getMessage(); // MimeMessage reply = (MimeMessage) 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 Exception * if the MimeMessage 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, Exception { 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());} out.write(message); } else { throw new Exception("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 pe) { throw new IOException("Error parsing sender address: " + pe.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 { // MimeMessage wrapper = getMessage(); // if (wrapper instanceof Disposable) { // ((Disposable)wrapper).dispose(); // } // } catch (Exception 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(); }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -