Wednesday, February 9, 2011

gwt for mutiple uplad file and send email

Gwt upload multiple files using muti upload and Servlet.
Email with multiple file attachment, first servlet can be used for file uploading, then rpc can be used for send email.
Tips: use  a uuid to identify an email session.



@Override
public String SendMailService(String uuid, String to, String subject,
String body) {
System.out.println(uuid+" " + to + " " + subject + " " + body);
// TODO Auto-generated method stub
String from = "benma@google.com";
String host = "mail.google.com";
String fileDir = "c:\\myupload\\"+uuid;
String recipients[] = to.split(";");
// create some properties and get the default Session
 Properties props = System.getProperties();
 props.put("mail.smtp.host", host);

 Session session = Session.getInstance(props, null);
 session.setDebug(true);
 try
 {
     // create a message
     MimeMessage msg = new MimeMessage(session);
   
     msg.setFrom(new InternetAddress(from));
   
//      InternetAddress[] address = {new InternetAddress(to)};
//      msg.setRecipients(Message.RecipientType.TO, address);
   
     InternetAddress[] addressTo = new InternetAddress[recipients.length];
     for (int i = 0; i < recipients.length; i++){
         addressTo[i] = new InternetAddress(recipients[i]);
     }
     msg.setRecipients(Message.RecipientType.TO, addressTo);
   
     msg.setSubject(subject);

     // create and fill the first message part
     MimeBodyPart mbp1 = new MimeBodyPart();
     mbp1.setText(body);

  // create the Multipart and add its parts to it
     Multipart mp = new MimeMultipart();
     mp.addBodyPart(mbp1);
   
     Vector<String> vecNames = getFileNames(fileDir);
     for(String filename : vecNames) {
     // create the second message part
     MimeBodyPart mbp2 = new MimeBodyPart();
     // attach the file to the message
     FileDataSource fds = new FileDataSource(filename);
     mbp2.setDataHandler(new DataHandler(fds));
     mbp2.setFileName(fds.getName());
     mp.addBodyPart(mbp2);
     }

     // add the Multipart to the message
     msg.setContent(mp);
     // set the Date: header
     msg.setSentDate(new Date());
     // send the message
     Transport.send(msg);
 }
 catch (Exception mex) {
     mex.printStackTrace();
     return "Mail NOT successfully.";
 }
return "Your mail has been sent successfuly.";
}

No comments:

Post a Comment