i am attaching excel file to the mail using java mail,it is rendering scrap

hi

i am attaching excel file to the mail using java mail,it is rendering scrap code when i am opening in mail, it is working fine if it is a PNG image

pls..... help me out

-

package com.cypress.jetspeed.cyutils.common;

import java.io.ByteArrayInputStream;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStream;

import java.util.Properties;

import java.util.Date;

import javax.mail.*;

import javax.mail.internet.*;

import javax.activation.*;

public class EmailSender {

EmailBean ebean;

public void sendMailattachment(EmailBean aBean) {

ebean= aBean;

File file=null;

FileWriter fw=null;

Message msg = null;

Session session = null;

Properties props = null;

MimeBodyPart bodyPart = null;

MimeBodyPart attach = null;

FileDataSource fds=null;

Multipart mp = null;

//FileReader fr=null;

String host = "mailhost.india.cypress.com";

String from = ebean.getFrom();

String to = ebean.getTo();

String cc=ebean.getCc();

String sub=ebean.getSub();

String body=ebean.getBody();

String filetype=ebean.getFiletype();

byte[] attachment=ebean.getAttachment();

int len=attachment.length;

System.out.println("attachment...........is"+len);

try {

file=File.createTempFile("temp","." + filetype.toLowerCase(),new File("C:/tmpdnld"));

InputStream in= new ByteArrayInputStream(attachment);

FileOutputStream fos=null;

fos = new FileOutputStream(file);

int n;

while((n = in.read(attachment))>0) {

fos.write(attachment,0,n);

}

fos.close();

in.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

//Setting System prperties

props = System.getProperties();

props.put("mail.smtp.host", host);

session = Session.getDefaultInstance(props, null);

session.setDebug(false);

try

{

msg = new MimeMessage(session);

msg.setFrom(new InternetAddress(from));

msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));

msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false));

msg.setSubject(sub);

msg.setSentDate(new Date());

//msg.setHeader("Attachment Test Mail", "Sample");

// Create and fill the first message part

bodyPart = new MimeBodyPart();

bodyPart.setText(body);

//Create the second message part

attach = new MimeBodyPart();

/*if((filetype.toLowerCase()).equals("png")){

attach.setContent(file,"image/png");

}

else{

attach.setContent(file,"application/");

}*/

fds = new FileDataSource(file);

attach.setDataHandler(new DataHandler(fds));

attach.setFileName(fds.getName());

// Create the Multipart and add its parts to it

mp = new MimeMultipart();

mp.addBodyPart(bodyPart);

mp.addBodyPart(attach);

// Add the Multipart to the message

msg.setContent(mp);

Transport.send(msg);

System.out.println("Mail with given attachment is Sent");

}catch (MessagingException me)

{

me.printStackTrace();

Exception ex = null;

if ((ex = me.getNextException()) != null)

{

ex.printStackTrace();

}

}

}

}

regards

srinu

[3597 byte] By [ettasrinua] at [2007-11-27 1:56:48]
# 1

You seem to be getting the file data from a byte array (attachment).You

read the data from that byte array using a ByteArrayInputStream, but you

read the data into the original byte array. That seems like a very bad

idea. Use a ByteArrayDataSource instead.

If you can't get it to work, try the sendfile.java demo program included with

JavaMail. If that program works and yours doesn't I'm sure you'll be able to

figure out what to do.

bshannona at 2007-7-12 1:31:37 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...