How to access outlook public folders using Java? - java

Using Java I want to access certain Outlook public folders. I tried below code
Properties props = System.getProperties();
Session session = Session.getDefaultInstance(props);
session.setDebug(true);
Store store = session.getStore("imap");
store.connect("imap4.<something>.com", "<my user id>", "<my password>");
Folder folder = store.getFolder("Public Folders/");
folder.open(Folder.READ_ONLY);
Message[] messages = folder.getMessages();
if(messages.length == 0){
System.out.println("no message");
}
for(Message message : messages){
System.out.println(message.getSubject());
}
I have tried different combinations for "Public Folders". Every time I get:
Exception in thread "main" javax.mail.FolderNotFoundException: Public Folders/ not found
at com.sun.mail.imap.IMAPFolder.checkExists(IMAPFolder.java:302)
at com.sun.mail.imap.IMAPFolder.open(IMAPFolder.java:885)
at MailReader.main(MailReader.java:23)
Please let me know if there is any way to access Outlook public folders.

The way I used in one of my projects was EWS Java API. Here's a link to some tutorial: http://blogs.msdn.com/b/exchangedev/archive/2013/01/03/ews-java-api-1-2-get-started.aspx It's not the easiest thing I've ever done though.

Looks like Microsoft removed the ability to access public folders through the IMAP protocol in Exchange 2007 and they have no plans to restore it.

Related

Java/JavaMail: Null pointer exception when trying to create a folder to fetch E-Mails (GMAIL/POP3)

