public void prepareMessage()

in common/servicemix-components/src/main/java/org/apache/servicemix/components/email/MimeMailMarshaler.java [155:240]


    public void prepareMessage(MimeMessage mimeMessage, MessageExchange exchange, NormalizedMessage normalizedMessage) throws javax.mail.MessagingException {
        try {
            Address[] to = getTo(exchange, normalizedMessage);
            if (to != null) {
                mimeMessage.setRecipients(Message.RecipientType.TO, to);
            }
            Address[] cc = getCc(exchange, normalizedMessage);
            if (cc != null) {
                mimeMessage.setRecipients(Message.RecipientType.CC, cc);
            }
            Address[] bcc = getBcc(exchange, normalizedMessage);
            if (bcc != null) {
                mimeMessage.setRecipients(Message.RecipientType.BCC, bcc);
            }
            Address from = getFrom(exchange, normalizedMessage);
            if (from != null) {
                mimeMessage.setFrom(from);
            }
            String text = getText(exchange, normalizedMessage);
            String html = getHtml(exchange, normalizedMessage);
            if ((text != null) && (html == null)) {
                mimeMessage.setText(text);
            }
            else if ((text != null) && (html != null)) {
                MimeMultipart content = new MimeMultipart("alternative");
                MimeBodyPart textBodyPart = new MimeBodyPart();
                MimeBodyPart htmlBodyPart = new MimeBodyPart();
                textBodyPart.setText(text);
                htmlBodyPart.setContent(html, "text/html");
                content.addBodyPart(textBodyPart);
                content.addBodyPart(htmlBodyPart);

                mimeMessage.setContent(content);
            }
            String subject = getSubject(exchange, normalizedMessage);
            if (subject != null) {
                mimeMessage.setSubject(subject);
            }
            Date sentDate = getSentDate(exchange, normalizedMessage);
            if (sentDate != null) {
                mimeMessage.setSentDate(sentDate);
            }
            Address[] replyTo = getReplyTo(exchange, normalizedMessage);
            if (replyTo != null) {
                mimeMessage.setReplyTo(replyTo);
            }

            // add attachment from message and properties
            HashMap attachments = this.getAttachments(exchange, normalizedMessage);
            if (attachments != null) {
                Set attNames = attachments.keySet();
                Iterator itAttNames = attNames.iterator();
                if (itAttNames.hasNext()) { // there is at least one attachment
                    // Create the message part
                    BodyPart messageBodyPart = new MimeBodyPart();
                    // Fill the message
                    messageBodyPart.setText(text);
                    // Create a Multipart
                    Multipart multipart = new MimeMultipart();
                    // Add part one
                    multipart.addBodyPart(messageBodyPart);
                    while (itAttNames.hasNext()) {
                        String oneAttachmentName = (String)itAttNames.next();
                        // Create another body part
                        messageBodyPart = new MimeBodyPart();
                        // Set the data handler to the attachment
                        messageBodyPart.setDataHandler(new DataHandler((DataSource)attachments.get(oneAttachmentName)));
                        // Set the filename
                        messageBodyPart.setFileName(oneAttachmentName);
                        // Set Disposition
                        messageBodyPart.setDisposition(Part.ATTACHMENT);
                        // Add part to multipart
                        multipart.addBodyPart(messageBodyPart);
                    }
                    // Put parts in message
                    mimeMessage.setContent(multipart);
                 }
            }
        }
        catch (MessagingException e) {
            throw new javax.mail.MessagingException(e.getMessage(), e);
        }
        catch (TransformerException e) {
            throw new javax.mail.MessagingException(e.getMessage(), e);
        }
    }