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