cancel
Showing results for 
Search instead for 
Did you mean: 

Send mail with attachment from the uploaded file

Former Member
0 Kudos

hi,

From a form thread i got the following code to send mail with attachment with the file uploaded from the file upload ui element.

public void onActionLoadFile(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )

{

//@@begin onActionLoadFile(ServerEvent)

WDWebResourceType FileType = null;

String FileName = new String();

//get attribute info for context attribute 'FileUpload'

IWDAttributeInfo attributeInfo =

wdContext.getNodeInfo().getAttribute(

IPrivateEmailView.IContextElement.FILE_UPLOAD);

//get modifiable binary type from the attribute info,requires type cast.

IWDModifiableBinaryType binaryType =

(IWDModifiableBinaryType) attributeInfo.getModifiableSimpleType();

IPrivateEmailView.IContextElement element =

wdContext.currentContextElement();

//if a file in the 'FileResource' attribute exists

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

try {

String mimeType = binaryType.getMimeType().toString();

byte[] file = element.getFileUpload();

//get the size of the uploaded file

element.setFileSize(this.getFileSize(file));

//get the extension of the uploaded file

element.setFileExtension(binaryType.getMimeType().getFileExtension());

//NOTE: context attribute 'FileName' must not be set

//because the FileUpload-UI-element property 'fileName'

//is bound to it. Consequently the fileName is automatically

//written to the context after file upload.

//report success message

wdComponentAPI.getMessageManager().reportMessage(

IMessageEmailComp.SF_UPLOAD,

new Object[] { binaryType.getFileName()},

false);

FileType = binaryType.getMimeType();

FileName = binaryType.getFileName();

} catch (Exception e) {

throw new WDRuntimeException(e);

}

}

//if no file in the 'FileResource' attribute exists

else {

//report error message

IWDMessageManager msgMgr = wdComponentAPI.getMessageManager();

msgMgr.reportContextAttributeMessage(

element,

attributeInfo,

IMessageEmailComp.NO_FILE,

new Object[] { "" },

true);

}

//clear the FileResource context value attribute

//element.setFileUpload(null);

String URL;

URL = this.CreateAndGetPathFileUpload(

wdContext.currentContextElement().getFileUpload(),

FileName);

// if (URL.length() == 1){

// //ERRORE

// }

wdContext.currentContextElement().setPATHFileUploaded(URL);

//@@end

}

public boolean send( java.lang.String subj, java.lang.String mess, java.lang.String dest, java.lang.String attach, java.lang.String FileName )

{

//@@begin send()

InitialContext ctx = null;

Address[] address = null;

Message msg = null;

Session sess = null;

MimeBodyPart bodyPart = null;

Multipart mp = null;

// "141.29.193.71" == milvl2ja.icn.siemens.it (SMTP di Siemens)

try {

Properties props = new Properties();

props.put("domain","true");

ctx = new InitialContext(props);

sess = (Session) ctx.lookup("java:comp/env/mail/MailSession");

msg = new MimeMessage(sess);

IWDClientUser utente = WDClientUser.getCurrentUser();

String senderEmail = utente.getSAPUser().getEmail();

InternetAddress addressFrom = new InternetAddress(senderEmail);

msg.setFrom(addressFrom);

String EmailDEST = dest;

InternetAddress addressTo = new InternetAddress(EmailDEST);

msg.setRecipient(Message.RecipientType.TO, addressTo);

msg.setSubject(subj);

// if ((mess != null) && (mess.length()>0)) {

// msg.setContent(mess, "text/plain");

// } else {

// msg.setContent("", "text/plain");

// }

//Gestione ATTACHMENT...

String attachedFileName = new String(wdContext.currentContextElement().getFileName());

boolean hasAttachment = (attachedFileName != null) && (attachedFileName.length() > 0);

boolean isMultiPart = (mess != null) && (mess.length() > 1);

//adding an attachment makes the message multipart

if (isMultiPart || hasAttachment) {

mp = new MimeMultipart();

// add text parts

if (mess != null) {

for (int i = 0; i < mess.length(); i++) {

bodyPart = new MimeBodyPart();

bodyPart.setContent(mess,"text/plain");

mp.addBodyPart(bodyPart);

}

}

//attach the file to the message if needed

if (hasAttachment) { // avoid the case with no text parts

bodyPart = new MimeBodyPart();

bodyPart.setContent("Allegato incluso nel messaggio.","text/plain");

mp.addBodyPart(bodyPart);

// the part with the file

FileDataSource fds = new FileDataSource(attach);

MimeBodyPart attachmentBodyPart = new MimeBodyPart();

attachmentBodyPart.setDataHandler(new DataHandler(fds));

//URL URLattachedFileName = new URL(attach);

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

attachmentBodyPart.setFileName(FileName);

mp.addBodyPart(attachmentBodyPart);

}

msg.setContent(mp);

} else {

if ((mess != null) && (mess.length() > 0)) {

msg.setContent(mess, "text/plain");

} else {

msg.setContent("", "text/plain");

}

}

//fine ATTACHMENT

msg.setSentDate(new GregorianCalendar().getTime());

msg.saveChanges();

address = msg.getAllRecipients();

Transport.send(msg, address);

} catch (Exception e) {

e.printStackTrace();

return false;

}

return true;

//@@end

}

When i used the same code in my application i am gett ing error in many places..

1)FileDataSource fds = new FileDataSource(<b>attach</b>);

attach cannot be resolved

2)attachmentBodyPart.setFileName(<b>FileName</b>);

fliename cannot be resolved

3)byte[] file = element.getFileUpload();

type mismatch cannot convert sting to byte[]

4)element.setFileSize(this.getFileSize(file));

method getFileSize() is undefined

5)element.setFileExtension(binaryType.getMimeType().getFileExtension());

method getFilExtension() is undefined

6)URL = this.CreateAndGetPathFileUpload(wdContext.currentContextElement().getFileUpload(),FileName);

method CreateAndGetPathFileUpload() is undefined.

7)wdContext.currentContextElement().setPATHFileUploaded(URL);

from the above error i can understand that only i have got the part of the code.

Please send me the complete coding.

some method definitions are missing....

Please help me to send the mail with attachment from the file uploaded from the file upload ui element.

Thanks in advance,

shami.

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

pls tell me where you get this sample code

Former Member
0 Kudos

hi,

I got this from the following link

plz help me ...

I am using 2004s with nwds 7.0.06.

also tell me what should be the type of the context variable FileUpload

Thanks in advance,

shami.

Former Member
0 Kudos

Hi Shami,

you should use valueattribute of type "resource".

Check my blog /people/anilkumar.vippagunta2/blog/2007/02/04/office-control-in-webdynpro-ii for reading from the resource type.

Reagrds,ANilkumar