Javamail Api 简明教程
JavaMail API - Checking Emails
在继续本章之前,需要了解两个方面。它们是 Check 和 Fetch 。
There are two aspects to which needs to understood before proceeding with this chapter. They are Check and Fetch.
-
*Check*ing an email in JavaMail is a process where we open the respective folder in the mailbox and get each message. Here we only check the header of each message i.e the From, To, subject. Content is not read.
-
*Fetch*ing an email in JavaMail is a process where we open the respective folder in the mailbox and get each message. Alongwith the header we also read the content by recognizing the content-type.
要使用 JavaMail API 检查或获取电子邮件,我们需要 POP 或 IMAP 服务器。要检查和获取电子邮件,需要用到 Folder 和 Store 类。这里,我们使用了 GMAIL 的 POP3 服务器(pop.gmail.com)。在本章中,将学习如何使用 JavaMail API 检查电子邮件。获取将在后续章节中介绍。要检查电子邮件:
To check or fetch an email using JavaMail API, we would need POP or IMAP servers. To check and fetch the emails, Folder and Store classes are needed. Here we have used GMAIL’s POP3 server (pop.gmail.com). In this chapter will learn how to check emails using JavaMail API. Fetching shall be covered in the subsequent chapters. To check emails:
-
Get a Session
-
Create pop3 Store object and connect with pop server.
-
Create folder object. Open the appropriate folder in your mailbox.
-
Get your messages.
-
Close the Store and Folder objects.
Create Java Class
创建 java 类文件 CheckingMails ,其内容如下:
Create a java class file CheckingMails, the contents of which are as follows:
package com.tutorialspoint;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
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.pop3.host", host);
properties.put("mail.pop3.port", "995");
properties.put("mail.pop3.starttls.enable", "true");
Session emailSession = Session.getDefaultInstance(properties);
//create the POP3 store object and connect with the pop server
Store store = emailSession.getStore("pop3s");
store.connect(host, user, password);
//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 = "yourmail@gmail.com";// change accordingly
String password = "*****";// change accordingly
check(host, mailStoreType, username, password);
}
}
Compile and Run
现在我们的类就绪了,让我们编译上面的类。我已经将类 CheckingMails.java 存储到了目录: /home/manisha/JavaMailAPIExercise 。我们需要在类路径中使用 jar 文件 javax.mail.jar 和 activation.jar。从命令提示符执行以下命令以编译类(这两个 jar 都放置在 /home/manisha/ 目录中):
Now that our class is ready, let us compile the above class. I’ve saved the class CheckingMails.java to directory : /home/manisha/JavaMailAPIExercise. We would need the jars javax.mail.jar and activation.jar in the classpath. Execute the command below to compile the class (both the jars are placed in /home/manisha/ directory) from command prompt:
javac -cp /home/manisha/activation.jar:/home/manisha/javax.mail.jar: CheckingMails.java
现在类已编译,执行以下命令来运行:
Now that the class is compiled, execute the below command to run:
java -cp /home/manisha/activation.jar:/home/manisha/javax.mail.jar: CheckingMails
Verify Output
您应该在命令控制台上看到以下消息:
You should see the following message on the command console:
messages.length---4
---------------------------------
Email Number 1
Subject: Test Mail--Fetch
From: <abcd@gmail.com>
Text: javax.mail.internet.MimeMultipart@327a5b7f
---------------------------------
Email Number 2
Subject: testing ----checking simple email
From: <abcd@gmail.com>
Text: javax.mail.internet.MimeMultipart@7f0d08bc
---------------------------------
Email Number 3
Subject: Email with attachment
From: <abcd@gmail.com>
Text: javax.mail.internet.MimeMultipart@30b8afce
---------------------------------
Email Number 4
Subject: Email with Inline image
From: <abcd@gmail.com>
Text: javax.mail.internet.MimeMultipart@2d1e165f
这里,我们打印了收件箱中的邮件数,在本例中为 4。我们还打印了每封电子邮件的主题、发件人地址和文本。
Here we have printed the number of messages in the INBOX which is 4 in this case. We have also printed Subject, From address and Text for each email message.