on 10-19-2010 4:07 PM
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
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);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
what's the error?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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();
}
User | Count |
---|---|
84 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.