sending attachments

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oll3i
    Contributor
    • Mar 2007
    • 679

    sending attachments

    The message comes to the email account without attachment?
    That's the code for attachment

    Code:
    MimeBodyPart messageBodyPart = new MimeBodyPart();
    
            Multipart multipart = new MimeMultipart();
    
            messageBodyPart = new MimeBodyPart();
           
            
            List fileItems = upload.parseRequest(request);
    	
          // Process the uploaded file items
          Iterator i = fileItems.iterator();
    
         
          while ( i.hasNext () ) 
          {
             FileItem fi = (FileItem)i.next();
             if ( !fi.isFormField () )	
             {
                // Get the uploaded file parameters
                String fieldName = fi.getFieldName();
                String fileName = fi.getName();
                String contentType = fi.getContentType();
                boolean isInMemory = fi.isInMemory();
                long sizeInBytes = fi.getSize();
                // Write the file
                if( fileName.lastIndexOf("\\") >= 0 ){
                   file = new File( filePath + 
                   fileName.substring( fileName.lastIndexOf("\\"))) ;
                }else{
                   file = new File( filePath + 
                   fileName.substring(fileName.lastIndexOf("\\")+1)) ;
                }
                fi.write( file ) ;
                 ///String filename = mrequest.getParameter("file");
                System.out.println("Uploaded Filename: " + fileName + "<br>");
                
            
            
            
            DataSource source = new FileDataSource(file);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(fileName);
            multipart.addBodyPart(messageBodyPart);
    
            message.setContent(multipart);
             }
          }
          
          Transport.send(message);
         System.out.println("send");
        }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    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.

    Comment

    • oll3i
      Contributor
      • Mar 2007
      • 679

      #3
      I have a loop but it does not go through the loop and goes to sending mail and sends mail without attachments

      Code:
      for(FileItem item : multiparts){
                          System.out.println("(FileItem item : multiparts");
                          if(!item.isFormField()){
                              String name = new File(item.getName()).getName();
                              System.out.println("name="+name);
                              
                              item.write( new File(UPLOAD_DIRECTORY + File.separator + name));
                    DataSource source =    
                      new FileDataSource(new File(UPLOAD_DIRECTORY + File.separator + name));   
                    messageBodyPart.setDataHandler(   
                      new DataHandler(source));   
                    messageBodyPart.setFileName(name);   
                    multipart.addBodyPart(messageBodyPart);                   
                         System.out.println("name="+name);
                          }
      }
                  
                     //File uploaded successfully
                     request.setAttribute("message", "File Uploaded Successfully");
                  } catch (Exception ex) {
                     request.setAttribute("message", "File Upload Failed due to " + ex);
                  }           
                
              }else{
                  request.setAttribute("message", 
                                       "Sorry this Servlet only handles file upload request");
              }
           
              ////request.getRequestDispatcher("/result.jsp").forward(request, response);
            Transport.send(msg);   
                    System.out.println("success....................................");   
                     
            
          } catch(MessagingException e){   
                        e.printStackTrace();         
                    }
        catch (UploadException e){ e.printStackTrace();  }
                    
         
                     
          }
          }
      }
      why multiparts.size () is 0 when i upload a file

      Comment

      Working...