Skip to Content
author's profile photo
Former Member

Sending an email from UDF and including an attachment

Hello all,

I am trying to send an email from a UDF and want to include the original message as an attachment to that mail.

Basically the body of the mail tells me there was an error and the attachment is the response received from the business system.

For this I have written/copied the following code:

First I create the attachment based on the input field errorMsg (which is the header segment "returned as xml" and thus the whole response message)

GlobalContainer globalContainer = container.getGlobalContainer();

OutputAttachments outputAttachments = globalContainer.getOutputAttachments();

Attachment attachments = outputAttachments.create("ByDesign_Response.xml", "text/xml", errorMsg.getBytes());


Then I create the body of the mail:

String mailxml = "<ns:Mail xmlns:ns=\"\">" +

"<Subject>Error</Subject>" +

"<From> etc ...

And then I call the channel and send out the payload through that channel

Channel channel = LookupService.getChannel(component, comChannel);

SystemAccessor accessor = LookupService.getSystemAccessor(channel);

InputStream inputStream = new ByteArrayInputStream(mailxml.getBytes());

XmlPayload payload = LookupService.getXmlPayload(inputStream);

Payload mailOutPayload = null;

mailOutPayload =;

In the receiver channel I set "Use Mail Package" in the mail attributes, otherwise the body of the mail will be sent out as a an attachment.

The mail with the correct text is sent out but the attachment is not sent, although I see it has been created in the XI Monitor:

Is it possible to send the attachment this way?



attachment.png (4.7 kB)
Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

5 Answers

  • Jun 03, 2015 at 03:25 PM

    Hi Martin,

    Have you set the Keep Attachments indicator in the Receiver Mail adapter?


    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hi Iñaki,

      Thanks for your answer. I have tried that indeed, but then the body text goes into an attachment and no body is shown in the mail itself anymore.

      Kind regards

  • Jun 03, 2015 at 03:51 PM

    Hi Martin,

    I haven't tried it, but you can try with PayloadSwapBean bean. Check the page 5 here….

    I know this is not your case because in that example move the payload to the attachment, but may be PI thinks the second message is not attachment and you can do a little trick.


    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hi Iñaki,

      I was thinking the same during the development and have tried it, but somehow the payload swap bean does not see the attachment.

      I get the feeling that because I call the channel during the mapping that not all things are available until after the message mapping is completed, since I have also tried to set the filename and directory dynamically (in an attempt to write the files using a file communication channel) and that did not work either.

      Kind regards

  • Jun 03, 2015 at 04:11 PM

    No need to create attachment like that, just try this,

    String CRLF="\r\n";

    String errorMsg="Error occured due to this reason";

    // forming Email Package

    String emailPackage=

    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+CRLF+

    "<ns:Mail xmlns:ns=\"\">"+CRLF+

    "<Subject>Delivery notes</Subject>"+CRLF+

    "<From>certificates@ .com</From>"+CRLF+

    "<To>ambuj.mishra@ .com;someoneelse@ .com</To>"+CRLF+

    "<Reply_To />"+CRLF+


    "<Content_Disposition>attachment; filename="+"\""+"ByDesign_Response.xml"+"\""+"</Content_Disposition>"+CRLF+



    This will create your attachment in your email.

    Add comment
    10|10000 characters needed characters exceeded

    • I have tried to create emailPackage string with boundaries so that I can use one part as the body and one part as the attachment, but so far I could not get that to work either.

      Recently I had requirment like you and i tried this boundry thing many time but it didn't work for me also.

      I finally resolved this issue by putting something in <Content> and <Content_Type> node of email package.

      Apparently my email server was not triggering emails, when these 2 tags were empty.

      I had 2 attachment in email, so I attached one using outputAttachments.setAttachment(attachments);

      and another attachment i had provided in content like this, so that email gets triggered.

      "<Content_Type>text/xml</Content_Type>"+CRLF+ "<Content_Disposition>attachment; filename="+"\""+"attachmentName.txt"+"\""+"</Content_Disposition>"+CRLF+

      "<Content>"+"my 2nd attachment content"+"</Content>"+CRLF+

      So I would suggest you to create attachment as you are creating now in your code and put your mail body part in content.

      Another thing is that the name of the attachment is always ATT0001.xml in stead of the name specified in the content-disposition

      May be you are overwirting or hardcoding it somewhere,check again.Attachment name would be as present in disposition tag.

  • author's profile photo
    Former Member
    Jun 04, 2015 at 06:34 AM

    Hi Martin,

    Have you tried above requirement by using Java Mapping... By Using Java Mapping we can send mail with attachment ( Payload)....

    I think by Java Mapping we can achieve your requirement..


    Srikanth Mavuri.

    Add comment
    10|10000 characters needed characters exceeded

    • Can you try removing <Content_Disposition> tag while forming email package.

      Use content type and content only. May be it works. And attach the message as you are doing it in your UDF currently.


      "<Content>"+"This content should be present in email body"+"</Content>"+CRLF+

  • author's profile photo
    Former Member
    Jun 10, 2015 at 11:03 AM

    Hi Martin,

    If you want i will share the Java Mapping code that will send the email as a notification with payload as an attachment.


    Srikanth Mavuri.

    Add comment
    10|10000 characters needed characters exceeded