Javamail-api-authentication

提供:Dev Guides
移動先:案内検索

JavaMail API-認証

前の章のlink:/javamail_api/javamail_api_checking_emails [Checking Emails]およびlink:/javamail_api/javamail_api_fetching_emails [Fetching Emails]では、メールボックスのストアに接続するときに、ホストとともに認証情報(ユーザー広告パスワード)を渡しました。 代わりに、ホストを持つように_Properties_を設定し、カスタムAuthenticatorインスタンスについてセッションに伝えることができます。 これを以下の例に示します。

Javaクラスを作成する

次の章のリンクからCheckingMails.javaを変更します:/javamail_api/javamail_api_checking_emails [Checking Emails]。 その内容は以下のとおりです。

package com.finddevguides;

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_および_activation.jar_のjarが必要です。 以下のコマンドを実行して、コマンドプロンプトからクラス(両方のjarが/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: javax.mail.internet.MimeMultipart@45f676cb
---------------------------------
Email Number 2
Subject: hiiii....
From: XYZ <[email protected]>
Text: javax.mail.internet.MimeMultipart@37f12d4f
---------------------------------
Email Number 3
Subject: helloo
From: XYZ <[email protected]>
Text: javax.mail.internet.MimeMultipart@3ad5ba3a