Excel to File by email

HiIm using the contentType to generete one excel file, but I need to send this file directly by email. Anyone know how to do this?If I can save the file in the server I can attch to email and then send the file.ThanksLu韘 Matos
[261 byte] By [lghm@aeiou.pta] at [2007-10-2 4:37:03]
# 1

Mail can be sent using SMTP.

Java Mail API provided u ways to attach files as attachments to the Mail Content and send it.

You need not save the file to the Server Directory to attach the file to the Mail Content.

Search the forum for attachments in mails for more details and Information.

Thanks and regards,

Pazhanikanthan. P

pazhanikanthana at 2007-7-16 0:09:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Hi pazhanikanthanThanks for your response.Im usgin a class to send email with attachments, but this class need a real path file to attach the file.I will search what you write.ThanksLu韘 Matos
lghm@aeiou.pta at 2007-7-16 0:09:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
HiI forgot to say that when I generate the excel file the browser prompt to save the file to disk. In this case, I have to send the file by email without save it to disk.ThanksLu韘 Matos
lghm@aeiou.pta at 2007-7-16 0:09:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Hi pazhanikanthanI do a search in the forum and I cant found anything that can help me to send the excel file directly by email.Every examples include a real file in the server directory.Do you know how can I do this.ThanksLu韘 Matos
lghm@aeiou.pta at 2007-7-16 0:09:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Hello Lu韘 Matos

Tell me the sequence of the flow in your application. You are confusing me as sometimes you are mentioning that you are downloading it thru browser and sometimes you are mentioning that you need to send email with attachment?

Please give me proper Information for me to provide solution.

Yes I have done file attachment without writing the content into the Server. We can definietly do that.

Thanks and regards,

Pazhanikanthan. P

pazhanikanthana at 2007-7-16 0:09:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

Hi pazhanikanthan

In my application I have to generate some files formats. In the specific case, I have to generate one Excel that must be sent by e-mail as attachment.

Actually the file is generated by the ContentType and when finish ask to user to save the file to disk (user interaction).

What I need is when the page generate the excel don't ask to save to user disk, but send it directly to user e-mail.

Thanks a lot for your help.

Lu韘 Matos

lghm@aeiou.pta at 2007-7-16 0:09:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

First of all when you say

"Actually the file is generated by the ContentType "

Do you mean that you change the Content Type as MS Excel in JSP to paint the excel in the screen?

If yes, then change the approach to generate the excel using some freewares like JExcel or some apis for File attachment.

Now once you create the excel using the above API,

1. Convert it into byte array in memory. You need not store in Server physical location.

2. Load the byte array into the attached MailDataSource.java.

3. Simple Mail API call steps would be

// Create part for the attachment

bodyPart = new MimeBodyPart();

// Add a header to connect to the HTML

bodyPart.setDataHandler(new DataHandler(new MailDataSource ((byte []) value.getAttachmentContent (), value.getContentType ())));

// Add part to multi-part

multiPart.addBodyPart (bodyPart);

Code for MailDataSource

--

// Java specific Imports

import java.io.InputStream;

import java.io.OutputStream;

import java.io.ByteArrayInputStream;

import javax.activation.DataSource;

/**

* Mail Data Source Class. Encapsulates the Data Source obtained as byte array [] into

* Data Source Implementation for Mail API

*/

public class MailDataSource implements DataSource

{

String mimeType = null;

String name=null;

byte [] bArrContent=null;

/**

* Default Constructor

* @param byte [] the content byte array to encapsulate

* @param string MIME-TYPE of the Content being encapsulated

*/

public MailDataSource (byte [] bArrContent, String mimeType)

{

this.mimeType=mimeType;

this.bArrContent= bArrContent;

}

/**

* Get Content Type

* @param none

* @return String containing MIME-TYPE of the Content

*/

public String getContentType ()

{

return mimeType;

}

/**

* Get Input Stream

* @param none

* @return InputStream containing the Content

*/

public InputStream getInputStream ()

{

ByteArrayInputStream contentStream = new ByteArrayInputStream (bArrContent);

return contentStream;

}

/**

* Get Name associated to the Content

* @param none

* @return String

*/

public String getName ()

{

return name;

}

/**

* Get Output Stream

* @param none

* @return OutputStream

*/

public OutputStream getOutputStream ()

{

return null;

}

}

Hope this helps you.

Thanks and regards,

Pazhanikanthan. P

pazhanikanthana at 2007-7-16 0:09:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
Hi pazhanikanthanThanks very much for the help. I will try to use the JExcel.Just one question: Do your know if the JExcel can generate from HTML to Excel? Because the data that will generate the Excel are in HTML format.ThanksLu韘 Matos
lghm@aeiou.pta at 2007-7-16 0:09:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9
I dont think JExcel can generate Excel from HTML.You should use the same Datastructure (which you used to build HTML) to rebuild excel objects using JExcel.Hope this helps you.
pazhanikanthana at 2007-7-16 0:09:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10
Hi pazhanikanthanI think that JExcel can't help me for what I want.Do you know any way to use the content type and save the result in the server disk? without ask for save as?ThanksLu韘 Matos
lghm@aeiou.pta at 2007-7-16 0:09:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11

using the contentype will never work to send an email.

the contentype is used to say to your browser the content should be displayed not as HTMl but as excel.

When you want to mail it you willl need to create the excel file yourself. Either with JExcel or with the Jakarta POI package

pgeuensa at 2007-7-16 0:09:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...