Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

image not displayed in outlook email when triggered via ABAP code

gopalkrishna_baliga
Participant
0 Kudos

Dear Experts,

I have a requirement where I have to generate email for plant maintenance notification, In the email body (content) i need to display an image(jpeg) .

I have written the code as specified in the blog

https://blogs.sap.com/2013/06/11/image-in-mail-body-in-abap/

Email is getting triggered however image is not displayed.

I have debugged my code and there is no issue. even the HTML source code from outlook email is also same as coded.

What else could be wrong? Please help as this is urgent.

See the attached image for more details.

My code snippet is as below

"Get image from mime repository in form of xstring

lr_mime_rep = cl_mime_repository_api=>if_mr_api~get_api( ).

CALL METHOD lr_mime_rep->get

EXPORTING

i_url = '/SAP/PUBLIC/PM/notify.jpg'

IMPORTING

e_is_folder = lv_folder

e_content = lv_content

e_loio = lv_loio

EXCEPTIONS

parameter_missing = 1

error_occured = 2

not_found = 3

permission_failure = 4

others = 5

.

IF sy-subrc <> 0.

* Implement suitable error handling here

ENDIF.

"Convert the image from xstring to table form

call method cl_bcs_convert=>xstring_to_solix

exporting

iv_xstring = lv_content

receiving

et_solix = i_solix.

lv_len = xstrlen( lv_content ).

"Attach Image in xstring

lv_filename = 'PM/notify.jpg'.

lv_content_id = 'PM/notify.jpg'.

CREATE OBJECT lo_mime_helper.

CALL METHOD lo_mime_helper->add_binary_part

EXPORTING

content = i_solix "Xstring in table form

filename = lv_filename "file name to be given to image

extension = 'JPG' "type of file

description = 'notify' "description

content_type = 'image/jpg' "content type / Mime type. If mime type not present in system

length = lv_len "length of image

content_id = lv_content_id. "content id would be used in html part

* Prepare email body

CONCATENATE '<html>'

'<body>'

INTO lv_line1.

CONCATENATE '<p>'

'<img src="cid:PM/notify.jpg" width="668" height="191" />'

------ Skipping some lines of code

CALL METHOD cl_document_bcs=>create_document

EXPORTING

i_text = lt_mail_body

i_subject = lv_subject

i_type = 'HTM'

i_language = sy-langu

RECEIVING

result = lr_document.

Thanks & Regards

Gopal

1 ACCEPTED SOLUTION

raymond_giuseppi
Active Contributor

Check the html source for the <img src=cid:filename.jpg> tag, look at the attachment(s) of the mail, is the picture attached, does it use the same filename, are you able to download the attached image and display it?

12 REPLIES 12

mmcisme1
Active Contributor
0 Kudos

It would help if you display the entire message. It would also help if you shared your actual code. Otherwise, we can't help.

Michelle

raymond_giuseppi
Active Contributor

Check the html source for the <img src=cid:filename.jpg> tag, look at the attachment(s) of the mail, is the picture attached, does it use the same filename, are you able to download the attached image and display it?

0 Kudos

Hi Raymond

I have checked the HTML source and it is fine. image is not coming as attachment. I cannot download the image as well. i have updated my text above with more details like code snippets. See above.

Thanks

Gopal

0 Kudos

So correct the code that send the mail to attach the image file.

Hint: Look for the add_binary_part method in original blog.

0 Kudos

In your first comment you asked me to check if there is any attachment in the email.

And my answer is "No".

I am also not expecting any attachment.

I want image in email body. The code I have written is correct but still image is not displayed in the email output.

Hope you are getting my question.

Thanks

Gopal

If your html code contains a cif: tag your mail must carry an attachment, I was expecting one (else why would I ask you to download and display it?) 😉

Hint: Seems you created wrongly the document, use cl_document_bcs=>create_from_multirelated (as in the original blog) and not cl_document_bcs=>create_document which doesn't add the attachment.

0 Kudos

So you are saying that even if I just want the image to be embedded in the email body I have to use cl_document_bcs=>create_from_multirelated?

Anyway I try that too.

0 Kudos

Yes this worked.

Thanks A lot!

Regards

Gopal

pokrakam
Active Contributor

It amazes me when I see code like this and people wonder why it doesn't work

IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

0 Kudos

Dear Sir,

If you have read my question fully then You will find that i have clearly mentioned that I have done the debugging and there is no error. So its not even going to any exception block. To be more clear sy-subrc is always 0.

Moreover please understand that this is not a final code that I can share. Once i get the basic functionality up and working then i will add all the exception/error handling.

Regards

Gopal

pokrakam
Active Contributor

If you had read my response fully, you would have found that it was not proposing that this is the cause of your problem, but an observation on code quality. Code quality from the start is a very important part of solutions with fewer errors. "I will add error handling later" is something many developers say/think/hope, but sadly the majority of such code stays as is.

Sandra_Rossi
Active Contributor
0 Kudos

Please format your code, it's difficult to read it. There's a button "CODE".