package com.oreilly.jent.javamail;

/**
 * In general, you may use the code in this book in your programs and 
 * documentation. You do not need to contact us for permission unless 
 * you're reproducing a significant portion of the code. For example, 
 * writing a program that uses several chunks of code from this book does 
 * not require permission. Selling or distributing a CD-ROM of examples 
 * from O'Reilly books does require permission. Answering a question by 
 * citing this book and quoting example code does not require permission. 
 * Incorporating a significant amount of example code from this book into 
 * your product's documentation does require permission.
 * 
 * We appreciate, but do not require, attribution. An attribution usually 
 * includes the title, author, publisher, and ISBN. For example: 
 * 
 *   "Java Enterprise in a Nutshell, Third Edition, 
 *    by Jim Farley and William Crawford 
 *    with Prakash Malani, John G. Norman, and Justin Gehtland. 
 *    Copyright 2006 O'Reilly Media, Inc., 0-596-10142-2."
 *  
 *  If you feel your use of code examples falls outside fair use or the 
 *  permission given above, feel free to contact us at 
 *  permissions@oreilly.com.
 */

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.mail.Address;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeBodyPart;

public class MimeShow { 
  static String mailhost = "pop.company.com"; 
  
  public static void main(String[] args) { 
    Session session = 
      Session.getDefaultInstance(System.getProperties(), null); 
    
    try { 
      Store popStore = session.getStore("pop3"); 
      popStore.connect(mailhost, "username", "password"); 
      
      // Get a default folder, and use it to open a real folder 
      Folder defaultPopFolder = popStore.getDefaultFolder(); 
      defaultPopFolder = defaultPopFolder.getFolder("INBOX"); 
      defaultPopFolder.open(Folder.READ_ONLY); 
      
      Message[] msgs = defaultPopFolder.getMessages(); 
      if (msgs != null) 
        for (int i = 0; i < msgs.length; i++) { 
          // Display the message envelope 
          System.out.println("\n\r----------------------------------"); 
          System.out.println("Subject: " + msgs[i].getSubject()); 
          Address[] from = msgs[i].getFrom(); 
          if (from != null) 
            for (int a = 0; a < from.length; a++) 
              System.out.println("From: " + from[a]); 
          // Display the content 
          if (msgs[i].isMimeType("text/plain")) { 
            System.out.println((String)msgs[i].getContent()); 
          } 
          else if (msgs[i].isMimeType("multipart/*")) { 
            Multipart mp = (Multipart)msgs[i].getContent(); 
            int count = mp.getCount(); 
            for (int m = 0; m < count; m++) { 
              showBodyPart((MimeBodyPart)mp.getBodyPart(m)); 
            } 
          } 
          else if (msgs[i].isMimeType("message/rfc822")) { 
            System.out.println("Nested Message"); 
          } 
          else {
            System.out.println(msgs[i].getContent().toString()); 
          } 
        }// end for 
    } 
    catch (MessagingException me) { 
      me.printStackTrace(System.out); 
    } 
    catch (IOException ie) { 
      ie.printStackTrace(System.out); 
    } 
  } // End main() 
  
  // Show or save a MIME body part; very simple 
  public static void showBodyPart(MimeBodyPart p) { 
    try { 
      String contentType = p.getContentType(); 
      System.out.println("-- MIME Part: " + contentType); 
      if (contentType.startsWith("text/")) 
        System.out.println((String)p.getContent()); 
      else if (p.getFileName() != null) { 
        File f = new File(p.getFileName()); 
        FileOutputStream fos = new FileOutputStream(f); 
        byte[] b = new byte[1024]; 
        InputStream is = p.getInputStream(); 
        fos.write(b); 
        is.close(); 
        fos.close(); 
        System.out.println("Attachment saved as " + f.getAbsolutePath()); 
      } 
      else { 
        System.out.println( 
        "Cannot display this content type and no filename supplied."); 
      } 
    } 
    catch(Exception e) { 
      System.out.println("Exception Caught: " + e.getMessage()); 
    } 
  } // End showBodyPart() 
} // End class 