如何用javamail阅读Outlook的电子邮件?
我是java的初学者,每天都会收到来自机器附带文件的许多电子邮件。 我必须打开每天的展望和检索附加的文件,然后把它放在一个文件夹中。如何用javamail阅读Outlook的电子邮件?
如何用javamail或java中的其他东西做到这一点,它必须打开电子邮件,检索附加的xtt文件,然后将其存储在一个文件夹中。
有人可以帮我做这个任务或指导我进入教程页面或样本。
非常感谢您
也许你可以像procmail执行排序消息使用的东西?另外请确保您使用的是imap
而不是pop3
。它将使您能够将电子邮件组织到服务器端的文件夹中。
好的我怎么知道它是imap还是pop3在outlook? – user618111 2011-03-12 21:58:10
你可以使用java mail来做到这一点。你需要找到配置细节,但是这个标准的代码片段就像下面这样。我复制了从here截取的代码。官方javamail link有一套相当不错的例子(即如何阅读附件)。
用于将电子邮件作为文件存储到文件夹,您可以apache FileUtils。将电子邮件写入文件并将其复制到您需要的文件夹中。
HTH!
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("<impap_address>","<mail ID> ", "<Password>");
inbox = store.getFolder("Inbox");
System.out.println("No of Unread Messages : " + inbox.getUnreadMessageCount());
inbox.open(Folder.READ_ONLY);
/* Get the messages which is unread in the Inbox*/
Message messages[] = inbox.search(new FlagTerm(new Flags(Flag.SEEN), false));
我已经用这种方法解决了这个无穷无尽的问题。
注意:
- 我使用的IMAP协议
- 我只用连接和这个类来看看我收到
的电子邮件,我希望能与这些设置属性的可以帮助很多与此有关的人阅读,撰写,无论是电子邮件,还是Outlook。
public class OutlookReader_imap {
Folder inbox;
// Constructor of the calss.
public OutlookReader_imap() {
System.out.println("Inside MailReader()...");
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
/* Set the mail properties */
/*
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(props);
MimeMessage msg = new MimeMessage(session);
// set the message content here
Transport.send(msg, username, password);
*/
Properties props = System.getProperties();
// Set manual Properties
props.setProperty("mail.imaps.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.imaps.socketFactory.fallback", "false");
props.setProperty("mail.imaps.port", "993");
props.setProperty("mail.imaps.socketFactory.port", "993");
props.put("mail.imaps.host", "imap-mail.outlook.com");
try {
/* Create the session and get the store for read the mail. */
Session session = Session.getDefaultInstance(System.getProperties(), null);
Store store = session.getStore("imaps");
store.connect("imap-mail.outlook.com", 993, "<email>", "<password>");
/* Mention the folder name which you want to read. */
// inbox = store.getDefaultFolder();
// inbox = inbox.getFolder("INBOX");
inbox = store.getFolder("INBOX");
/* Open the inbox using store. */
inbox.open(Folder.READ_ONLY);
Message messages[] = inbox.search(new FlagTerm(new Flags(
Flags.Flag.SEEN), false));
System.out.println("No. of Unread Messages : " + inbox.getUnreadMessageCount());
/* Use a suitable FetchProfile */
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
inbox.fetch(messages, fp);
try {
printAllMessages(messages);
inbox.close(true);
store.close();
} catch (Exception ex) {
System.out.println("Exception arise at the time of read mail");
ex.printStackTrace();
}
} catch (MessagingException e) {
System.out.println("Exception while connecting to server: " + e.getLocalizedMessage());
e.printStackTrace();
System.exit(2);
}
}
public void printAllMessages(Message[] msgs) throws Exception {
for (int i = 0; i < msgs.length; i++) {
System.out.println("MESSAGE #" + (i + 1) + ":");
printEnvelope(msgs[i]);
}
}
public void printEnvelope(Message message) throws Exception {
Address[] a;
// FROM
if ((a = message.getFrom()) != null) {
for (int j = 0; j < a.length; j++) {
System.out.println("De : " + a[j].toString());
}
}
String subject = message.getSubject();
Date receivedDate = message.getReceivedDate();
Date sentDate = message.getSentDate(); // receivedDate is returning
// null. So used getSentDate()
//Dar Formato a la fecha
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
System.out.println("Asunto : " + subject);
if (receivedDate != null) {
System.out.println("Recibido: " + df.format(receivedDate));
}
System.out.println("Enviado : " + df.format(sentDate));
}
public static void main(String args[]) {
new OutlookReader_imap();
}
}
这可能是生产家伙的实时问题! – Jayy 2012-03-02 13:07:53