Example usage for javax.mail.internet MimeUtility fold

List of usage examples for javax.mail.internet MimeUtility fold

Introduction

In this page you can find the example usage for javax.mail.internet MimeUtility fold.

Prototype

public static String fold(int used, String s) 

Source Link

Document

Fold a string at linear whitespace so that each line is no longer than 76 characters, if possible.

Usage

From source file:mitm.application.djigzo.james.mailets.DKIMSign.java

@Override
public void serviceMail(Mail mail) {
    try {/*from  w  w w  .  j a  va 2s  . c o m*/
        MimeMessage message = mail.getMessage();

        if (convertTo7Bit) {
            /*
             * Before signing, the message should be converted to 7bit to make sure
             * the signature 'survives' the internet.
             */
            boolean converted = MailUtils.convertTo7Bit(message);

            if (converted) {
                message.saveChanges();
            }
        }

        PrivateKey privateKey = getPrivateKey(mail);

        if (privateKey != null) {
            DKIMSigner signer = new DKIMSigner(getSignatureTemplate(), privateKey);

            signer.setDKIMHeader(dkimHeader);

            String signature = signer.sign(message);

            /*
             * the signature field cannot be folded after signing in the simple mode
             * because that would break the signature
             */
            if (foldSignature) {
                signature = MimeUtility.fold(dkimHeader.length(), signature);
            }

            HeaderUtils.prependHeaderLine(message, signature);

            /*
             * Validate the message to make sure that James can write the message. 
             * 
             * Note: Because normally only a header is added, the message-id is not changed. The
             * exception to this is when the message is converted from 8bit to 7bit (see above).
             * When the message is converted from 7bit to 8bit the message-id is changed.
             */
            MailUtils.validateMessage(message);
        } else {
            logger.warn("Unable to sign the message because the private key is missing.");
        }
    } catch (MessagingException e) {
        logger.error("DKIM signing failed.", e);
    } catch (IOException e) {
        logger.error("DKIM signing failed.", e);
    } catch (FailException e) {
        logger.error("DKIM signing failed.", e);
    }
}

From source file:com.sonicle.webtop.mail.Service.java

public Message reply(MailAccount account, MimeMessage orig, boolean replyToAll, boolean fromSent)
        throws MessagingException {
    MimeMessage reply = new MimeMessage(account.getMailSession());
    /*//  w  w  w . j av  a 2 s .c  om
     * Have to manipulate the raw Subject header so that we don't lose
     * any encoding information.  This is safe because "Re:" isn't
     * internationalized and (generally) isn't encoded.  If the entire
     * Subject header is encoded, prefixing it with "Re: " still leaves
     * a valid and correct encoded header.
     */
    String subject = orig.getHeader("Subject", null);
    if (subject != null) {
        if (!subject.regionMatches(true, 0, "Re: ", 0, 4)) {
            subject = "Re: " + subject;
        }
        reply.setHeader("Subject", subject);
    }
    Address a[] = null;
    if (!fromSent)
        a = orig.getReplyTo();
    else {
        Address ax[] = orig.getRecipients(RecipientType.TO);
        if (ax != null) {
            a = new Address[1];
            a[0] = ax[0];
        }
    }
    reply.setRecipients(Message.RecipientType.TO, a);
    if (replyToAll) {
        Vector v = new Vector();
        Session session = account.getMailSession();
        // add my own address to list
        InternetAddress me = InternetAddress.getLocalAddress(session);
        if (me != null) {
            v.addElement(me);
        }
        // add any alternate names I'm known by
        String alternates = null;
        if (session != null) {
            alternates = session.getProperty("mail.alternates");
        }
        if (alternates != null) {
            eliminateDuplicates(v, InternetAddress.parse(alternates, false));
        }
        // should we Cc all other original recipients?
        String replyallccStr = null;
        boolean replyallcc = false;
        if (session != null) {
            replyallcc = PropUtil.getBooleanSessionProperty(session, "mail.replyallcc", false);
        }
        // add the recipients from the To field so far
        eliminateDuplicates(v, a);
        a = orig.getRecipients(Message.RecipientType.TO);
        a = eliminateDuplicates(v, a);
        if (a != null && a.length > 0) {
            if (replyallcc) {
                reply.addRecipients(Message.RecipientType.CC, a);
            } else {
                reply.addRecipients(Message.RecipientType.TO, a);
            }
        }
        a = orig.getRecipients(Message.RecipientType.CC);
        a = eliminateDuplicates(v, a);
        if (a != null && a.length > 0) {
            reply.addRecipients(Message.RecipientType.CC, a);
        }
        // don't eliminate duplicate newsgroups
        a = orig.getRecipients(MimeMessage.RecipientType.NEWSGROUPS);
        if (a != null && a.length > 0) {
            reply.setRecipients(MimeMessage.RecipientType.NEWSGROUPS, a);
        }
    }

    String msgId = orig.getHeader("Message-Id", null);
    if (msgId != null) {
        reply.setHeader("In-Reply-To", msgId);
    }

    /*
     * Set the References header as described in RFC 2822:
     *
     * The "References:" field will contain the contents of the parent's
     * "References:" field (if any) followed by the contents of the parent's
     * "Message-ID:" field (if any).  If the parent message does not contain
     * a "References:" field but does have an "In-Reply-To:" field
     * containing a single message identifier, then the "References:" field
     * will contain the contents of the parent's "In-Reply-To:" field
     * followed by the contents of the parent's "Message-ID:" field (if
     * any).  If the parent has none of the "References:", "In-Reply-To:",
     * or "Message-ID:" fields, then the new message will have no
     * "References:" field.
     */
    String refs = orig.getHeader("References", " ");
    if (refs == null) {
        // XXX - should only use if it contains a single message identifier
        refs = orig.getHeader("In-Reply-To", " ");
    }
    if (msgId != null) {
        if (refs != null) {
            refs = MimeUtility.unfold(refs) + " " + msgId;
        } else {
            refs = msgId;
        }
    }
    if (refs != null) {
        reply.setHeader("References", MimeUtility.fold(12, refs));
    }

    //try {
    //    setFlags(answeredFlag, true);
    //} catch (MessagingException mex) {
    //    // ignore it
    //}
    return reply;
}