cancel
Showing results for 
Search instead for 
Did you mean: 

Attaching an excel file

Former Member
0 Kudos

Hi all

In my present assignment I want to give an option to the user to attach an excel file and send it as a mail.

Does anybody know how to do that?

How do we create mail sending option with and without attachment?

If anybody knows the answer for this please reply. Its urgent.

(If there is sample code please send that too)

Thanks and Regards

Do i have to change any properties of the FileUpload UIElement?

Message was edited by:

aparnna prasad

Accepted Solutions (0)

Answers (2)

Answers (2)

roberto_tagliento
Active Contributor
0 Kudos

I did this.

With FileUpload UIElement on upload event save the fileto your server.

On send event launch

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


  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
  }

Former Member
0 Kudos

Hi,

You can use fileupload UIElement in webdynpro to select the excel/any file from the UI and use javamail APIs for sending mail.

Ex for javamail:

Properties props = new Properties();

props.put("mail.smtp.host", "<<Value>>");

Session mailsession = Session.getDefaultInstance(props, null);

Message msg = new MimeMessage(mailsession);

StringBuffer Cc = new StringBuffer();

msg.setFrom(new InternetAddress(From));

InternetAddress[] Toaddress = new InternetAddress[To.size()];

for (int Tolist = 0; Tolist < To.size(); Tolist++) {

Toaddress[Tolist] = new InternetAddress(To.get(Tolist).toString());

}

msg.setRecipients(MimeMessage.RecipientType.TO, Toaddress);

InternetAddress[] address = new InternetAddress[CC.size()];

for (int k = 0; k < CC.size(); k++) {

address[k] = new InternetAddress(CC.get(k).toString());

}

if (CC.size() > 0)

msg.setRecipients(MimeMessage.RecipientType.CC, address);

if (!Attachment.trim().equals("")) {

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);

Where To,CC ate list of emailIDs.

Regards, Anilkumar