2
votes

I'm using Apache Camel 2.22.0 and have a simple route that is sending an email (with a few property references):

public class EmailFailureRoute extends RouteBuilder {

@Override
public void configure() {
    from("seda:mail")
        .setHeader("To", simple("{{mail.failure.to}}"))
        .setHeader("From", simple("{{mail.failure.from}}"))
        .setHeader("Subject", constant("TEST!"))
        .to("velocity://templates/failure-mail.vm")
        .to("{{mail.smtpServer}}");
  }

}

What I expect to get is a normal email with the text from the Velocity template in the body of the message. What I actually get is an email with the text from the Velocity template attached to the email. It looks like this in MS Outlook:

Camel sends mail message body as attachment?

Why the attachment? How do I get the mail component to insert the result of the Velocity template directly into the email message body?

UPDATE:

By turning on the 'debugMode' flag on the mail component, I was able to see the content of the actual email as it's being sent to the SMTP server. It appears that my problem is that the Content-Type is 'application/json'! So now it makes sense that the body is attached but I have no idea why the Content-Type is set this way. Even setting the query parameter 'contentType=text/plain' on the mail end point has no effect on the final Content-Type of the email message.

2
Is your mail plain text? Or is it html? Outlook in particular can be stubborn about content. Could you send the mail to a Gmail account and check. I had to jump through hoops to get Outlook displaying images correctly via Apache camel mail component. MS has it own way of handling mail in Outlook.Namphibian
The mail component uses MIME type text/plain by default, although I've tried text/plain and text/html with the same result. I also tried sending the email to a GMail account and the email still had an attachment instead of text in the body.Nick A. Watts

2 Answers

4
votes

While I still don't really understand what's happening here, I do have a workable solution. The only way I could get the content type of the email message to change was to set the 'Content-Type' header on the Camel message before routing to the mail end point:

.setHeader("Content-Type", constant("text/plain"))

I couldn't even get the content type to change by using the 'contentType' query parameter on the mail component.

0
votes

how are you recently and gone through something similar and solved it in the following way I hope it will be helpful

@Handler
public void attachmentValidate(@ExchangeProperty("MAIL_ATTACHMENTS") List<Attachment> attachments,
        Exchange exchange) throws Exception {
    Message in = exchange.getIn();
    if (attachments != null) {
        for (Attachment attachment : attachments) {
            FileNameMap fileNameMap = URLConnection.getFileNameMap();
            String mimeType = fileNameMap.getContentTypeFor(attachment.getName()
                    .substring(attachment.getName().indexOf('.'), attachment.getName().length()));
            if (StringUtils.isEmpty(mimeType)) {
                mimeType = "application/octet-stream";
            }
            byte[] decoded = Base64.getDecoder().decode(attachment.getValue());
            in.addAttachment(attachment.getName(), new DataHandler(new ByteArrayDataSource(decoded, mimeType)));
        }
    }
    exchange.setProperty("MAIL_ATTACHMENTS", attachments);
}