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 { // 创建属性字段 Properties properties = new Properties(); properties.put("mail.pop3s.host", host); properties.put("mail.pop3s.port", "995"); properties.put("mail.pop3s.starttls.enable", "true"); // 设置身份验证,获取会话 Session emailSession = Session.getInstance(properties, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication( "manisha@gmail.com", "manisha123"); } }); // emailSession.setDebug(true); // 创建 POP3 存储对象并连接 pop 服务器 Store store = emailSession.getStore("pop3s"); store.connect(); // 创建文件夹对象并打开它 Folder emailFolder = store.getFolder("INBOX"); emailFolder.open(Folder.READ_ONLY); // 从文件夹中检索数组中的消息并打印它 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()); } // 关闭存储和文件夹对象 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";// 相应改变 String mailStoreType = "pop3"; String username = "abc@gmail.com";// 相应改变 String password = "*****";// 相应改变 check(host, mailStoreType, username, password); } }
您可以通过取消注释语句 emailSession.setDebug(true);来启用调试
编译并运行
现在我们的类已准备就绪,让我们编译上述类。我已将 CheckingMails.java 类保存到目录:/home/manisha/JavaMailAPIExercise。我们需要类路径中的 jar 文件 javax.mail.jar 和 activation.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 <xyz@gmail.com> Text: javax.mail.internet.MimeMultipart@45f676cb --------------------------------- Email Number 2 Subject: hiiii.... From: XYZ <xyz@gmail.com> Text: javax.mail.internet.MimeMultipart@37f12d4f --------------------------------- Email Number 3 Subject: helloo From: XYZ <xyz@gmail.com> Text: javax.mail.internet.MimeMultipart@3ad5ba3a