cancel
Showing results for 
Search instead for 
Did you mean: 

How to attach pdf and send as email?

Former Member
0 Kudos

Hi all,

I have a requirement, i need to create a upload for the user to upload the pdf from his system and after uploading, the user can click on the send email button to send the uploaded pdf document to another person. How do i achieve that? Is there any source code or sample to follow?

Regards,

Rayden

Accepted Solutions (0)

Answers (1)

Answers (1)

sid_sunny
Contributor
0 Kudos

Hi Rayden,

You can use the java mail api for this.

For reference you can use these two PDF's

[1. Sending Mail in Web Dynpro|https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/2678e05a-0701-0010-fa86-ddfbec5a2b94]

[2. Sending Attachement in web dynpro|https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/cacb9a7d-0c01-0010-1281-be4962c9ab3e]

Regards

Sid

Edited by: siddharth chauhan on Jan 14, 2008 10:49 AM

Former Member
0 Kudos

Hi siddharth chauhan,

The tutorial given is only allow to upload only a standard pdf with template.

String filename =

"temp
webdynpro
web
local
TutWD_EmailInteractiveForm_Init
Components
com.sap.tut.wd.emailinteractiveform.EmailInteractiveFormComp
TravelRequest.pdf";

My application must be dynamic in a way that any pdf file uploaded, the pdf file will be attached as an attachment and send as an email. The path must be dynamic. Is that possible to achieve in java web dynpro?

Regards,

Rayden

Edited by: Rayden on Jan 14, 2008 4:11 PM

sid_sunny
Contributor
0 Kudos

Hi Rayden,

Use the code below:

DataSource source = wdContext.currentContextElement().getResource();

messageBodyPart.setDataHandler(new DataHandler(source));

messageBodyPart.setFileName((resource.getResourceName());

messageBodyPart.setHeader("Content-Type","application/"+resource.getResourceType().getFileExtension());

multipart.addBodyPart(messageBodyPart);

message.setContent(multipart);

Transport.send(message);

Resource is the VA binded to the RESOURCE property of the upload UI element.

Regards

Sid

Former Member
0 Kudos

Hi siddharth chauhan ,

I try your code.

DataSource source = wdContext.currentContextElement().getFileResource();

The above code giving an error:

Type mismatch: Cannot convert from IWDResource to DataSource.

So I modify the code to FileDataSource as shown:

File newFile = new File(wdContext.currentContextElement().getFileResource().toString());

boolean isFile = newFile.isFile();

boolean isdir = newFile.isDirectory();

boolean isAbs= newFile.isAbsolute();

FileDataSource source = new FileDataSource(newFile.getAbsoluteFile());

mbp1.setDataHandler(new DataHandler(source));

mbp1.setFileName(res.getResourceName());

mbp1.setHeader("Content-Type","application/" + res.getResourceType().getFileExtension());

mp.addBodyPart(mbp1);

msg.setContent(mp);

Transport.send(msg);

I do debugging the following result :

isFile = false

isdir = false

isAbs= false

Sending failed; nested exception is: javax.mail.MessagingException: IOException while sending message; nested exception is: java.io.FileNotFoundException: ..\..\local\UploadEmailPjt\UploadEMail\~wd_key123_1200360661453\1.pdf?sap-wd-download=1&sap-wd-cltwndid=82bfc481c30911dcc4b00015605b280a&sap-wd-appwndid=82bfc482c30911dc8bff0015605b280a&sap-wd-norefresh=X (The system cannot find the path specified)

-


I try to modify the code to URLDataSource as shown:

URL newurl = new URL(wdContext.currentContextElement().getFileResource().toString());

String pro = newurl.getProtocol();

String newfilez = newurl.getFile();

String urlpath = newurl.getPath();

URLDataSource urlsrc = new URLDataSource(newurl);

mbp1.setDataHandler(new DataHandler(urlsrc));

mbp1.setFileName(res.getResourceName());

mbp1.setHeader("Content-Type","application/" + res.getResourceType().getFileExtension());

mp.addBodyPart(mbp1);

msg.setContent(mp);

Transport.send(msg);

I encounter the following error at URL newurl = new URL(wdContext.currentContextElement().getFileResource().toString());

no protocol: ../../local/UploadEmailPjt/UploadEMail/~wd_key121_1200360322468/1.pdf?sap-wd-download=1&sap-wd-cltwndid=b9f4b8d3c30811dcc3870015605b280a&sap-wd-appwndid=b9f4b8d4c30811dcca950015605b280a&sap-wd-norefresh=X

The path

../../local/UploadEmailPjt/UploadEMail/~wd_key121_1200360322468/1.pdf?sap-wd-download=1&sap-wd-cltwndid=b9f4b8d3c30811dcc3870015605b280a&sap-wd-appwndid=b9f4b8d4c30811dcca950015605b280a&sap-wd-norefresh=X

is generated by the web dynpro, when i upload the file. When i create a link to download the file using the path, it work fine. But when i try to use the link to attach the file in the email as an pdf, it cant. Why is that so? Any idea how to attach the pdf?

Regards,

Rayden

sid_sunny
Contributor
0 Kudos

Hi Rayden,

I got a workaround for this data conversion, what I am doing is I am writting the uploaded file on a secified location and then reading it from there for attaching and then I was able to send the attachment.

Just see if this workaround can be applicable in your case.

Regards

Sid