網路城邦
上一篇 回創作列表 下一篇   字體:
利用JavaMail送件 & 取得郵件資訊
2009/06/05 16:06:13瀏覽896|回應0|推薦1

******************


 static String host = "localhost";
 static String username = "jordan";
 static String password = "god";
 static String msgText1 = "test  test  11111111  localhost";
 static String msgText2 = "This is the text in the message attachment.";


    public static void getDetailHeader () throws Exception {
   String mailStoreProtocol = "pop3";  //"imap";
   String mailBox = "INBOX";//inbox
      URLName server = new URLName(host);
   Properties props = new Properties();
   props.put("mail.smtp.host", host);  //設定SMTP Server,而"mail.smtp.host"是固定的
   MimeMultipart part = null;

   // Get session
   Session session = Session.getDefaultInstance(props, null);

   // Get the store
   Store store = session.getStore(mailStoreProtocol);

   // Connect to store
   try {
    store.connect(host, username, password);
   } catch (MessagingException mex) {
    System.out.println("連結mail server error,錯誤InboxerUtils.scanInbox() MessagingException : " + mex);   
   }

   // Get folder
   Folder folder = store.getFolder(mailBox);

      if (folder == null) {
        System.out.println("Folder " + server.getFile() + " not found.");
        System.exit(1);
      }
      folder.open(Folder.READ_ONLY);

      Message[] messages = folder.getMessages();
       
      for (int i = 0; i < messages.length; i++) {
       System.out.println("**********************Message " + (i + 1) + "********START***********");

         System.out.println("From: " + InternetAddress.toString(messages[i].getFrom()));
         System.out.println("Reply-to: " + InternetAddress.toString(messages[i].getReplyTo()));
         String to = InternetAddress.toString(messages[i].getRecipients(Message.RecipientType.TO));
         System.out.println("To: " + to);
         String cc = InternetAddress.toString(messages[i].getRecipients(Message.RecipientType.CC));
         System.out.println("Cc: " + cc);
         String bcc = InternetAddress.toString(messages[i].getRecipients(Message.RecipientType.BCC));
         System.out.println("Bcc: " + bcc);
         System.out.println("Subject: " + messages[i].getSubject());
         System.out.println("Sent: " + messages[i].getSentDate());
         System.out.println("Received: " + messages[i].getReceivedDate());       

      StringBuffer AllHeaders = new StringBuffer();
      Enumeration headers = messages[i].getAllHeaders();
      while (headers.hasMoreElements()) {
              Header h = (Header) headers.nextElement();
              AllHeaders.append(h.getName() + ": " + h.getValue()); 
          }
         System.out.println("AllHeaders: " + AllHeaders);       
         System.out.println("AllRecipients: " + InternetAddress.toString(messages[i].getAllRecipients()));

    part = (MimeMultipart) (messages[i].getContent());

    StringBuffer ContentType = new StringBuffer();
    for (int j = 0; j < part.getCount(); j++) {
     if (part.getBodyPart(j).getContentType()!=null){
      ContentType.append(part.getBodyPart(j).getContentType().toString());
     }      
    }
    System.out.println("ContentType: " + ContentType);

    StringBuffer Content = new StringBuffer();
    for (int j = 0; j < part.getCount(); j++) {
     if (part.getBodyPart(j).getContent()!=null){
      Content.append("\n\t" + part.getBodyPart(j).getContent().toString());
     }      
    }
    System.out.println("Content: " + Content);
    System.out.println("**********************Message " + (i + 1) + " ********END***********");       
      }
  
      folder.close(false);
    }
  }

  class MailAuthenticator extends Authenticator {
   static String username = "admin";
   static String password = "1234";
    public MailAuthenticator() {
    }

    public PasswordAuthentication getPasswordAuthentication() {
      return new PasswordAuthentication(username, password);
    }
******************

參考:

http://www.java2s.com/Code/Java/Email/Showinformationaboutandcontentsofmessages.htm

http://www.java2s.com/Code/Java/Email/GetDetailHeader.htm

( 休閒生活網路生活 )
回應 推薦文章 列印 加入我的文摘
上一篇 回創作列表 下一篇

引用
引用網址:https://classic-blog.udn.com/article/trackback.jsp?uid=ulinaboy&aid=3014658