如何用javamail阅读outlook的电子邮件?

我是java的初学者,每天我从附带文件的机器上收到许多电子邮件。 我必须打开everday outlook并检索附件,然后将其放在一个文件夹中。

如何使用javamail或java中的任何其他内容,它必须打开电子邮件,检索附加的xtt文件,然后将其存储在一个文件夹中。

有人可以帮助我完成这项任务或指导我到教程页面或样本。

非常感谢你

也许你可以使用像procmail这样的东西进行消息排序? 还要确保你使用的是imap而不是pop3 。 它将使您能够将电子邮件整理到服务器端的文件夹中。

你可以使用java邮件来做到这一点。 您需要找到配置详细信息,但标准代码段如下所示。 我复制了从这里剪断的代码。 官方javamail链接有一套相当不错的例子(即如何读取附件)。

要将电子邮件作为文件存储到文件夹,您可以使用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(""," ", ""); 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, "", ""); /* 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(); } }