How to read Outlook email with javamail?

I am new to java and every day I get a lot of emails from a machine with attached files. I have to open the perpetual forecast and get the attached file, and then put it in a folder.

How to do this using javamail or something else in java, it should open the letter, extract the attached xtt file and then save it in a folder.

Can someone help me complete this task or direct me to a page or sample textbook.

Many thanks

+6
java outlook
source share
3 answers

Perhaps you could sort the messages using procmail ? Also make sure you use imap instead of pop3 . This will give you the ability to organize your emails into folders on the server side.

0
source share

You can do this using java mail . You will need to find the configuration details, but the standard code snippet for this will look something like this. I copied the code cut off from here . Thos official javamail link has a pretty decent set of examples (like how to read attachments).

To save the email as a file to a folder, you can apache FileUtils . Write a letter to the file and copy it to the desired folder.

NTN!

  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)); 
+9
source share

I solved this endless problem this way.

Note:

  • I am using IMAP
  • I use only connection and this class to see received letters.

I hope that with these property sets this will help many who have struggled with this read, write, regardless of email, but with 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(); } } 
0
source share

All Articles