JavaMail:阅读 Ubuntu 上的电子邮件
JavaMail: Read Email on Ubuntu
我的 Ubuntu 邮箱中的电子邮件进入了 /var/mail 这样的目录。其中有一个名为 "root" 的文件,其中包含所有收到的电子邮件。我使用 "mail" 命令以更加用户友好的方式访问这些电子邮件。有什么方法可以配置 JavaMail 来阅读这些电子邮件,而不必像解析文本文件一样解析 "root" 电子邮件文件?
下面来自 http://www.tutorialspoint.com/javamail_api/javamail_api_checking_emails.htm 的代码从 POP3 帐户读取电子邮件。
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);
}
}
JavaMail 可以使用 "mbox" provider.
为您解析 Unix 邮件文件
我的 Ubuntu 邮箱中的电子邮件进入了 /var/mail 这样的目录。其中有一个名为 "root" 的文件,其中包含所有收到的电子邮件。我使用 "mail" 命令以更加用户友好的方式访问这些电子邮件。有什么方法可以配置 JavaMail 来阅读这些电子邮件,而不必像解析文本文件一样解析 "root" 电子邮件文件?
下面来自 http://www.tutorialspoint.com/javamail_api/javamail_api_checking_emails.htm 的代码从 POP3 帐户读取电子邮件。
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);
}
}
JavaMail 可以使用 "mbox" provider.
为您解析 Unix 邮件文件