By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,653 Members | 1,483 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,653 IT Pros & Developers. It's quick & easy.

sending attachments

oll3i
100+
P: 679
The message comes to the email account without attachment?
That's the code for attachment

Expand|Select|Wrap|Line Numbers
  1. MimeBodyPart messageBodyPart = new MimeBodyPart();
  2.  
  3.         Multipart multipart = new MimeMultipart();
  4.  
  5.         messageBodyPart = new MimeBodyPart();
  6.  
  7.  
  8.         List fileItems = upload.parseRequest(request);
  9.  
  10.       // Process the uploaded file items
  11.       Iterator i = fileItems.iterator();
  12.  
  13.  
  14.       while ( i.hasNext () ) 
  15.       {
  16.          FileItem fi = (FileItem)i.next();
  17.          if ( !fi.isFormField () )    
  18.          {
  19.             // Get the uploaded file parameters
  20.             String fieldName = fi.getFieldName();
  21.             String fileName = fi.getName();
  22.             String contentType = fi.getContentType();
  23.             boolean isInMemory = fi.isInMemory();
  24.             long sizeInBytes = fi.getSize();
  25.             // Write the file
  26.             if( fileName.lastIndexOf("\\") >= 0 ){
  27.                file = new File( filePath + 
  28.                fileName.substring( fileName.lastIndexOf("\\"))) ;
  29.             }else{
  30.                file = new File( filePath + 
  31.                fileName.substring(fileName.lastIndexOf("\\")+1)) ;
  32.             }
  33.             fi.write( file ) ;
  34.              ///String filename = mrequest.getParameter("file");
  35.             System.out.println("Uploaded Filename: " + fileName + "<br>");
  36.  
  37.  
  38.  
  39.  
  40.         DataSource source = new FileDataSource(file);
  41.         messageBodyPart.setDataHandler(new DataHandler(source));
  42.         messageBodyPart.setFileName(fileName);
  43.         multipart.addBodyPart(messageBodyPart);
  44.  
  45.         message.setContent(multipart);
  46.          }
  47.       }
  48.  
  49.       Transport.send(message);
  50.      System.out.println("send");
  51.     }
Oct 10 '13 #1
Share this Question
Share on Google+
2 Replies


10K+
P: 13,264
Print out the value of your file path and check to see if there is a file there or if you are using the wrong path. For uploaded in memory files just write the bytes to message body part.
Oct 11 '13 #2

oll3i
100+
P: 679
I have a loop but it does not go through the loop and goes to sending mail and sends mail without attachments

Expand|Select|Wrap|Line Numbers
  1. for(FileItem item : multiparts){
  2.                     System.out.println("(FileItem item : multiparts");
  3.                     if(!item.isFormField()){
  4.                         String name = new File(item.getName()).getName();
  5.                         System.out.println("name="+name);
  6.  
  7.                         item.write( new File(UPLOAD_DIRECTORY + File.separator + name));
  8.               DataSource source =    
  9.                 new FileDataSource(new File(UPLOAD_DIRECTORY + File.separator + name));   
  10.               messageBodyPart.setDataHandler(   
  11.                 new DataHandler(source));   
  12.               messageBodyPart.setFileName(name);   
  13.               multipart.addBodyPart(messageBodyPart);                   
  14.                    System.out.println("name="+name);
  15.                     }
  16. }
  17.  
  18.                //File uploaded successfully
  19.                request.setAttribute("message", "File Uploaded Successfully");
  20.             } catch (Exception ex) {
  21.                request.setAttribute("message", "File Upload Failed due to " + ex);
  22.             }           
  23.  
  24.         }else{
  25.             request.setAttribute("message", 
  26.                                  "Sorry this Servlet only handles file upload request");
  27.         }
  28.  
  29.         ////request.getRequestDispatcher("/result.jsp").forward(request, response);
  30.       Transport.send(msg);   
  31.               System.out.println("success....................................");   
  32.  
  33.  
  34.     } catch(MessagingException e){   
  35.                   e.printStackTrace();         
  36.               }
  37.   catch (UploadException e){ e.printStackTrace();  }
  38.  
  39.  
  40.  
  41.     }
  42.     }
  43. }
  44.  
why multiparts.size() is 0 when i upload a file
Oct 11 '13 #3

Post your reply

Sign in to post your reply or Sign up for a free account.