How to do a IMAP UID SEARCH with JavaMail? - java

I am using JavaMail 1.5.1 and i want to do a UID SEARCH. Is this supported or do i have to issue a low level IMAP command via ((IMAPFolder) folder).doCommand(...)?

((UIDFolder)folder).getMessageByUID(uid)

According with the docs, this is the way:
Folder f = store.getFolder("INBOX");
UIDFolder uf = (UIDFolder)f;
Message msg = uf.getMessageByUID(uid);
You can find more here: https://javamail.java.net/nonav/docs/api/javax/mail/UIDFolder.html

Related

Siteminder: Java Agent API

I want to create an agent object using siteminder r.12. Want to find the values for the below parameters. Where is the value stored?
User-defined connection parameters defined in your codeā€”for example:
AgentAPI agent = new AgentAPI();
ServerDef sd = new ServerDef();
sd.serverIpAddress = POLICY_IP;
sd.connectionMin = CX_MIN;
sd.connectionMax = CX_MAX;
sd.connectionStep = CX_STEP;
sd.timeout = CX_TIMEOUT;
sd.authorizationPort = AZ_PORT;
sd.authenticationPort = AUTH_PORT;
sd.accountingPort = ACC_PORT;
InitDef init=new InitDef(AGENT_LOGIN,SHARED_SECRET,false, sd);
agent.init(init);
Thanks,
navi
To use the AgentApi you will need to know some things about your Policy Server and agent. You will have to find the host address of the Policy Server, the agent name you will use to query the PS, the authorization port and authentication port and the shared secret.
Go here for the spec of the AgentApi:
https://support.ca.com/cadocs/0/CA%20SiteMinder%2012%2052-ENU/Bookshelf_Files/programming-reference/legacy-sm-java-sdk/netegrity/siteminder/javaagent/AgentAPI.html
the parameters you are looking for are related to Host Configuration Object (HCO) and SmHost.conf settings.

How to access outlook public folders using 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.

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