Hi Ajay,

Here's some code; change as required, I grab the folder to drop the
attachments into from the config file using getInitParameter() in the init
method.

public void service(Mail mail) throws MessagingException 
    {
        try 
        {
                MimeMessage msg = (MimeMessage) mail.getMessage();
                Multipart mp = (Multipart) msg.getContent();
            
                for (int j = 0, n = mp.getCount(); j < n; j++) 
                {
                        Part part = mp.getBodyPart(j);
 
                if(part.getFileName()==null)
                                continue;
                
                //log("Content Type = " + part.getContentType()+ "
Disposition = " + part.getDisposition()+ " Filename: = " +
part.getFileName());
                      
                File outputFile = new File(folderPath+part.getFileName());
                FileOutputStream  outputStream = new
FileOutputStream(outputFile);
                InputStream inputStream = part.getInputStream();

                int i;
                while((i = inputStream.read()) != -1)
                                outputStream.write(i);
                outputStream.close();
                }



Carl


-----Original Message-----
From: Ajay Chitre [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, July 09, 2008 3:37 AM
To: James Users List
Subject: Processing Email Attachments...

When an email comes in to my James server with one ore more attachments, I
need to get the attached files and save them someplace.  Can someone
(please, please) provide a code snippet of processing email attachments
under James?

I know I can get the file name as follows:

    try {
      if (mail.getMessage().getContent() instanceof MimeMultipart) {
        MimeMultipart mmp = (MimeMultipart) mail.getMessage().getContent();

        for (int i = 0; i < mmp.getCount(); i++) {
          MimeBodyPart mbp = (MimeBodyPart) mmp.getBodyPart(i);
          System.out.println("File name: " + mbp.getFileName());
        }
      }
    } catch ( Exception e ) {
      System.out.println("Error: " + e.getMessage());
    }


Do I need to write code to explicitly read the file using InputStream?  Is
there a better way to do this?  Any help in this regard will be greatly
appreciated.  Thanks.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to