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

