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.

attchments in java

oll3i
100+
P: 679
How do I atach files in java?

Thank YOU
Oct 3 '13 #1
Share this Question
Share on Google+
12 Replies


10K+
P: 13,264
Attach them to where?
Oct 3 '13 #2

oll3i
100+
P: 679
@r035198x
I want to write sending attachemnts with email.
Oct 3 '13 #3

10K+
P: 13,264
You create a multipart body and set the file
Expand|Select|Wrap|Line Numbers
  1.  
  2.  BodyPart messageBodyPart = new MimeBodyPart();
  3.  Multipart multipart = new MimeMultipart();
  4. //....
  5.  // Set the normal text part 
  6.  multipart.addBodyPart(messageBodyPart);
  7.  messageBodyPart = new MimeBodyPart();
  8.  DataSource source = new FileDataSource("yourfilePath.txt");
  9.  messageBodyPart.setDataHandler(new DataHandler(source));
  10.  messageBodyPart.setFileName(filename);
  11.  multipart.addBodyPart(messageBodyPart);
Oct 3 '13 #4

oll3i
100+
P: 679
but how do i get the filename that i want to upload
if i have html form <input type="file" ... >
Oct 3 '13 #5

10K+
P: 13,264
You are uploading the file to the server right? You can use a ByteArrayDataSource instead of a FileDatasource if you are getting the file as a byte[].
Oct 3 '13 #6

oll3i
100+
P: 679
but how do i get the filename?


String filename = mrequest.getParameter("file");
returns null
Oct 3 '13 #7

10K+
P: 13,264
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 ByteArrayDataSource which you create from the byte[]. How are you uploading the file to the server?
Oct 3 '13 #8

oll3i
100+
P: 679
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. <html>
  4.     <head>
  5.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  6.         <link rel="stylesheet" type="text/css" href="css.css"/>
  7.         <title>HRMailer</title>
  8.     </head>
  9.     <body>
  10.        <table width="900">
  11.             <tr>
  12.                 <td valign="top">     
  13.  
  14.        <table> <form action="send.jsp" method="post" enctype="multipart/form-data">
  15. <input type="hidden" name="MAX_FILE_SIZE" value="120000" />
  16.  
  17.            <tr><td>Subject:</td>
  18.                <td><input type="text" name="subject" value=""><td>
  19.            </tr>
  20.            <tr>
  21.                <td>From</td>
  22.                <td>
  23.                    <% //HttpSession session = request.getSession();%>
  24. <input type="text" name="from" value="<%=(String)session.getAttribute("username")%>">
  25. </td> </tr>
  26.  
  27.            <tr>
  28. <td>To:</td>
  29. <td><input type="text" name="to" value=""></td>
  30.            </tr>
  31.            <tr>
  32.            <tr>
  33. <td>Body:</td> 
  34. <td valign="buttom">
  35. <textarea rows="10" cols="30" name="body" value="">
  36.  
  37. </textarea>
  38.  
  39.         </td>
  40.            </tr>
  41.             <tr>
  42.                 <td>
  43.                     attachment
  44.                 </td>
  45.                <td>
  46.                    <input type="file" name="file" size="30" value=""> 
  47.                </td>
  48.            </tr>
  49.            <tr>
  50.                <td>
  51.                    <input type="submit" name="send" value="send"> 
  52.                </td>
  53.            </tr>
  54.        </form>
  55.        </table> 
  56.          </td>
  57.         </tr>
  58.        </table>
  59.     </body>
  60. </html>
  61.  
Oct 3 '13 #9

oll3i
100+
P: 679
and then

Expand|Select|Wrap|Line Numbers
  1. Session session = Session.getDefaultInstance(properties);
  2.  
  3.  
  4.          // Create a default MimeMessage object.
  5.          MimeMessage message = new MimeMessage(session);
  6.  
  7.          // Set From: header field of the header.
  8.          message.setFrom(new InternetAddress(from));
  9.  
  10.          // Set To: header field of the header.
  11.          message.addRecipient(Message.RecipientType.TO,
  12.                                   new InternetAddress(to));
  13.  
  14.          // Set Subject: header field
  15.          message.setSubject(subject);
  16.  
  17.          // Create the message part 
  18.          BodyPart messageBodyPart = new MimeBodyPart();
  19.  
  20.          // Fill the message
  21.          messageBodyPart.setText(body);
  22.  
  23.          // Create a multipar message
  24.          Multipart multipart = new MimeMultipart();
  25.  
  26.          // Set text message part
  27.          multipart.addBodyPart(messageBodyPart);
  28.  
  29.          // Part two is attachment
  30.          messageBodyPart = new MimeBodyPart();
  31.          String filename = mrequest.getParameter("file");
  32.          DataSource source = new FileDataSource(filename);
  33.          messageBodyPart.setDataHandler(new DataHandler(source));
  34.          messageBodyPart.setFileName(filename);
  35.          multipart.addBodyPart(messageBodyPart);
  36.  
  37.          // Send the complete message parts
  38.          message.setContent(multipart );
  39.  
  40.          // Send message
  41.          Transport.send(message);
  42.          System.out.println("Sent message successfully....");
  43. }
  44.    }
  45.       catch (MessagingException ex) {
  46.          ex.printStackTrace();
  47.       }
Oct 3 '13 #10

10K+
P: 13,264
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.
Oct 3 '13 #11

oll3i
100+
P: 679
ok
i will do so ... :)
Oct 3 '13 #12

oll3i
100+
P: 679
Expand|Select|Wrap|Line Numbers
  1.  if(ServletFileUpload.isMultipartContent(request)){
  2.  
  3.                 List<FileItem> multiparts = new ServletFileUpload(
  4.                                          new DiskFileItemFactory()).parseRequest(request);
  5.  
  6.                 for(FileItem item : multiparts){
  7.                     if(!item.isFormField()){
  8.                         String file = new File(item.getName()).getName();
  9.                         item.write( new File(UPLOAD_DIRECTORY + File.separator + file));
  10.                     DataSource source = new FileDataSource(UPLOAD_DIRECTORY + File.separator +file);
  11.          messageBodyPart.setDataHandler(new DataHandler(source));
  12.          messageBodyPart.setFileName(file);
  13.  
  14.                     }
  15.                 }
  16.          }
it does not upload the file
email sends without any error but does not come to email account.
Oct 3 '13 #13

Post your reply

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