URGENT : Multiple Attachments using JavaMail
843830May 26 2004 — edited May 26 2004Hello ,
I'm trying to send multiple attachments to a single mailid. I have 3 text boxes in my JSP page which holds the file name that is selected . in the next page i retrieving the values of those filenames and sending it as attachment .
But when i do so , i'm getting NullPointeException when one of 2 file is choose . but when i choose 3 files to send , it is working fine . Can any one help pls,
Here is the code
-------------------------
public class Attachservlet extends HttpServlet{
public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{
String host="172.30.1.35";
String full[]=new String[3];
String part[]=new String[3];
String fname[]=new String[3];
String pname[]=new String[3];
String ss[]=new String[3];
String name[]=new String[3],conc="";
int j,jj;
String sender_email=req.getParameter("senderemail");
String sender_name=req.getParameter("sendername");
String receiver_email=req.getParameter("receiveremail");
String receiver_name=req.getParameter("receivername");
String sent_message=req.getParameter("area");
PrintWriter out=res.getWriter();
Properties props=System.getProperties();
props.put("mail.smtp.host",host);
Session session=Session.getDefaultInstance(props,null);
MimeMessage message=new MimeMessage(session);
try{
for ( jj=0;jj<3;jj++){
j=jj+1;
name[jj]="filename"+j;
pname[jj]="pname"+j;
ss[jj]=req.getParameter(name[jj]);
part[jj]=req.getParameter(pname[jj]);
if(ss[jj].length()!=0){
System.out.println("values is "+ss[jj] +"\n"+part[jj]);
System.out.println("values is "+ss[jj] +"\n"+part[jj]);
conc=conc+ss[jj]+",";
}
System.out.println(conc);
}
message.setFrom(new InternetAddress(sender_email,"Sumathi"));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(receiver_email));
message.setSubject(receiver_name);
BodyPart bodypart=new MimeBodyPart();
bodypart.setText(sent_message);
Multipart multi=new MimeMultipart();
multi.addBodyPart(bodypart);
bodypart = new MimeBodyPart();
DataSource source=new FileDataSource("test.jsp");
bodypart.setDataHandler(new DataHandler(source));
bodypart.setFileName(part[jj]);
multi.addBodyPart(bodypart);
message.setContent(multi);
Transport.send(message);
res.sendRedirect("http://sumathi/Mail/Success.jsp");
System.out.println("Message Sent");
}catch(Exception e){
System.err.println("An Exception has Arrised \t"+e);
}
}
public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException{
doPost(req,res);
}
}
Any Help pls ,
Thanks in Advance !