cancel
Showing results for 
Search instead for 
Did you mean: 

Sending an email from UDF and including an attachment

Former Member
0 Kudos

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());

outputAttachments.setAttachment(attachments);

Then I create the body of the mail:

String mailxml = "<ns:Mail xmlns:ns=\"http://sap.com/xi/XI/Mail/30\">" +

    "<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 = accessor.call(payload);


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?


Thanks,

Martin

Accepted Solutions (0)

Answers (5)

Answers (5)

Former Member
0 Kudos

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.

Regards,

Srikanth Mavuri.

Former Member
0 Kudos

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..

Regards,

Srikanth Mavuri.

Former Member
0 Kudos

I could also try that.

Thanks Srikanth

former_member183816
Active Participant
0 Kudos

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_Type>text/plain</Content_Type>"+CRLF+

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

former_member183816
Active Participant
0 Kudos

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=\"http://sap.com/xi/XI/Mail/30\">"+CRLF+

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

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

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

                "<Reply_To />"+CRLF+

                  "<Content_Type>text/xml</Content_Type>"+CRLF+

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

                       "<Content>"+errorMsg+"</Content>"+CRLF+

                            "</ns:Mail>";                 

This will create your attachment in your email.

Former Member
0 Kudos

Hi Ambuj,

Thanks for your reply.

Your code does indeed create an attachment, but this way the body of the email is empty again. All content is placed in the attachment.

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.

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

Kind regards

former_member183816
Active Participant
0 Kudos
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.

iaki_vila
Active Contributor
0 Kudos

Hi Martin,

I haven't tried it, but you can try with PayloadSwapBean bean. Check the page 5 here http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/6d967fbc-0a01-0010-4fb4-91c6d38c5....

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.

Regards.

Former Member
0 Kudos

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

iaki_vila
Active Contributor
0 Kudos

Hi Martin,

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

Regards.

Former Member
0 Kudos

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