cancel
Showing results for 
Search instead for 
Did you mean: 

how to read email attachment encoding

Former Member
0 Kudos

Hi there,

I have a requirement in which I need to read the email attachment which I am able to but inside the attachment I have couple of German characters which is not displaying correctly because of encoding.

I am using UDF to read the email Content and when I check the ABAP stack it is showing

MailMessage (text/xml;charset = utf-8)

MailAttachment-1 (application/octet-stream;name="abcdedg"

XML file which is coming as an attachment contains this

<STRAS>Eisenhüttenstr. 99</STRAS>

But after when the file comes to PI it will become

<STRAS>Eisenhüttenstr. 99</STRAS>

I have tried all the Content Encoding in Sender Mail adapter but nothing works.

Can anybody please tell me how to solve this problem.

Thanks,

Iqbal

Accepted Solutions (0)

Answers (1)

Answers (1)

former_member184681
Active Contributor
0 Kudos

Hi Iqbal,

The Attachment Java class that you are most probably using in the UDF to read the attachment content contains a method getContentType() that will help you determine the charset used. You could also read the charset from the attachment payload, as it should be starting with:

<?xml version="1.0" encoding="SOME_ENCODING"/?>

Then you can generate the UDF output with:

String output = new String(attachment.getBytes("ISO-8859-1"), "UTF-8");

return output;

Where attachment is some string variable containing the attachment payload.

Regards,

Greg

Former Member
0 Kudos

Hi Gzegorz,

Here is what I am doing to read the attachement

GlobalContainer globalContainer = container.getGlobalContainer();

InputAttachments inputAttachments = globalContainer.getInputAttachments();

OutputAttachments outputAttachments = globalContainer.getOutputAttachments();

Attachment attachments =inputAttachments.getAttachment(attachmentID);

Attachment attach = outputAttachments.create(attachmentID, "application/xml;charset=utf-8", attachments.getContent());

attachmentContent = new String(attach.getContent());

result.addValue(attachmentContent);

Can you please suggest what changes I  do in this code to change the email attachment encoding from MailAttachment-1 (application/octet-stream;name="abcdedg" to MailAttachment-1 (application/xml;name=abcbdef"

Thanks

former_member184681
Active Contributor
0 Kudos

Try the following:

GlobalContainer globalContainer = container.getGlobalContainer();

InputAttachments inputAttachments = globalContainer.getInputAttachments();

OutputAttachments outputAttachments = globalContainer.getOutputAttachments();

Attachment attachments =inputAttachments.getAttachment(attachmentID);

String attachmentContent = new String(attach.getContent());

String converted = new String(attachmentContent.getBytes("ISO-8859-1"), "UTF-8");

Attachment attach = outputAttachments.create(attachmentID, "application/xml;charset=utf-8", converted);

result.addValue(attachmentContent);

Regards,

Greg

Former Member
0 Kudos

Hi Greg,

Thank you so much for your help.

I am still getting an error

Can you please check the code.

former_member184681
Active Contributor
0 Kudos

Ok, here is a little correction:

GlobalContainer globalContainer = container.getGlobalContainer();

InputAttachments inputAttachments = globalContainer.getInputAttachments();

OutputAttachments outputAttachments = globalContainer.getOutputAttachments();

Attachment attachments =inputAttachments.getAttachment(attachmentID);

String attachmentContent = new String(attach.getContent());

Attachment attach = outputAttachments.create(attachmentID, "application/xml;charset=utf-8", attachmentContent.getBytes("UTF-8"));

result.addValue(attachmentContent);

Regards,

Greg

Former Member
0 Kudos

Hi Greg,

Still issues

I don't think so getBytes Method in avaliable in the InputAttachment

Here are the Methods

Method Summary
booleanequals(Object object)
          Compare attachments.
StringgetBase64EncodedContent()
          Return the actual content of the attachment as Base64-encoded String.
byte[]getContent()
          Return the actual content of the attachment as byte array.
StringgetContentId()
          Get the identifier of the attachment.
StringgetContentType()
          Get the type of the attachment.
inthashCode()
          Uses the content IDs hashCode.
StringtoString()
          

Now I am getting this error

Can you please suggest what else can be done to solve this problem.

I really appreciate all your help.

Message was edited by: Muhammad iqbal

former_member184681
Active Contributor
0 Kudos

Dear Iqbal,

Check that attachmentContent is of type String and String has the getBytes method. Still, there is one more issue with the code: I've forgotten that catching exceptions is mandatory in Java (got used to ABAP so much ). So simply replace:

Attachment attach = outputAttachments.create(attachmentID, "application/xml;charset=utf-8", attachmentContent.getBytes("UTF-8"));

with:

try {

Attachment attach = outputAttachments.create(attachmentID, "application/xml;charset=utf-8", attachmentContent.getBytes("UTF-8"));

} catch (UnsupportedEncodingException e) { }

You can also add some exception handling after the catch block if required.

Regards,

Greg

Former Member
0 Kudos

Hi Greg,

Once again thanks for your help.

At last Code is working fine but I am still facing the same issue

Final Code

Attachment attachments =inputAttachments.getAttachment(attachmentID);

String attachmentContent = new String(attachments.getContent());

try {

Attachment attach = outputAttachments.create(attachmentID, "application/xml;charset=utf-8", attachmentContent.getBytes("UTF-8"));

} catch (UnsupportedEncodingException e) { }

result.addValue(attachmentContent);

ABAP Stack It is still showing the MailAttachment as

MailMessage (text/xml;charset = utf-8)

MailAttachment-1 (application/octet-stream;name="abcdedg"

Don't you think we need to display attach in the result.addValue because it has the correct encoding

Any other suggestion?

baskar_gopalakrishnan2
Active Contributor
0 Kudos

We hit the same problem yesterday (after code compiling successfully ) with your other thread of the same problem. Is that so? Is it feasible to write the content in a file and open the content manually? Since you write the attachment content as application/xml?  Just a thought.

Former Member
0 Kudos

Hi Baskar,

Yes it's the same problem.

I open the file in the notepad++ editor and check the encoding and it is showing ANSI and when I change this to UTF-8 then that character will appear in it's correct form.

My Scenario is Email Attachment ---> PI ----> iDoc

Please suggest if anything else can be done to resolve this problem either JAVA Mapping or XSLT Mapping.

former_member194677
Participant
0 Kudos

Hi Iqbal,

is it resolved?

is your scenario working?