身份验证(Authentication)
优质
小牛编辑
137浏览
2023-12-01
在前面的“ 检查电子邮件和获取电子邮件”章节中,我们在连接到您的邮箱存储时传递了授权凭据(用户广告密码)和主机。 相反,我们可以将Properties配置为拥有主机,并告诉Session您的自定义Authenticator实例。 这显示在下面的示例中:
创建Java类 (Create Java Class)
我们将从“ 检查电子邮件 ”一章修改我们的CheckingMails.java。 其内容如下:
package cn.xnip;
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(
"manisha@gmail.com", "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 = "abc@gmail.com";// change accordingly
String password = "*****";// change accordingly
check(host, mailStoreType, username, password);
}
}
您可以通过取消注释语句emailSession.setDebug(true);来设置调试emailSession.setDebug(true);
编译和运行 (Compile and Run)
现在我们的课已经准备好了,让我们编译上面的类。 我已将类CheckingMails.java保存到目录: /home/manisha/JavaMailAPIExercise 。 我们需要在类路径中使用jars 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
验证输出 (Verify Output)
您可以在命令控制台上看到类似的消息:
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