Search in sources :

Example 1 with BodyPart

use of javax.mail.BodyPart in project camel by apache.

the class MailBinding method addBodyToMultipart.

protected void addBodyToMultipart(MailConfiguration configuration, MimeMultipart activeMultipart, Exchange exchange) throws MessagingException, IOException {
    BodyPart bodyMessage = new MimeBodyPart();
    populateContentOnBodyPart(bodyMessage, configuration, exchange);
    activeMultipart.addBodyPart(bodyMessage);
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 2 with BodyPart

use of javax.mail.BodyPart in project camel by apache.

the class MailBinding method addAttachmentsToMultipart.

protected void addAttachmentsToMultipart(MimeMultipart multipart, String partDisposition, AttachmentsContentTransferEncodingResolver encodingResolver, Exchange exchange) throws MessagingException {
    LOG.trace("Adding attachments +++ start +++");
    int i = 0;
    for (Map.Entry<String, Attachment> entry : exchange.getIn().getAttachmentObjects().entrySet()) {
        String attachmentFilename = entry.getKey();
        Attachment attachment = entry.getValue();
        if (LOG.isTraceEnabled()) {
            LOG.trace("Attachment #{}: Disposition: {}", i, partDisposition);
            LOG.trace("Attachment #{}: DataHandler: {}", i, attachment.getDataHandler());
            LOG.trace("Attachment #{}: FileName: {}", i, attachmentFilename);
        }
        if (attachment != null) {
            if (shouldAddAttachment(exchange, attachmentFilename, attachment.getDataHandler())) {
                // Create another body part
                BodyPart messageBodyPart = new MimeBodyPart();
                // Set the data handler to the attachment
                messageBodyPart.setDataHandler(attachment.getDataHandler());
                // Set headers to the attachment
                for (String headerName : attachment.getHeaderNames()) {
                    List<String> values = attachment.getHeaderAsList(headerName);
                    for (String value : values) {
                        messageBodyPart.setHeader(headerName, value);
                    }
                }
                if (attachmentFilename.toLowerCase().startsWith("cid:")) {
                    // add a Content-ID header to the attachment
                    // must use angle brackets according to RFC: http://www.ietf.org/rfc/rfc2392.txt
                    messageBodyPart.addHeader("Content-ID", "<" + attachmentFilename.substring(4) + ">");
                    // Set the filename without the cid
                    messageBodyPart.setFileName(attachmentFilename.substring(4));
                } else {
                    // Set the filename
                    messageBodyPart.setFileName(attachmentFilename);
                }
                LOG.trace("Attachment #" + i + ": ContentType: " + messageBodyPart.getContentType());
                if (contentTypeResolver != null) {
                    String contentType = contentTypeResolver.resolveContentType(attachmentFilename);
                    LOG.trace("Attachment #" + i + ": Using content type resolver: " + contentTypeResolver + " resolved content type as: " + contentType);
                    if (contentType != null) {
                        String value = contentType + "; name=" + attachmentFilename;
                        messageBodyPart.setHeader("Content-Type", value);
                        LOG.trace("Attachment #" + i + ": ContentType: " + messageBodyPart.getContentType());
                    }
                }
                // set Content-Transfer-Encoding using resolver if possible
                resolveContentTransferEncoding(encodingResolver, i, messageBodyPart);
                // Set Disposition
                messageBodyPart.setDisposition(partDisposition);
                // Add part to multipart
                multipart.addBodyPart(messageBodyPart);
            } else {
                LOG.trace("shouldAddAttachment: false");
            }
        } else {
            LOG.warn("Cannot add attachment: " + attachmentFilename + " as DataHandler is null");
        }
        i++;
    }
    LOG.trace("Adding attachments +++ done +++");
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) Attachment(org.apache.camel.Attachment) DefaultAttachment(org.apache.camel.impl.DefaultAttachment) MimeBodyPart(javax.mail.internet.MimeBodyPart) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 3 with BodyPart

use of javax.mail.BodyPart in project camel by apache.

the class MailBinding method extractHeadersFromMail.

protected Map<String, Object> extractHeadersFromMail(Message mailMessage, Exchange exchange) throws MessagingException, IOException {
    Map<String, Object> answer = new TreeMap<String, Object>(String.CASE_INSENSITIVE_ORDER);
    Enumeration<?> names = mailMessage.getAllHeaders();
    while (names.hasMoreElements()) {
        Header header = (Header) names.nextElement();
        String value = header.getValue();
        if (headerFilterStrategy != null && !headerFilterStrategy.applyFilterToExternalHeaders(header.getName(), value, exchange)) {
            CollectionHelper.appendValue(answer, header.getName(), value);
        }
    }
    // if the message is a multipart message, do not set the content type to multipart/*
    if (((MailEndpoint) exchange.getFromEndpoint()).getConfiguration().isMapMailMessage()) {
        Object content = mailMessage.getContent();
        if (content instanceof MimeMultipart) {
            MimeMultipart multipart = (MimeMultipart) content;
            int size = multipart.getCount();
            for (int i = 0; i < size; i++) {
                BodyPart part = multipart.getBodyPart(i);
                content = part.getContent();
                // in case of nested multiparts iterate into them
                while (content instanceof MimeMultipart) {
                    if (multipart.getCount() < 1) {
                        break;
                    }
                    part = ((MimeMultipart) content).getBodyPart(0);
                    content = part.getContent();
                }
                if (part.getContentType().toLowerCase().startsWith("text")) {
                    answer.put(Exchange.CONTENT_TYPE, part.getContentType());
                    break;
                }
            }
        }
    }
    return answer;
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) Header(javax.mail.Header) MimeMultipart(javax.mail.internet.MimeMultipart) TreeMap(java.util.TreeMap)

Example 4 with BodyPart

use of javax.mail.BodyPart in project camel by apache.

the class MailBinding method createMultipartAlternativeMessage.

protected void createMultipartAlternativeMessage(MimeMessage mimeMessage, MailConfiguration configuration, Exchange exchange) throws MessagingException, IOException {
    MimeMultipart multipartAlternative = new MimeMultipart("alternative");
    mimeMessage.setContent(multipartAlternative);
    MimeBodyPart plainText = new MimeBodyPart();
    plainText.setText(getAlternativeBody(configuration, exchange), determineCharSet(configuration, exchange));
    // remove the header with the alternative mail now that we got it
    // otherwise it might end up twice in the mail reader
    exchange.getIn().removeHeader(configuration.getAlternativeBodyHeader());
    multipartAlternative.addBodyPart(plainText);
    // if there are no attachments, add the body to the same mulitpart message
    if (!exchange.getIn().hasAttachments()) {
        addBodyToMultipart(configuration, multipartAlternative, exchange);
    } else {
        // treat them as normal. It will append a multipart-mixed with the attachments and the body text
        if (!configuration.isUseInlineAttachments()) {
            BodyPart mixedAttachments = new MimeBodyPart();
            mixedAttachments.setContent(createMixedMultipartAttachments(configuration, exchange));
            multipartAlternative.addBodyPart(mixedAttachments);
        } else {
            // if the attachments are set to be inline, attach them as inline attachments
            MimeMultipart multipartRelated = new MimeMultipart("related");
            BodyPart related = new MimeBodyPart();
            related.setContent(multipartRelated);
            multipartAlternative.addBodyPart(related);
            addBodyToMultipart(configuration, multipartRelated, exchange);
            AttachmentsContentTransferEncodingResolver resolver = configuration.getAttachmentsContentTransferEncodingResolver();
            addAttachmentsToMultipart(multipartRelated, Part.INLINE, resolver, exchange);
        }
    }
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 5 with BodyPart

use of javax.mail.BodyPart in project KJFrameForAndroid by kymjs.

the class SimpleMailSender method sendHtmlMail.

/**
     * 以HTML格式发送邮件
     * 
     * @param mailInfo
     *            待发送的邮件信息
     */
public static boolean sendHtmlMail(MailSenderInfo mailInfo) {
    // 判断是否需要身份认证
    MyAuthenticator authenticator = null;
    Properties pro = mailInfo.getProperties();
    // 如果需要身份认证,则创建一个密码验证器
    if (mailInfo.isValidate()) {
        authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
    }
    // 根据邮件会话属性和密码验证器构造一个发送邮件的session
    Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
    try {
        // 根据session创建一个邮件消息
        Message mailMessage = new MimeMessage(sendMailSession);
        // 创建邮件发送者地址
        Address from = new InternetAddress(mailInfo.getFromAddress());
        // 设置邮件消息的发送者
        mailMessage.setFrom(from);
        // 创建邮件的接收者地址,并设置到邮件消息中
        Address to = new InternetAddress(mailInfo.getToAddress());
        // Message.RecipientType.TO属性表示接收者的类型为TO
        mailMessage.setRecipient(Message.RecipientType.TO, to);
        // 设置邮件消息的主题
        mailMessage.setSubject(mailInfo.getSubject());
        // 设置邮件消息发送的时间
        mailMessage.setSentDate(new Date());
        // MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
        Multipart mainPart = new MimeMultipart();
        // 创建一个包含HTML内容的MimeBodyPart
        BodyPart html = new MimeBodyPart();
        // 设置HTML内容
        html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
        mainPart.addBodyPart(html);
        // 将MiniMultipart对象设置为邮件内容
        mailMessage.setContent(mainPart);
        // 发送邮件
        Transport.send(mailMessage);
        return true;
    } catch (MessagingException ex) {
        ex.printStackTrace();
    }
    return false;
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) InternetAddress(javax.mail.internet.InternetAddress) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MessagingException(javax.mail.MessagingException) Properties(java.util.Properties) Date(java.util.Date) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart) Session(javax.mail.Session)

Aggregations

BodyPart (javax.mail.BodyPart)71 MimeMultipart (javax.mail.internet.MimeMultipart)52 MimeBodyPart (javax.mail.internet.MimeBodyPart)41 MessagingException (javax.mail.MessagingException)23 MimeMessage (javax.mail.internet.MimeMessage)19 Header (javax.mail.Header)15 Multipart (javax.mail.Multipart)15 DispositionNotification (com.sun.mail.dsn.DispositionNotification)13 IOException (java.io.IOException)13 HashMap (java.util.HashMap)13 InternetHeaders (javax.mail.internet.InternetHeaders)13 ByteString (com.linkedin.data.ByteString)12 DataHandler (javax.activation.DataHandler)10 Test (org.testng.annotations.Test)10 Session (javax.mail.Session)9 InternetAddress (javax.mail.internet.InternetAddress)8 Properties (java.util.Properties)7 File (java.io.File)6 InputStream (java.io.InputStream)6 FileDataSource (javax.activation.FileDataSource)6