473,403 Members | 2,222 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,403 software developers and data experts.

sending attachments

oll3i
679 512MB
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
2 1623
r035198x
13,262 8TB
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
679 512MB
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

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

Similar topics

4
by: Liz Patton | last post by:
Here's the exception: System.Exception: Unable to send mail: Could not access 'CDO.Message' object. ---> System.Web.HttpException: Could not access 'CDO.Message' object. --->...
0
by: Alejandro Penate-Diaz | last post by:
hi. I am sending attachments and for some reason the files are getting corrupted and a different size than the original file. I am sending word and excel documents. below is the code I am using ...
0
by: Wayne | last post by:
When sending a snapshot file as an Outlook attachment from within Access I have the following intermittent problem. Sometimes when I press the "Send" button in Outlook the message will not send. ...
7
by: Ray Booysen | last post by:
Hi all I'm sending email via ASP.NET in HTML mode. Each email has exactly one attachment and I do have full access to the SMTP server. However, if I send the email in HTML format, the...
0
by: bambi | last post by:
I am facing issues while adding attachments in an E-mail via Mapisend. The code I use is below. If I try to send , I am getting error messgae in Mailmessage.12. Mailmessage.12 is expected as numeric,...
1
by: =?Utf-8?B?QnJhbmR5?= | last post by:
I am trying to set up my windows live account and it is asking for my incomming POP3/IMAP server and my outgoing SMPT server. Can anyone please give me an example of what this would be. I am sure...
0
Jugmike
by: Jugmike | last post by:
I have a strange problem: The utility of the code i have written is to generate a file at runtime and send it to the users. Now i am using 2 functions to generate files 1. The first function...
1
by: budyerr | last post by:
All, I am trying to build a email submission form using asp.net. I currently have a web form page that will upload to my webhosting server, attach to email then delete the file after sending. ...
0
by: =?Utf-8?B?VGhlcmVzYSBCZWxs?= | last post by:
I am having issues with sending attachments and pictures through my windows email. I have to go to the aol page to do this can some one give me some advice on sending pic through wm? -- Theresa...
1
by: spl1490 | last post by:
Hello, First off, I would like to say that I'm sorry for having to ask a programming site where to find a download for the outlook reference (msoutl9.olb) that is supposed to be in my C: drive....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.