Java Code Examples for javax.mail.internet.MimeMessage#getContentType()

The following examples show how to use javax.mail.internet.MimeMessage#getContentType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HC4DavExchangeSession.java    From davmail with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void sendMessage(MimeMessage mimeMessage) throws IOException {
    try {
        // need to create draft first
        String itemName = UUID.randomUUID().toString() + ".EML";
        HashMap<String, String> properties = new HashMap<>();
        properties.put("draft", "9");
        String contentType = mimeMessage.getContentType();
        if (contentType != null && contentType.startsWith("text/plain")) {
            properties.put("messageFormat", "1");
        } else {
            properties.put("mailOverrideFormat", String.valueOf(ENCODING_PREFERENCE | ENCODING_MIME | BODY_ENCODING_TEXT_AND_HTML));
            properties.put("messageFormat", "2");
        }
        createMessage(DRAFTS, itemName, properties, mimeMessage);
        HttpMove httpMove = new HttpMove(URIUtil.encodePath(getFolderPath(DRAFTS + '/' + itemName)),
                URIUtil.encodePath(getFolderPath(SENDMSG)), false);
        // set header if saveInSent is disabled
        if (!Settings.getBooleanProperty("davmail.smtpSaveInSent", true)) {
            httpMove.setHeader("Saveinsent", "f");
        }
        moveItem(httpMove);
    } catch (MessagingException e) {
        throw new IOException(e.getMessage());
    }
}
 
Example 2
Source File: DavExchangeSession.java    From davmail with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void sendMessage(MimeMessage mimeMessage) throws IOException {
    try {
        // need to create draft first
        String itemName = UUID.randomUUID().toString() + ".EML";
        HashMap<String, String> properties = new HashMap<>();
        properties.put("draft", "9");
        String contentType = mimeMessage.getContentType();
        if (contentType != null && contentType.startsWith("text/plain")) {
            properties.put("messageFormat", "1");
        } else {
            properties.put("mailOverrideFormat", String.valueOf(ENCODING_PREFERENCE | ENCODING_MIME | BODY_ENCODING_TEXT_AND_HTML));
            properties.put("messageFormat", "2");
        }
        createMessage(DRAFTS, itemName, properties, mimeMessage);
        MoveMethod method = new MoveMethod(URIUtil.encodePath(getFolderPath(DRAFTS + '/' + itemName)),
                URIUtil.encodePath(getFolderPath(SENDMSG)), false);
        // set header if saveInSent is disabled 
        if (!Settings.getBooleanProperty("davmail.smtpSaveInSent", true)) {
            method.setRequestHeader("Saveinsent", "f");
        }
        moveItem(method);
    } catch (MessagingException e) {
        throw new IOException(e.getMessage());
    }
}
 
Example 3
Source File: MimeMessageWrapper.java    From scipio-erp with Apache License 2.0 6 votes vote down vote up
public synchronized void setMessage(MimeMessage message) {
    if (message != null) {
        // serialize the message
        this.message = message;
        try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
            message.writeTo(baos);
            baos.flush();
            serializedBytes = baos.toByteArray();
            this.contentType = message.getContentType();

            // see if this is a multi-part message
            Object content = message.getContent();
            if (content instanceof Multipart) {
                Multipart mp = (Multipart) content;
                this.parts = mp.getCount();
            } else {
                this.parts = 0;
            }
        } catch (IOException | MessagingException e) {
            Debug.logError(e, module);
        }
    }
}
 
Example 4
Source File: Sign.java    From james-project with Apache License 2.0 4 votes vote down vote up
/**
 * A text file with the massaged contents of {@link #getExplanationText}
 * is attached to the original message.
 */    
@Override
protected MimeBodyPart getWrapperBodyPart(Mail mail) throws MessagingException, IOException {
    
    String explanationText = getExplanationText();
    
    // if there is no explanation text there should be no wrapping
    if (explanationText == null) {
        return null;
    }

        MimeMessage originalMessage = mail.getMessage();

        MimeBodyPart messagePart = new MimeBodyPart();
        MimeBodyPart signatureReason = new MimeBodyPart();
        
        String contentType = originalMessage.getContentType();
        Object content = originalMessage.getContent();
        
        if (contentType != null && content != null) {
        messagePart.setContent(content, contentType);
        } else {
            throw new MessagingException("Either the content type or the content is null");
        }
        
        String headers = getMessageHeaders(originalMessage);
        
        signatureReason.setText(getReplacedExplanationText(getExplanationText(),
                                                           getSignerName(),
                                                           getKeyHolder().getSignerAddress(),
                                                           mail.getMaybeSender().asString(),
                                                           headers));
        
        signatureReason.setFileName("SignatureExplanation.txt");
        
        MimeMultipart wrapperMultiPart = new MimeMultipart();
        
        wrapperMultiPart.addBodyPart(messagePart);
        wrapperMultiPart.addBodyPart(signatureReason);
        
        MimeBodyPart wrapperBodyPart = new MimeBodyPart();
        
        wrapperBodyPart.setContent(wrapperMultiPart);
        
        return wrapperBodyPart;
}
 
Example 5
Source File: SMIMESign.java    From james-project with Apache License 2.0 4 votes vote down vote up
/**
 * A text file with the massaged contents of {@link #getExplanationText}
 * is attached to the original message.
 */    
@Override
protected MimeBodyPart getWrapperBodyPart(Mail mail) throws MessagingException, IOException {
    
    String explanationText = getExplanationText();
    
    // if there is no explanation text there should be no wrapping
    if (explanationText == null) {
        return null;
    }

        MimeMessage originalMessage = mail.getMessage();

        MimeBodyPart messagePart = new MimeBodyPart();
        MimeBodyPart signatureReason = new MimeBodyPart();
        
        String contentType = originalMessage.getContentType();
        Object content = originalMessage.getContent();
        
        if (contentType != null && content != null) {
        messagePart.setContent(content, contentType);
        } else {
            throw new MessagingException("Either the content type or the content is null");
        }
        
        String headers = getMessageHeaders(originalMessage);
        
        signatureReason.setText(getReplacedExplanationText(getExplanationText(),
                                                           getSignerName(),
                                                           getKeyHolder().getSignerAddress(),
                                                           mail.getMaybeSender().asString(),
                                                           headers));
        
        signatureReason.setFileName("SignatureExplanation.txt");
        
        MimeMultipart wrapperMultiPart = new MimeMultipart();
        
        wrapperMultiPart.addBodyPart(messagePart);
        wrapperMultiPart.addBodyPart(signatureReason);
        
        MimeBodyPart wrapperBodyPart = new MimeBodyPart();
        
        wrapperBodyPart.setContent(wrapperMultiPart);
        
        return wrapperBodyPart;
}