7 Replies Latest reply on Jun 4, 2009 6:27 AM by jamie3_james.archibald

    XML Email attachments

    jamie3_james.archibald

      I am trying to add an attachment to a message and send it to the smtp endpoint:

       

       

           public void process(Exchange exchange) throws Exception {

                String body = (String) exchange.getIn().getBody();

                 

                Source src = new javax.xml.transform.stream.StreamSource(new StringReader(body));

                 

                exchange.getIn().addAttachment("file.xml", new DataHandler(src, "text/xml"));

           }

       

       

      The body is as follows:

       

       

       

      I am checking the place where I sent the message and I have confirmed that I am getting the email, however no attachment is present.

        • 1. Re: XML Email attachments
          davsclaus

          Hi

           

          What does your route look like? Do you add the attachment just before sending to the mail endpoint?

           

          And what version of Camel are you using? We have improved it in the recent releases.

           

          And you can enable DEBUG logging to check what is going on.

           

          And beware of the mail provider jar. Do you use sun mail jar or geronimo. The geronimo one is a bit buggy.

           

          And you could also try creating a unit test with the attachment to see if that works. See some of the unit tests in the camel-mail source code.

           

          And I assume you have looked at the Camel mail wiki page.

          • 2. Re: XML Email attachments
            jamie3_james.archibald

            Hi

             

            What does your route look like? Do you add the attachment just before sending > to the mail endpoint?

             

            My route is quite large but here it is broken into its smallest form.

             

            from("activemq:topic:SomeTopic").

            process(new EmailProcessor()) // this is the guy that adds the attachment and email headers

            .to("smtp://mymailserver:25?password=password&username=username&dummyTrustManager=true")

             

            And what version of Camel are you using? We have improved it in the recent releases.

             

            1.6.1

             

            And you can enable DEBUG logging to check what is going on.

             

            Ok will try that

             

            And beware of the mail provider jar. Do you use sun mail jar or geronimo. The geronimo one is a bit buggy.

             

            some jars in my classpath are:

             

            geronimo-servlet_2.5_spec-1.2.jar

            geronimo-j2ee-management_1.0_spec-1.0.jar

            javamail-1.4.2.jar

             

             

            And you could also try creating a unit test with the attachment to see if that works. See some of the unit tests in the camel-mail source code.

             

            My unit test sends a message to the jms topic and then this is received via my route, and it that downloads the incoming email and no attachment is found. ill take a look in the camle code as well.

             

            And I assume you have looked at the Camel mail wiki page.

             

            Yep but it only showed how to email from a File data source.

            • 3. Re: XML Email attachments
              davsclaus

              Hi

               

              Could you temporary try the file datasource as attachment. To see if that works.

              Most of the unit tests is based on attaching real files.

               

              So there could be something when not using real files as attachment. So if you could try if that makes a different on your side.

               

              You can also look at the mail mock we use in camel-mail for unit test to see if you can write a little unit test that does as the camel unit tests and see if you can get it to work with attachment.

              • 4. Re: XML Email attachments
                jamie3_james.archibald

                I found the problem!

                 

                Remember how I said the route was a bit more complicated?

                 

                 

                from("activemq:topic:SomeTopic").

                process(new EmailProcessor()) // this is the guy that adds the attachment and email headers

                .to("velocity:some/path/emailTemplate.txt").

                .to("smtp://mymailserver:25?password=password&username=username&dummyTrustManager=true")

                 

                Well after experimenting I removed the velocity URI and the attachment came through. Using the FileDataSource of course.

                 

                So the velocity endpoint appears to be killing the attachments.

                • 5. Re: XML Email attachments
                  davsclaus

                  Hi

                   

                  Yeah attachments is not a first class citizen in all Camel components. In fact it only used by the mail component, hence its from the Java Activation API that is really only used by the mail API anyway.

                   

                  So some components do not by nature support attachments. The rule of thumb is to add the attachments just before sending to the mail endpoint.

                   

                  For me personally I would not have added the attachment API in Camel. It should just have a been a special API for the mail component. Then the attachments could just be a regular Camel header.

                   

                  setHeader(Exchange.ATTACHMENT, myAttachment);

                  setHeader(Exchange.ATTACHMENT_NAME, "hello.txt");

                   

                  I will add a note on the Camel mail wiki page about your problem and the solution.

                   

                  And there are a very few 3rd party frameworks that support the Attachments API anyway. Only seems to be mail based.

                   

                  Well that was just a side note.

                  • 6. Re: XML Email attachments
                    njiang

                    Hi Claus

                     

                    Because we can send a bunch of attachment file through one mail, I think the new Attachment API need to support multi attachment files, such as set the header value with a list of attchment files.

                     

                    Just my 2 cents,

                     

                    Willem

                    • 7. Re: XML Email attachments
                      jamie3_james.archibald

                      Sounds good.

                       

                      I think it is fair to keep it the way it is an simply ensure that the documentation is updated.

                       

                      Works great btw.

                       

                      thanks.