JavaMail認證/驗證


在前面的章節查詢電子郵件和讀取電子郵件,我們通過授權憑證(使用者名和密碼)以及主機,連線到您的郵箱中儲存時。相反,我們可以組態屬性有主機,並告訴您的自定義的Authenticator範例的對談。這示於下面的例子:

建立Java類

我們將修改CheckingMails.java 請先閱讀 章查詢電子郵件 這章。其內容如下所述:

package com.yiibai;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;

public class CheckingMails {

   public static void check(String host, String storeType, String user,
      String password) 
   {
      try {

      // create properties field
      Properties properties = new Properties();

      properties.put("mail.pop3s.host", host);
      properties.put("mail.pop3s.port", "995");
      properties.put("mail.pop3s.starttls.enable", "true");

      // Setup authentication, get session
      Session emailSession = Session.getInstance(properties,
         new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
               return new PasswordAuthentication(
                  "[email protected]", "manisha123");
            }
         });
      // emailSession.setDebug(true);

      // create the POP3 store object and connect with the pop server
      Store store = emailSession.getStore("pop3s");

      store.connect();

      // create the folder object and open it
      Folder emailFolder = store.getFolder("INBOX");
      emailFolder.open(Folder.READ_ONLY);

      // retrieve the messages from the folder in an array and print it
      Message[] messages = emailFolder.getMessages();
      System.out.println("messages.length---" + messages.length);

      for (int i = 0, n = messages.length; i < n; i++) {
         Message message = messages[i];
         System.out.println("---------------------------------");
         System.out.println("Email Number " + (i + 1));
         System.out.println("Subject: " + message.getSubject());
         System.out.println("From: " + message.getFrom()[0]);
         System.out.println("Text: " + message.getContent().toString());
      }

      // close the store and folder objects
      emailFolder.close(false);
      store.close();

      } catch (NoSuchProviderException e) {
         e.printStackTrace();
      } catch (MessagingException e) {
         e.printStackTrace();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   public static void main(String[] args) {

      String host = "pop.gmail.com";// change accordingly
      String mailStoreType = "pop3";
      String username = "[email protected]";// change accordingly
      String password = "*****";// change accordingly

      check(host, mailStoreType, username, password);

   }

}
您可以通過取消註釋語句上設定偵錯emailSession.setDebug(true);

編譯並執行

現在,我們班是準備好了,讓我們編譯上面的類。我已經儲存了類CheckingMails.java目錄: /home/manisha/JavaMailAPIExercise. 我們需要 javax.mail.jar andactivation.jar 在classpath中。執行下面的命令從命令提示字元編譯類(兩個罐子被放置在 /home/manisha/ 目錄下):

javac -cp /home/manisha/activation.jar:/home/manisha/javax.mail.jar: CheckingMails.java

現在,這個類被編譯,執行下面的命令來執行:

java -cp /home/manisha/activation.jar:/home/manisha/javax.mail.jar: CheckingMails

驗證輸出

你可以看到一個類似的訊息,如下命令控制台上:

messages.length---3
---------------------------------
Email Number 1
Subject: Today is a nice day
From: XYZ <[email protected]>
Text: [email protected]
---------------------------------
Email Number 2
Subject: hiiii....
From: XYZ <[email protected]>
Text: [email protected]
---------------------------------
Email Number 3
Subject: helloo
From: XYZ <[email protected]>
Text: [email protected]