Skip to Content

sending mail with images via xsjs

May 04, 2017 at 10:34 AM


avatar image


I am using SMTP relay server to send mails via XSJS project. I am able to send mails with HTML content and normal text type. However now i also want to embed images in the mails. I am using typical HTML format <img width=500 height=100 src=cid:Capture>", however it is not working.When the mail is received there is error shown in space of image : The linked image cannot be displayed.

Any pointers on how to send mails using xsjs using HTML content with embedded images.

10 |10000 characters needed characters left characters exceeded

Please can you share how to create the parts of the mail (the image file part and the html part).



Below is the HTML content of the mail with the image embedded :

var mailContent = "<html><body><img width=500 height=100 src=cid:Capture.PNG><p>Test email,</p> </body></html>";<I have placed the image with name Capture.png in the XS project folder directly>

Below is the code for sending the mail :

// Create an attachment $.net.Mail.Part from JSObject.

var Attachment = new $.net.Mail.Part({ type: $.net.Mail.Part.TYPE_ATTACHMENT,

data: <Here i have converted the image into binary format which is long queues of 0s and 1s and passing as string>

contentType: "image/png",

fileName: "Capture.PNG",

fileNameEncoding: "UTF-8" });

var HTMLContent= new $.net.Mail.Part();

HTMLContent.type = $.net.Mail.Part.TYPE_TEXT;

HTMLContent.text = bodyContent;

HTMLContent.contentType = "text/html";

HTMLContent.encoding = "UTF-8";

var mail = new $.net.Mail({ sender: {address: ""}, to: [{ address: "}], subject: mainContent, subjectEncoding: "UTF-8" });,HTMLContent);



* Please Login or Register to Answer, Follow or Comment.

1 Answer

Florian Pfeffer
May 05, 2017 at 09:59 AM

Instead of an attachment you should create an mail part of type $.net.Mail.Part.TYPE_INLINE in which you can define the Content ID you refer in the HTML part.

You can check the example in the documentation:$.net.Mail.Part.html

Please enclose the value of the image src attribute also in double quotes to avoid any issues (e.g. src="cid:CAPUTURE"). Of course you need to adjust the value to contain the defined content id in the inline part.


Show 10 Share
10 |10000 characters needed characters left characters exceeded

Thanks . i will follow the same. But what should i pass in the data1 value for the property "data" :

var firstPart =new $.net.Mail.Part();

firstPart.type = $.net.Mail.Part.TYPE_INLINE; = data1;// data1 contains the binary data of the image firstPart.contentType ="image/jpg"; firstPart.contentId ="IMAGE1_ID"; firstPart.fileName ="fileName1.jpg"; firstPart.fileNameEncoding ="UTF-8";




The "data" property of the inline part has to contain the image data in binary format (the same as you do it already for your current attachment part).


The value that i am passing in property data is like = "1000000000000011111111111111111111111111111111111111111111111111111111111111111111111111111111111111"+ "1000000000000011111111111111111111111111111111111111111111111111111111111111111111111111111111111111"+ "1000010010000011111111111111111111111111111111111111111111111111111111111111111111111111111111111111"+ "1000011111000011111111111111111111111111111111111111111111111111111111111111111111111111111111111111"+ "1000000111000011111111111111111111111111111111111111111111111111111111111111111111111111111111111111"+ "1000010111000011111111111111111111111111111111111111111111111111111111111111111111111111111111111111"+ "1000110111000011111111111111111111111111111111010111110111010100001010010111111111111111111111111111"+ "1000111111000011111111111111111111111111111111101101001111010001101001011111111111111111111111111111"+ "1001111111000011111111111111111111111111111111111111111111111111111111111111111111111111111111111111"+ "1111111111000011111111111111111111111111111111111111111111111111111111111111111111111111111111111111"+ "1111111111110011111111111111111111111111111111111111111111111111111111111111111111111111111111111111"+ "1111111111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111"+ "1111111111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111"+ "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111";

I have got this binary data of the image from a 3rd party tool to convert image to binary format. Is this fine or should i be doing it another way.Thanks in advance.


Does not really look like a binary for an image.

Because you did not share anything about where your image data comes from, what initial format it has and how you convert it, no further advice can be given at the moment.


the image i want to send as mail hear is company logo and i have the compage logo image saved in png format on local file system. and i have pasted this image in the xs project created. Can you please advice.

Later i used this site to conver the local image into binary format ? is this way correct. i couldnt find any documentation on how to get binary data of the image. Any advice is appreciated.


How do you read the image from the repo. I would assume that you get it already in the right format if you would use e.g. the file rest api or the objectslib xsjslib implementation (e.g. method getTarget of "sap.hana.xs.dt.base.server.objectsLib.xsjslib").


Hi Florian,

Yes i am using "sap.hana.xs.dt.base.server", "utilsLib") library to fetch the image using below code :

var workspace = "_SYS_BIC\.\...\app\gsap\ZPACKAGE\Namrata\Mailfunctionality\";

//var activationMode = 0;

; var version = -1;

var active = null;

var conn = $.db.getConnection(8);

var session = utilsLib.getInactiveSession(conn,workspace);

//var zip = new $.util.Zip();

var objects = XSDS.$defineEntity("Packages", '"_SYS_REPO"."ACTIVE_OBJECT"');

var jsonOut = {};

jsonOut.Objects = objects.$query().$project({OBJECT_NAME : true}) .$where(objects.PACKAGE_ID.$like(app.gsap.ZPACKAGE.Namrata.Mailfunctionality') .$and(objects.OBJECT_NAME.$like('%'))).$order({by : objects.OBJECT_NAME}).$distinct().$execute();

var object = jsonOut.Objects[0].OBJECT_NAME;

var path = "app/gsap/ZPACKAGE/Namrata/Mailfunctionality" + object + ".jpg";

if (objectsLib.getTarget(path, version, session, active)) { return objectsLib.getTarget(path, version, session, active).bdata; }

However i am getting error here at utilsLib.getInactiveSession(conn,workspace); as it says workspace column name is incorrect. I am suspecting it is due to the workspace variable i have passed. Do you know the correct format in which workspace should be passed.




Have to tried it?with an empty workspace.


Yes i have tried with empty workspace it gives below error :

InternalError: dberror(Connection.prepareStatement): 260 - invalid column name: UNDEFINED: line 1 col 220 (at pos 219)


Resolved !!! Passed HANA server address in the workspace and it worked.