JavaMail API - 身份验证



在前面的章节中 检查电子邮件提取电子邮件,我们在连接到邮箱存储时,将授权凭证(用户广告密码)与主机一起传递。而我们可以配置“属性”来拥有主机,并通过自定义身份验证器实例告诉会话。如下面的示例中所示

创建 Java 类

我们将修改我们从章节 检查电子邮件 中获得的 CheckingMails.java。其内容如下

package com.tutorialspoint;

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 中。我将需要类路径中的 jar 文件 javax.mail.jaractivation.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
广告