📄 mailimpl.java
字号:
public Date getLastUpdated() { return lastUpdated; } /** * <p>Return the size of the message including its headers. * MimeMessage.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 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 MessagingException { //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; } /** * Set the error message associated with this MailImpl. * * @param msg the new error message associated with this MailImpl */ public void setErrorMessage(String msg) { this.errorMessage = msg; } /** * Set the MimeMessage associated with this MailImpl. * * @param message the new MimeMessage associated with this MailImpl */ public void setMessage(MimeMessage message) { this.message = message; } /** * Set the recipients for this MailImpl. * * @param recipients the recipients for this MailImpl */ public void setRecipients(Collection recipients) { this.recipients = recipients; } /** * Set the sender of this MailImpl. * * @param sender the sender of this MailImpl */ public void setSender(MailAddress sender) { this.sender = sender; } /** * Set the state of this MailImpl. * * @param state the state of this MailImpl */ public void setState(String state) { this.state = state; } /** * Set the remote address associated with this MailImpl. * * @param remoteHost the new remote host associated with this MailImpl */ public void setRemoteHost(String remoteHost) { this.remoteHost = remoteHost; } /** * Set the remote address associated with this MailImpl. * * @param remoteAddr the new remote address associated with this MailImpl */ 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 MimeMessage is not set for this MailImpl * @throws IOException if an error occurs while reading or writing from the stream */ public void writeMessageTo(OutputStream out) throws IOException, MessagingException { if (message != null) { message.writeTo(out); } else { throw new MessagingException("No message set for this MailImpl."); } } /** * 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 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 MailImpl( "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 MimeMessage is not set for this MailImpl * @throws IOException if an error occurs while reading or writing from the stream */ public void writeContentTo(OutputStream out, int lines) throws IOException, MessagingException { 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 MailImpl."); } } // Serializable Methods // TODO: These need some work. Currently very tightly coupled to // the internal representation. /** * Read the MailImpl 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 (ParseException 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(); } catch (OptionalDataException ode) { if (ode.eof) { attributes = new HashMap(); } else { throw ode; } } } /** * Write the MailImpl 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); } /** * @see org.apache.avalon.framework.activity.Disposable#dispose() */ public void dispose() { try { MimeMessage 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 + -