📄 jwmacomposemessage.java
字号:
/***
* jwma Java WebMail
* Copyright (c) 2000-2003 jwma team
*
* jwma is free software; you can distribute and use this source
* under the terms of the BSD-style license received along with
* the distribution.
***/package dtw.webmail.model;import java.util.*;import java.io.*;import javax.mail.*;import javax.mail.internet.*;import javax.activation.*;import org.apache.log4j.Logger;import dtw.webmail.util.EntityHandler;import dtw.webmail.util.MimeBodyPartDataSource;import dtw.webmail.JwmaSession;//import dtw.webmail.JwmaKernel;/**
* Class implementing the JwmaComposeMessage model.
* <p>
* This implementation is specialized to wrap a
* <tt>javax.mail.Message</tt> for composing.
*
* @author Dieter Wimberger
* @version 0.9.7 07/02/2003
*/public class JwmaComposeMessage implements JwmaMessage { //logging private static Logger log = Logger.getLogger(JwmaComposeMessage.class); //instance attributes private boolean m_Reply; private boolean m_Forward; private boolean m_Draft; private StringBuffer m_Body; private MimeMessage m_Message; private MimeMultipart m_Attachments; private boolean m_Singlepart = true; private JwmaDisplayMessage m_RepliedMessage; private boolean m_InvisibleToList = false; private JwmaMessagePart[] m_Parts; private int m_Number = -1; /**
* Constructs a <tt>JwmaComposeMessage</tt> instance.
*/ private JwmaComposeMessage() { }//constructor /**
* Constructs a <tt>JwmaComposeMessage</tt> instance.
*
* @param msg the aggregated <tt>MimeMessage</tt>.
*/ private JwmaComposeMessage(MimeMessage msg) { m_Message = msg; m_Attachments = new MimeMultipart(); m_Parts = new JwmaMessagePart[0]; try { m_Message.setHeader("X-Mailer", X_MAILER_STRING); } catch (Exception ex) { } }//constructor public String getFullHeader() { StringBuffer fullhead = new StringBuffer(); try { for (Enumeration enum = m_Message.getAllHeaderLines(); enum.hasMoreElements();) { fullhead.append((String) enum.nextElement()) .append("\n"); } } catch (MessagingException mex) { } return fullhead.toString(); }//getFullHeader public int getMessageNumber() { return m_Number; }//getMessageNumber private void setMessageNumber(int nr) { m_Number = nr; }//setMessageNumber public Date getReceivedDate() { return null; }//getDate public Date getSentDate() { return null; }//getSentDate public Date getDate() { return null; }//getDate public boolean isSent() { return false; }//isSent public boolean isReceived() { return false; }//isReceived /**
* Tests if the message is a reply.
*
* @return true if the message is a reply,
* false otherwise.
*/ public boolean isReply() { return m_Reply; }//isReply public boolean isDraft() { return m_Draft; }//isDraft /**
* Flags the message as a forward.
*
* @param true if the message is a forward,
* false otherwise.
*/ private void setForward(boolean b) { m_Forward = b; }//setForward /**
* Flags the message as a reply.
*
* @param true if the message is a reply,
* false otherwise.
*/ private void setReply(boolean b) { m_Reply = b; }//setReply /** * Flags the message as a draft. * * @param true if the message is a draft, * false otherwise. */ private void setDraft(boolean b) { m_Draft = b; }//setDraft /**
* Sets the message that is being replied to.
* In case the message gets send, this can be
* used to set the answered flag.
* Normally this is done by calling reply in the JavaMail API,
* but as the folder is not open in READ_WRITE mode,
* and even if it was, the message might never get send,
* this is the consistent way.
*/ private void setRepliedMessage(JwmaDisplayMessage msg) { m_RepliedMessage = msg; }//setRepliedMessage public String getFrom() { try { return EntityHandler.encode( InternetAddress.toString(m_Message.getFrom()) ); } catch (Exception ex) { return ""; } }//getFrom /**
* Set the sender(s) address of the message.
*
* @param from the sender(s) address of the message
* as <tt>String</tt>.
*/ public void setFrom(String from) { try { m_Message.setFrom(new InternetAddress(from)); } catch (Exception ex) { } }//setFrom public String getReplyTo() { try { return InternetAddress.toString(m_Message.getReplyTo()); } catch (Exception ex) { return ""; } }//getReplyTo /**
* Set the Reply-To address(es) of the message.
*
* @param from the Reply-To address(es) of the message
* as <tt>String</tt>.
*/ public void setReplyTo(String from) { try { m_Message.setReplyTo(InternetAddress.parse(from)); } catch (Exception ex) { } }//setReplyTo public String getTo() { String str = ""; try { str = InternetAddress.toString(m_Message.getRecipients(Message.RecipientType.TO)); if (str == null) { str = ""; } } catch (Exception ex) { return str; } return str; }//getTo /**
* Sets the receiver's address(es) of the message as
* <tt>String</tt>.
*
* @param to the receiver(s) address(es) of the message as String.
*
* @throws MessagingException if the receiver's address(es) is (are) malformed.
*/ public void setTo(String to) throws MessagingException { if (to == null || to.equals("")) { return; } else { m_Message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); } }//setTo private void setTo(Address to) throws MessagingException { m_Message.setRecipient(Message.RecipientType.TO, to); }//setTo public boolean isInvisibleToList() { return m_InvisibleToList; }//isInvisibleToList public void setInvisibleToList(boolean invisibleToList) { m_InvisibleToList = invisibleToList; }//setInvisibleToList public String getCCTo() { String str = ""; try { str = InternetAddress.toString(m_Message.getRecipients(Message.RecipientType.CC)); if (str == null) { str = ""; } } catch (Exception ex) { return str; } return str; }//getCCTo /**
* Sets the carbon copy receiver's address(es) of the message.
*
* @param to the carbon copy receiver(s) address(es) of the
* message as String.
*
* @throws MessagingException if the carbon copy receiver's address(es)
* is (are) malformed.
*/ public void setCCTo(String ccto) throws MessagingException { m_Message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccto)); }//setCCTo; public String getBCCTo() { String str = ""; try { str = InternetAddress.toString(m_Message.getRecipients(Message.RecipientType.BCC)); if (str == null) { str = ""; } } catch (Exception ex) { return str; } return str; }//getBCCTo /**
* Sets the blind carbon copy receiver's address(es) of the message.
*
* @param to the blind carbon copy receiver(s) address(es) of the
* message as String.
*
* @throws MessagingException if the blind carbon copy receiver's address(es)
* is (are) malformed.
*/ public void setBCCTo(String bccto) throws MessagingException { m_Message.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bccto)); }//setBCCTo; public String getSubject() { String str = ""; try { str = MimeUtility.decodeText(m_Message.getSubject()); if (str == null) { str = ""; } } catch (Exception ex) { return str; } return str; }//getSubject /**
* Sets the subject of the message.
*
* @param subject the subject of the message as <tt>String</tt>.
*
* @throws JwmaException if the string is malformed (encoding, null).
*/ public void setSubject(String subject) throws JwmaException { try { if (subject != null) { m_Message.setSubject(MimeUtility.encodeText(subject)); } else { m_Message.setSubject(""); } } catch (Exception ex) { } }//setSubject public String getBody() { if (m_Body == null || m_Body.length() == 0) { return ""; } else { return m_Body.toString(); } }//getBody /**
* Sets the body of the message.
* <p>
* Note that the body will be the text/plain content of
* a singlepart message, and attached as text/plain part to
* a multipart message.
*
* @param body the text/plain content of the message as
* <tt>String</tt>.
*/ public void setBody(String body) { if (body != null) { m_Body = new StringBuffer(body); } else { m_Body = new StringBuffer(); } }//setBody /**
* Appends to the body of the message.
*
* @param str more text/plain content for the
* message as <tt>String</tt>.
*/ public void appendBody(String str) { if (m_Body == null) { m_Body = new StringBuffer(str); } else { m_Body.append(str); } }//appendBody /**
* Adds attachments to this <tt>JwmaComposeMessage</tt>.
*
* @param mmp the <tt>MimeMultipart</tt> that represents the
* attachments.
*/ public void addAttachments(MimeMultipart mmp) { try { for (int i = 0; i < mmp.getCount(); i++) { m_Attachments.addBodyPart((MimeBodyPart) mmp.getBodyPart(i)); } m_Singlepart = false; } catch (Exception ex) { log.error("addAttachments()", ex); } }//addAttachments public boolean isSinglepart() { return m_Singlepart; }//isSinglepart public boolean isMultipart() { return !m_Singlepart; }//isMultipart public JwmaMessagePart[] getMessageParts() { return m_Parts; }//getMessageParts /**
* Returns the <tt>Message</tt> instance associated
* with this <tt>JwmaComposeMessage</tt>.
*
* @return associated <tt>Message</tt> instance.
*/ public Message getMessage() { return m_Message; }//getMessage /**
* Sends this instance via the standard convenience <tt>Transport.send()</tt>.
* <p>
* Asserts that a sender is set, and creates either a multipart
* or a singlepart <tt>Message</tt> from the data stored
* in this instance.
*
* @throws JwmaException If there is no sender, or if sending fails.
*/ public void send(JwmaSession ses) throws JwmaException { try { //Assert that a valid sender identity is set.
if (getFrom() == null || getFrom().equals("")) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -