cancel
Showing results for 
Search instead for 
Did you mean: 

Attach to Mail using FileUpload UI in Web dynpro Java

Former Member
0 Kudos

Hi,

I need to send a file from my system as attachment with a mail in a web dynpro java application.

I am trying to implement it by uploading the file to server using FileUpload UI and then sending the file as attachment

using java mail api which is not working at the moment.

Can anyone please suggest possible solution for this. Is there anyother approach to implement this?

-- Arnab

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

try this

Message msg = new MimeMessage(mailsession);

BodyPart messageBodyPart = new MimeBodyPart();

messageBodyPart.setText(Attachment);

Multipart multipart = new MimeMultipart();

multipart.addBodyPart(messageBodyPart);

messageBodyPart = new MimeBodyPart();

DataSource source = new FileDataSource(Attachment);

messageBodyPart.setDataHandler(new DataHandler(source));

messageBodyPart.setFileName(Attachment);

multipart.addBodyPart(messageBodyPart);

msg.setContent(multipart);

msg.setSubject(Subject);

msg.setText(Message);

Transport.send(msg);

Answers (1)

Answers (1)

junwu
Active Contributor
0 Kudos

what's the error?

Former Member
0 Kudos

I am using the following code to upload the file. Its not showing any error only the mail is sent without the attachment.

IPrivateFormView.IFileUploadElement element = wdContext.currentFileUploadElement();

InputStream text = null;

int temp=0;

try{

File file = new File(element.getFilename().getResourceName());

FileOutputStream out = new FileOutputStream(file);

if(element.getFilename()!= null){

text = element.getFilename().read(false);

while((temp = text.read()) != -1){

out.write(temp);

}

}

out.flush();

out.close();

String filePath = file.getAbsolutePath();

wdContext.currentContextElement().setAttachFileExtension(filePath);

msg.reportSuccess("File:" + filePath);

}catch(Exception e){

}

And the following code to attach the file to mail

MimeBodyPart bodyPart = null;

Multipart mp = new MimeMultipart();

try {

bodyPart = new MimeBodyPart();

bodyPart.setContent("Attached Mail","text/plain");

mp.addBodyPart(bodyPart);

//FileDataSource fds = new FileDataSource("http://dcwwdvsaprt02:50000/irj/go/km/docs");

MimeBodyPart attachmentBodyPart = new MimeBodyPart();

//attachmentBodyPart.setDataHandler(new DataHandler(fds));

URL URLattachedFileName = new URL(wdContext.currentContextElement().getAttachFileExtension());

attachmentBodyPart.setDataHandler(new DataHandler(URLattachedFileName));

attachmentBodyPart.setFileName("Attachment");

mp.addBodyPart(attachmentBodyPart);

} catch (Exception e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}