473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

attchments in java

oll3i
679 512MB
How do I atach files in java?

Thank YOU
Oct 3 '13 #1
12 2666
r035198x
13,262 8TB
Attach them to where?
Oct 3 '13 #2
oll3i
679 512MB
@r035198x
I want to write sending attachemnts with email.
Oct 3 '13 #3
r035198x
13,262 8TB
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
679 512MB
but how do i get the filename that i want to upload
if i have html form <input type="file" ... >
Oct 3 '13 #5
r035198x
13,262 8TB
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
679 512MB
but how do i get the filename?


String filename = mrequest.getParameter("file");
returns null
Oct 3 '13 #7
r035198x
13,262 8TB
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
679 512MB
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
679 512MB
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
r035198x
13,262 8TB
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
679 512MB
ok
i will do so ... :)
Oct 3 '13 #12
oll3i
679 512MB
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

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

Similar topics

2
by: Michael | last post by:
Hello I am trying to write a Java-Program which converts a XML-file in a HTML. It should take the Transformation-file from the XML-file itself. Below find a possible XML-file: <?xml...
0
by: Ravi Tallury | last post by:
Hi We are having issues with our application, certain portions of it stop responding while the rest of the application is fine. I am attaching the Java Core dump. If someone can let me know what...
1
by: ptaz | last post by:
Hi I'm trying to run a web page but I get the following error. Ca anyone please tell me a solution to this. Thanks Ptaz HTTP Status 500 - type Exception report
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
0
by: Markus Wollny | last post by:
Hello! When I try to run ./configure --with-java, it complains that ant doesn't work. However ant is installed, as is the latest Java SDK 1.4.2 from sun, PATH and JAVA_HOME are set correctly; ...
0
by: mailkhurana | last post by:
Hii , I am trying to use a type 2 driver to connect to DB2 0n AIX 5 I have a small java test to class to establish a conneciton with the db .. I am NOT using WAS or any appserver When I try to...
5
by: TZESENG | last post by:
DECEMBER 13, 2005 . Editions: N. America | Europe | Asia | Edition Preference News Analysis By Steve Hamm Source: http://www.businessweek.com/technology/content/dec2005/tc20051213_042973.htm...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
0
oll3i
by: oll3i | last post by:
package library.common; import java.sql.ResultSet; public interface LibraryInterface { public ResultSet getBookByAuthor(String author); public ResultSet getBookByName(String name);
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.