📄 jwmacomposemessage.java
字号:
throw new JwmaException("jwma.composemessage.send.sendermissing", true); }
//set content either single or multipart
if (isMultipart()) { //body represents the first part
if (getBody() != null && getBody().length() > 0) { MimeBodyPart bodypart = new MimeBodyPart(); bodypart.setText(getBody()); m_Attachments.addBodyPart(bodypart, 0); }
//set the content
m_Message.setContent(m_Attachments); } else { m_Message.setText(getBody()); }
//transport the message if (isInvisibleToList()) { log.debug("send()::" + "sending message seperately to receivers on list."); InternetAddress[] tos = (InternetAddress[]) m_Message.getRecipients(Message.RecipientType.TO); for (int i = 0; i < tos.length; i++) { setTo(tos[i]); ses.getJwmaTransport().sendMessage(m_Message); } //reset tos to remember for saving m_Message.setRecipients(Message.RecipientType.TO, tos); } else { ses.getJwmaTransport().sendMessage(m_Message); }
//flag the message as answered
try { if (isReply() && m_RepliedMessage != null) { Message msg = m_RepliedMessage.getMessage(); Folder fld = msg.getFolder(); fld.open(Folder.READ_WRITE); msg.setFlag(Flags.Flag.ANSWERED, true); fld.close(false); } } catch (MessagingException mex) { log.error("Failed to send message", mex);
//JwmaKernel.getReference().debugLog().writeStackTrace(mex); } } catch (MessagingException mex) { throw new JwmaException("jwma.composemessage.send.failed", true) .setException(mex); } }//send public void openDraft(JwmaStoreImpl store) throws JwmaException { log.debug("openDraft()"); try { if (isDraft()) { //existing draft will be updated //this should ensure the message is fetched RW?! Folder f = m_Message.getFolder(); f.open(Folder.READ_WRITE); //mark old deleted m_Message.setFlag(Flags.Flag.DELETED, true); m_Message = new MimeMessage(m_Message); f.close(false); } //else { //new draft will be appended //get the folder // Folder draft = store.getDraftFolder(); //open it read write //draft.open(Folder.READ_WRITE); //} } catch (Exception ex) { log.error("openDraft()", ex); throw new JwmaException("jwma.composemessage.draft.openfailed"); } }//openDraft public void closeDraft(JwmaStoreImpl store) throws JwmaException { //log.debug("closeDraft()"); try { //Assert that a valid sender identity is set. if (getFrom() == null || getFrom().equals("")) { throw new JwmaException("jwma.composemessage.sendermissing", true); } //set content either single or multipart if (isMultipart()) { //body represents the first part if (getBody() != null && getBody().length() > 0) { MimeBodyPart bodypart = new MimeBodyPart(); bodypart.setText(getBody()); m_Attachments.addBodyPart(bodypart, 0); } //set the content m_Message.setContent(m_Attachments); } else { m_Message.setText(getBody()); } //set draft flag m_Message.setFlag(Flags.Flag.DRAFT, true); m_Message.saveChanges(); Folder draft = store.getDraftFolder(); draft.open(Folder.READ_WRITE); Message[] tosave = {m_Message}; draft.appendMessages(tosave); draft.close(true); } catch (Exception ex) { log.error("closeDraft()", ex); throw new JwmaException("jwma.composemessage.draft.failed", true); } }//closeDraft /**
* Prepares the message instance, specifically for forwarding and replying,
* were the content probably has to be copied into the message to be
* composed.
* A copy of multi-parts will be attached to a forward if requested.
*
* @param msg the <tt>JwmaDisplayMessage</tt> instance that is replied
* to or forwarded.
* @param prefs the <tt>JwmaPreferences</tt> instance associated with the
* actual user (for settings).
* @param togglequote the flag that can toggle the setting from the user's
* preferences. * @param attfwd the flag that toggles whether attachments are forwarded * with this message.
*
* @throws JwmaException if there is a failure on accessing or quoting the
* given message's content.
*/ private void prepare(JwmaDisplayMessage msg, JwmaPreferences prefs, boolean togglequote, boolean attfwd) throws JwmaException { if (!m_Forward) { log.debug("Reply"); setSubject("Re: " + msg.getSubject()); //transfer quoted text if applicable
//there is a little bit of logic in the if ;)
if (prefs.isAutoQuote() != togglequote) { //FIXME!!!
//replace for textproc processor
//and add On blah blah wrote....
//retrieve the quotechar
String quotechar = prefs.getQuoteChar();
//Quote singlepart mail
if (msg.isSinglepart()) { //log.debug("Quoting single part message"); String messtr = msg.getBody(); StringBuffer rbodybuf = new StringBuffer(messtr.length() + 10); rbodybuf.append("\n\n"); rbodybuf.append(quotechar); boolean done = false; int pos = 0; int foundpos = 0; while (!done) { foundpos = messtr.indexOf(10, pos); if (foundpos != -1) { rbodybuf.append(messtr.substring(pos, foundpos + 1)); rbodybuf.append(quotechar); pos = foundpos + 1; } else { rbodybuf.append(messtr.substring(pos, messtr.length())); done = true; } } this.appendBody(rbodybuf.toString()); rbodybuf = null; messtr = null; } else { //log.debug("Quoting multi part message (text parts)."); JwmaMessagePart[] mparts = msg.getMessageParts(); //quote text parts of multipart messages for (int i = 0; i < mparts.length; i++) { JwmaMessagePartImpl part = (JwmaMessagePartImpl) mparts[i]; if (part.isMimeType("text/plain")) { //FIXME: Process to make quoted this.appendBody(part.getTextContent()); } } } } } else { //log.debug("Forwarding message."); setSubject("Fwd: " + msg.getSubject()); if (msg.isSinglepart()) { //FIXME: process to make quote this.appendBody(msg.getBody()); //log.debug("Quoting single part message"); } else { if (attfwd) { //log.debug("Forwarding message with attachments."); JwmaMessagePart[] mparts = msg.getMessageParts(); try { //1. get MimeMultiPart MimeMultipart mmp = (MimeMultipart) msg.getMessage().getContent(); MimeMultipart nmmp = new MimeMultipart(); ArrayList nonplaintextparts = new ArrayList(mmp.getCount()); //2. copy over contents for (int n = 0; n < mmp.getCount(); n++) { if (mparts[n].isMimeType("text/plain")) { //FIXME: process to make quote? this.appendBody(((JwmaMessagePartImpl) mparts[n]).getTextContent()); //log.debug("Forwarding message: quoted plain text attachment."); } else { MimeBodyPart mbp = (MimeBodyPart) mmp.getBodyPart(n); MimeBodyPart nmbp = new MimeBodyPart(); //copy all headers for (Enumeration enum = mbp.getAllHeaders(); enum.hasMoreElements();) { Header h = (Header) enum.nextElement(); nmbp.setHeader(h.getName(), h.getValue()); } //copy content InputStream in = mbp.getInputStream(); /*String[] encoding = mbp.getHeader("Content-Transfer-Encoding"); if (encoding != null && encoding.length > 0) { in = MimeUtility.decode(in, encoding[0]); } */ ByteArrayOutputStream bout = new ByteArrayOutputStream(); OutputStream out = (OutputStream) bout; byte[] buffer = new byte[10240]; int amount = 0; while ((amount = in.read(buffer)) >= 0) { out.write(buffer, 0, amount); } out.flush(); out.close(); //create the datasource MimeBodyPartDataSource mbpds = new MimeBodyPartDataSource( mparts[n].getContentType(), mparts[n].getName(), bout.toByteArray() ); nmbp.setDataHandler(new DataHandler(mbpds)); //ensure transfer encoding is set nmbp.setHeader("Content-Transfer-Encoding", "base64"); nmmp.addBodyPart(nmbp); nonplaintextparts.add(mparts[n]); //log.debug("Forwarding message: added attachment and partinfo."); } } //copy over non plain text message part infos m_Parts = new JwmaMessagePart[nonplaintextparts.size()]; m_Parts = (JwmaMessagePart[]) nonplaintextparts.toArray(m_Parts); //3. append message's attachments to this message this.addAttachments(nmmp); } catch (Exception ex) { throw new JwmaException("jwma.composemessage.failedattforward").setException(ex); } } else { //log.debug("Forwarding message without attachments."); JwmaMessagePart[] mparts = msg.getMessageParts(); //quote text parts of multipart messages for (int i = 0; i < mparts.length; i++) { JwmaMessagePartImpl part = (JwmaMessagePartImpl) mparts[i]; if (part.isMimeType("text/plain")) { //FIXME: Process to make quoted this.appendBody(part.getTextContent()); //log.debug("Forwarding message woa: quoted plain/text part."); } } } } }
//set sender identity from the active mailidentity
String from = prefs.getMailIdentity().getFrom(); if (from == null || from.length() == 0) { from = prefs.getFirstname() + " " + prefs.getLastname() + " <" + prefs.getUserIdentity() + ">"; } setFrom(from); }//prepare /**
* Creates a <tt>JwmaComposeMessage</tt> instance.
* <p>This factory method should be used to create new instances
* of <tt>JwmaComposeMessage</tt>, which are not a reply to
* an already existing message.
*
* @param ses the mail <tt>Session</tt> the message is created within.
* @return the newly created instance.
*/ public static JwmaComposeMessage createJwmaComposeMessage(Session ses) { JwmaComposeMessage message = null; message = new JwmaComposeMessage(new MimeMessage(ses)); message.setReply(false); return message; }//createJwmaComposeMessage /**
* Creates a <tt>JwmaComposeMessage</tt> instance.
* <p> This factory method should be used to create new instances
* of <tt>JwmaComposeMessage</tt>, which are a reply to
* an already existing message.
* <p>The reply will address the sender, and if flagged, also all
* recipients of the original <tt>Message</tt>.
*
*
* @param msg the <tt>Message</tt> to be replied.
* @param toall a flag determining wheter the reply should address also all
* recipients of the original <tt>Message</tt>.
* @return the newly created instance.
* @throws JwmaException if it fails to create the new instance.
*/ public static JwmaComposeMessage createReply(JwmaDisplayMessage msg, boolean toall, JwmaPreferences prefs, boolean togglequote) throws JwmaException { JwmaComposeMessage message = null; try { message = new JwmaComposeMessage( (MimeMessage) msg.getMessage().reply(toall) ); message.setRepliedMessage(msg); message.setReply(true); message.prepare(msg, prefs, togglequote, false); } catch (MessagingException mex) { throw new JwmaException("jwma.composemessage.failedcreation", true) .setException(mex); } return message; }//createReply /**
* Creates a <tt>JwmaComposeMessage</tt> instance.
* <p> This factory method should be used to create new instances
* of <tt>JwmaComposeMessage</tt>, which are a reply to
* an already existing message.
* <p>The reply will address the sender, and if flagged, also all
* recipients of the original <tt>Message</tt>.
*
*
* @param msg the <tt>Message</tt> to be replied.
* @param toall a flag determining wheter the reply should address also all
* recipients of the original <tt>Message</tt>.
* @return the newly created instance.
* @throws JwmaException if it fails to create the new instance.
*/ public static JwmaComposeMessage createForward(Session ses, JwmaDisplayMessage msg, String to, JwmaPreferences prefs, boolean togglequote, boolean attfwd) throws JwmaException { log.debug("createForward()"); JwmaComposeMessage message = createJwmaComposeMessage(ses); try { message.setForward(true); message.setTo(to); Folder fld = msg.getMessage().getFolder(); fld.open(Folder.READ_ONLY); message.prepare(msg, prefs, togglequote, attfwd); fld.close(false); } catch (MessagingException mex) { throw new JwmaException("jwma.composemessage.failedcreation", true) .setException(mex); } return message; }//createReply public static JwmaComposeMessage createDraft(Message msg) throws JwmaException { JwmaComposeMessage message = new JwmaComposeMessage( (MimeMessage) msg ); try { message.setDraft(true); message.setMessageNumber(msg.getMessageNumber()); message.setBody(msg.getContent().toString()); } catch (Exception ex) { throw new JwmaException("jwma.composemessage.failedcreation", true) .setException(ex); } return message; }//createDraft /**
* Defines the X-MAILER set by jwma.
* This can probably help one day to recognize special mailer features.
*/ public static final String X_MAILER_STRING = "jwma";}//class JwmaComposeMessage
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -