attchments in java

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

    attchments in java

    How do I atach files in java?

    Thank YOU
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Attach them to where?

    Comment

    • oll3i
      Contributor
      • Mar 2007
      • 679

      #3
      Originally posted by r035198x
      Attach them to where?
      I want to write sending attachemnts with email.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        You create a multipart body and set the file
        [CODE=java]
        BodyPart messageBodyPart = new MimeBodyPart();
        Multipart multipart = new MimeMultipart() ;
        //....
        // Set the normal text part
        multipart.addBo dyPart(messageB odyPart);
        messageBodyPart = new MimeBodyPart();
        DataSource source = new FileDataSource( "yourfilePath.t xt");
        messageBodyPart .setDataHandler (new DataHandler(sou rce));
        messageBodyPart .setFileName(fi lename);
        multipart.addBo dyPart(messageB odyPart);[/CODE]

        Comment

        • oll3i
          Contributor
          • Mar 2007
          • 679

          #5
          but how do i get the filename that i want to upload
          if i have html form <input type="file" ... >

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            You are uploading the file to the server right? You can use a ByteArrayDataSo urce instead of a FileDatasource if you are getting the file as a byte[].

            Comment

            • oll3i
              Contributor
              • Mar 2007
              • 679

              #7
              but how do i get the filename?


              String filename = mrequest.getPar ameter("file");
              returns null

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                If you have a the file in a byte[] on the server side then you don't need a file path. You would just use the ByteArrayDataSo urce which you create from the byte[]. How are you uploading the file to the server?

                Comment

                • oll3i
                  Contributor
                  • Mar 2007
                  • 679

                  #9
                  Code:
                          
                  
                  <html>
                      <head>
                          <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                          <link rel="stylesheet" type="text/css" href="css.css"/>
                          <title>HRMailer</title>
                      </head>
                      <body>
                         <table width="900">
                              <tr>
                                  <td valign="top">     
                                     
                         <table> <form action="send.jsp" method="post" enctype="multipart/form-data">
                  <input type="hidden" name="MAX_FILE_SIZE" value="120000" />
                  
                             <tr><td>Subject:</td>
                                 <td><input type="text" name="subject" value=""><td>
                             </tr>
                             <tr>
                                 <td>From</td>
                                 <td>
                                     <% //HttpSession session = request.getSession();%>
                  <input type="text" name="from" value="<%=(String)session.getAttribute("username")%>">
                  </td> </tr>
                           
                             <tr>
                  <td>To:</td>
                  <td><input type="text" name="to" value=""></td>
                             </tr>
                             <tr>
                             <tr>
                  <td>Body:</td> 
                  <td valign="buttom">
                  <textarea rows="10" cols="30" name="body" value="">
                  
                  </textarea>
                  
                          </td>
                             </tr>
                              <tr>
                                  <td>
                                      attachment
                                  </td>
                                 <td>
                                     <input type="file" name="file" size="30" value=""> 
                                 </td>
                             </tr>
                             <tr>
                                 <td>
                                     <input type="submit" name="send" value="send"> 
                                 </td>
                             </tr>
                         </form>
                         </table> 
                           </td>
                          </tr>
                         </table>
                      </body>
                  </html>

                  Comment

                  • oll3i
                    Contributor
                    • Mar 2007
                    • 679

                    #10
                    and then

                    Code:
                    Session session = Session.getDefaultInstance(properties);
                    
                        
                             // Create a default MimeMessage object.
                             MimeMessage message = new MimeMessage(session);
                    
                             // Set From: header field of the header.
                             message.setFrom(new InternetAddress(from));
                    
                             // Set To: header field of the header.
                             message.addRecipient(Message.RecipientType.TO,
                                                      new InternetAddress(to));
                    
                             // Set Subject: header field
                             message.setSubject(subject);
                    
                             // Create the message part 
                             BodyPart messageBodyPart = new MimeBodyPart();
                    
                             // Fill the message
                             messageBodyPart.setText(body);
                             
                             // Create a multipar message
                             Multipart multipart = new MimeMultipart();
                    
                             // Set text message part
                             multipart.addBodyPart(messageBodyPart);
                    
                             // Part two is attachment
                             messageBodyPart = new MimeBodyPart();
                             String filename = mrequest.getParameter("file");
                             DataSource source = new FileDataSource(filename);
                             messageBodyPart.setDataHandler(new DataHandler(source));
                             messageBodyPart.setFileName(filename);
                             multipart.addBodyPart(messageBodyPart);
                    
                             // Send the complete message parts
                             message.setContent(multipart );
                    
                             // Send message
                             Transport.send(message);
                             System.out.println("Sent message successfully....");
                    }
                       }
                          catch (MessagingException ex) {
                             ex.printStackTrace();
                          }

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      As I explained in your thread http://bytes.com/topic/java/answers/...variables-null
                      Read about how to upload files to the server side in java first. Google is your friend.

                      Comment

                      • oll3i
                        Contributor
                        • Mar 2007
                        • 679

                        #12
                        ok
                        i will do so ... :)

                        Comment

                        • oll3i
                          Contributor
                          • Mar 2007
                          • 679

                          #13
                          Code:
                           if(ServletFileUpload.isMultipartContent(request)){
                                      
                                          List<FileItem> multiparts = new ServletFileUpload(
                                                                   new DiskFileItemFactory()).parseRequest(request);
                                         
                                          for(FileItem item : multiparts){
                                              if(!item.isFormField()){
                                                  String file = new File(item.getName()).getName();
                                                  item.write( new File(UPLOAD_DIRECTORY + File.separator + file));
                                              DataSource source = new FileDataSource(UPLOAD_DIRECTORY + File.separator +file);
                                   messageBodyPart.setDataHandler(new DataHandler(source));
                                   messageBodyPart.setFileName(file);
                                              
                                              }
                                          }
                                   }
                          it does not upload the file
                          email sends without any error but does not come to email account.

                          Comment

                          Working...