Everything works, including authentication, but when I get to the step of creating a folder the program crashes.
I've tried switching to SMTP, didn't work, not even sure what SMTP is,
I've Tried a different Gmail account,
I've tried removing the line properties.put("mail.pop3.starttls.enable", "true"), and
I've tried removing the 3's from pop3 and pop3s.
private static void createProperties() {
// Create properties field.
Properties properties = new Properties();
properties.put("mail.pop3.host", host);
properties.put("mail.store.protocol", "pop3s");
properties.put("mail.pop3.port", "995");
//properties.put("mail.pop3.starttls.enable", "true");
emailSession = Session.getDefaultInstance(properties, null);
}
private static void createStore() throws MessagingException {
// Create the POP3 store object and connect with the POP server.
Store store = emailSession.getStore("pop3s");
store.connect(host, user, password);
}
private static void createFolder() throws MessagingException {
// Create the folder object and open it.
Folder emailFolder = store.getFolder("INBOX"); // Error here
emailFolder.open(Folder.READ_ONLY);
}
I expected to get some nicely formatted email messages.
I got the following errors:
DEBUG POP3: server doesn't support TOP, disabling it
Exception in thread "main" java.lang.NullPointerException
at GetMail.createFolder(GetMail.java:60)
at GetMail.main(GetMail.java:33)```
The POP3 protocol only supports one folder - Inbox. Use IMAP instead.

Unable to delete messages using JavaMail

I am using JavaMail to access an Exchange mailbox (private to the company I work for). My applicable code is as follows:
Store store = Session.getDefaultInstance(props, null).getStore("imap");
store.connect(...stuff...);
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_WRITE);
int numOfMessages = inbox.getMessageCount();
for (int i = 1; i<=numOfMessages; i++){
Message message = inbox.getMessage(i);
message.setFlag(Flags.Flag.DELETED, true);
System.out.println(message.getSubject());
}
inbox.close(true);
store.close();
It is accessing and printing out all of the message names properly. However, with each run through, it is printing the same names over and over, indicating that they weren't actually deleting.
Resolution: I found that I was throwing an error before the inbox.close(true) (in code I deemed inapplicable). I'm not marking it as an answer, because this isn't a real answer.
Try to call the saveChanges method on your Message object. Javadoc here.

How to get all unread emails in JAVA from lotus notes domino server

I am new to Notes JAVA API and developing a utility where I require to read all unread mails from a Lotus notes id.Now when i try to use lotus.domino.Database.getAllUnreadDocuments() it gives me the following exception
NotesException: Not implemented
at lotus.domino.cso.Base.notImplemented(Unknown Source)
at lotus.domino.cso.Document.markRead(Unknown Source)
at com.email.ReadEmailRemotely.readEmails(ReadEmailRemotely.java:428)
at com.email.ReadEmailRemotely.run(ReadEmailRemotely.java:96)
at java.lang.Thread.run(Unknown Source)
My application is a plain JAVA application in eclipse using NCSO.jar
My question is , do i need to extend lotus.domino.AgentBase ?
If yes then what all dependencies do i require as , JAVA app is not allowing to extend it.
& if no then is there any other way to get all unread mails?
An easy way (assuming you are can edit the NSF) is to create a hidden view which lists only the documents you want to get back.
Then access that view and iterate through it.
If the server supports IMAP or POP3, you can use JavaMail API, which is pretty easy and has a flag for unread messages.
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
try {
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("myserver.com", "user", "pass");
Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_ONLY);
FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
Message messages[] = inbox.search(ft);
}
You will have to switch to using notes.jar instead of ncso.jar.
In order to use notes.jar and access the getAllUnreadDocuments method, you will need to install Notes and Domino 8 or above on the system where your code is running.
May require Secure Connection(SSL), Use the following properties to connect mail server supporting POP3 protocol:
properties.put("mail.pop3.socketFactory.port", "POP3_PORT");
properties.put("mail.pop3.host", "POP3_SERVER_HOST_NAME_OR_IP");
properties.put("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.put("mail.pop3.socketFactory.fallback", "false");

How to work with mstor to read mbox mail messages

Can anybody tell me how to use mstor to read mbox mail messages on windows
Thanks in advance...
An example url for accessing an mstor mailbox might be:
mstor:c:/mail on a Microsoft Windows machine
Reading messages from a local store:
Session session = Session.getDefaultInstance(new Properties());
Store store = session.getStore(new URLName("mstor:c:/mailbox/MyStore"));
store.connect();
// read messages from Inbox..
Folder inbox = store.getDefaultFolder().getFolder("Inbox");
inbox.open(Folder.READ_ONLY);
Message[] messages = inbox.getMessages();
//Remember to add the properties in above code.
this.properties = new Properties();
this.properties.setProperty("mail.store.protocol", "mstor");
this.properties.setProperty("mstor.mbox.metadataStrategy", "none");
this.properties.setProperty("mstor.mbox.cacheBuffers", "disabled");
this.properties.setProperty("mstor.mbox.bufferStrategy", "mapped");
this.properties.setProperty("mstor.metadata", "disabled");
this.properties.setProperty("mstor.mozillaCompatibility", "enbled")

How to write java program to read new emails from any emailid

Hi I want to write a java program where I will provide my email id and password. and I want to read all new unread messages that arrived to that email id. I donot know how to write program for that.
The below program works fine for gmail. but it does not work for yahoomail because for yahoo pop3 is not configured. I want a generic code which will work for all email id.
import java.io.*;
import java.util.*;
import javax.mail.*;
public class ReadMail {
public static void main(String args[]) throws Exception {
// String host = "pop.gmail.com";
// String user = "xyz";
// String password = "12345";
// Get system properties
Properties properties = System.getProperties();
// Get the default Session object.
Session session = Session.getDefaultInstance(properties, null);
// Get a Store object that implements the specified protocol.
Store store = session.getStore("pop3s");
//Connect to the current host using the specified username and password.
store.connect(host, user, password);
//Create a Folder object corresponding to the given name.
Folder folder = store.getFolder("inbox");
// Open the Folder.
folder.open(Folder.READ_ONLY);
Message[] message = folder.getMessages();
// Display message.
for (int i = 0; i < message.length; i++) {
System.out.println("------------ Message " + (i + 1) + " ------------");
System.out.println("SentDate : " + message[i].getSentDate());
System.out.println("From : " + message[i].getFrom()[0]);
System.out.println("Subject : " + message[i].getSubject());
System.out.print("Message : ");
InputStream stream = message[i].getInputStream();
while (stream.available() != 0) {
System.out.print((char) stream.read());
}
System.out.println();
}
folder.close(true);
store.close();
}
}
You need to know more than just login-pass. Things like mail server address, mail server type, port for connections, etc.
You should probably check out Java Mail API, or Commons Email.
UPD:
You create a Session using Session.getDefaultInstance() method (which takes connection Properties object and authenticator), get a Store from this Session using Session.getStore() method, get a Folder from that store using Store.getFolder("FOLDER_NAME") method, open that Folder, using Folder.open(Folder.READ) method, and get all messages, using something like Message[] messages = inboxFolder.getMessages();
Is that what you were looking for?
UPD2:
There is simply no way to write a generic program, which will work with any mail provider, using just server path, userID and password. Because different mail servers are configured differently. They talk differen protocols (imap/pop3/pop3 ssl) on different ports. There's always some guy, who has configured his mail server to talk imap over ssl on 31337 port only, all the other ports and protocols are banned. And this guy breaks your program. So, you'll have to specify all this properties in your properties object. Look here for properties, you'll have to specify.
UPD3:
On second thought, you actually have one option. Just try connecting to the server using different protocols. If that does not help, start iterating through ports. The one that fits is your configuration. If that's really what you want.
You need the javax.mail package, and the documentation of it. Read the documentation. Then you know.
There are two ways to do it:
1) Google provides API's to access mail you could use that library which provides more control over your mails. See here: http://code.google.com/apis/gmail/. In the same way try for other email providers.
2) Simple mail client(you could find it easily googling), but you need to look at headers to identify which mails are read/unread etc.
You need a registry where you can get the properties for a given mail service.
For instance, instead of specifying a pop3 host, you could specify the name of a .properties file that would contain the host, the port, the protocol, etc...
If your .properties file contains the protocol, for instance mail.store.protocol=pop3, you could use session.getStore() (with no argument), and the same code could be used for pop3, imap, pop3s, imaps.

Categories

Resources