We need to build a client for Hotmail, which doesn't support IMAP. To my understanding you have to use exchange w/ POP3 but POP3 doesn't support moving mail from one folder to another. We need the features:
be able to read mail without marking it as "read"
be able to delete mail
be able to move mail out of inbox to another folder and mark as read
Any way to get this to work?
Short answer, No.
License the ActiveSync protocol from Microsoft.
There is an Outlook connector for Hotmail. Maybe with a ton of JNI but check the license first.
Update: Outlook now supports IMAP. Hotmail uses the same servers.
You can do everything you need with JavaMail. Here is the API
Here is an example of getting all unread messages from an inbox and marking them as read. Take a look at the folder class (specifically the copyMessages() method) to move messages to a new folder.
import java.util.Properties;
import javax.mail.*;
import javax.mail.search.FlagTerm;
public class Driver {
public static void main(String[] args){
// Create properties (disable security checks on server)
Properties props = new Properties();
// Get session
Session session = Session.getDefaultInstance(props, null);
try{
// Get the store
Store store = session.getStore("pop3");
store.connect("servername", "username", "password");
//connection configuration
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
//get all unread messages in the inbox
FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), true);
Message[] messages = folder.search(ft);
for (int i = messages.length -1; i>=0; i--) {
messages[i].setFlag(Flags.Flag.SEEN, true);
}
// Close connection
folder.close(false);
store.close();
}
catch(Exception e){
e.printStackTrace();
}
}
Related
I have been trying lot of things from multiple resources like Read Inbox Java2s
and How to get the list of available folders in a mail account using JavaMail
I am sending emails succesfully, but to be sure of mail is sent successfully, i need to read emails from sent items folder Is this possible with smtp? if yes, how?
Currently I am stuck even in connceting with stroe. I am finding no way to pass the steps
Store store = session.getStore(); and store.connect();
I have no idea about imap or pop3. They might not have been even configured at our server, but if smtp does not faicilitate then I am ready to process with those protocols, though I am sending mails using stmp yet. I have tried lot of edits in my following code, but nothing is helping
String host = "mysite.smtp.com";
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "myport");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
String user = "myname";
String password = "mypassword";
return new PasswordAuthentication(user, password);
}
});
Store store = session.getStore(); // had tried writing "imaps" here
store.connect(host, null, null);
//store.connect(); also tried this
Folder inbox = store.getFolder("INBOX"); //actually i need "SENT"
if (inbox == null) {
System.out.println("No INBOX");
System.exit(1);
}
inbox.open(Folder.READ_ONLY);
Message[] messages = inbox.getMessages();
for (int i = 0; i < messages.length; i++) {
System.out.println("Message " + (i + 1));
messages[i].writeTo(System.out);
}
inbox.close(false);
store.close();
You can't use smtp to read email, you need to use imap. pop3 won't help because it only lets you read the Inbox.
Depending on what you mean by "sent successfully", a successful return from the send method will tell you what you want to know.
Note also that depending on the mail server you're using, sent messages don't automatically appear in a "Sent Messages" folder; you might need to copy them their yourself after sending the message. (Gmail does this automatically, but not all other servers do.)
If what you really want to know is that the message was successfully delivered to the destination mail server(s) and all the addresses were valid, that's harder. The JavaMail FAQ has more information.
I know that I am answering late. I encountered quite similar problems while maintaining a software using extensively mail sending functionalities. I finally created a JUnit extension to write integration tests with a SMTP server emulation.
Please have a look to github.com/sleroy/fakesmtp-junit-runner/. By the way it's open-source.
Refer this link:
How to save sent items mail using the Java mail API?
Sent Mail Service using JAVAMAIL API
Note also that depending on the mail server you're using, sent messages don't automatically appear in a "Sent Messages" folder; you might need to copy them their yourself after sending the message. (Gmail does this automatically, but not all other servers do.)
When I receive mails, open the inbox, and get the messages; how can I compare them to a previous array of messages, so that I know which of them I have already read?
Properties properties = new Properties();
properties.put("mail.imaps.host", host);
properties.put("mail.imaps.port", "993");
Session emailSession = Session.getDefaultInstance(properties);
//2) create the POP3 store object and connect with the pop server
Store emailStore = emailSession.getStore("imaps");
emailStore.connect(user,pw);
//3) create the folder object and open it
Folder emailFolder = emailStore.getFolder("INBOX");
emailFolder.open(Folder.READ_ONLY);
Message[] messages = emailFolder.getMessages();
How to handle this depends on exactly what your requirements are. For example, will other applications and/or users be accessing the mailbox at the same time as your application? Or does your application have dedicated access to the mailbox?
You can use the SEEN flag to find messages that you haven't already read.
You can also save the IMAP UID for the last message you processed (and the UIDVALIDITY for the folder) to later find messages with larger UIDs. See the UIDFolder interface for details.
(The comments in your code talk about pop, but your code is using imap; if you ever have to use the pop3 protocol the answers are quite different.)
I'm trying to write an app that get emails from a POP3 server. The "Strategy" I used eventually is:
Get single message
check if the message is already present in my database
if not, insert it, goto 1
I know that this is not perfect, but ok...let's just face a problem at once :)
apologizing for the ugly style, I'm well trained as Assembly and pure C programmer, just started with Java, my code to get the single message is (I have cut the try/catch statements to make it more readable)
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
Store store = null;
POP3Folder folder = null;
openStaffLabel: {
store = session.getStore(exXContainer.accounts[selectedAccount].accountType.toLowerCase());
store.connect(host, username, password);
folder =(POP3Folder) store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
int countMsg = 0;
countMsg = folder.getMessageCount();
if (countMsg == 0)
break openStaffLabel;
message = folder.getMessages(countMsg - first + 1);
for(int i = 0;i < messages.length;i++)
{
// do some stuffs with messages
}
} // openStaffLabel:
folder.close(false);
store.close();
That seems to work fine. Then I found over internet this code (here), claimed by the poster to be much more efficient:
URLName url = new URLName("pop3", host, 110, "", user, password);
Session session = Session.getDefaultInstance(System.getProperties(),null);
Store store = session.getStore(url);
store.connect();
POP3Folder inbox = (POP3Folder)store.getFolder("INBOX");
inbox.open(Folder.READ_WRITE);
FetchProfile profile = new FetchProfile();
profile.add(UIDFolder.FetchProfileItem.UID);
Message[] messages = inbox.getMessages();
inbox.fetch(messages,profile);
for(int i = 0;i < messages.length;i++)
{
// do some stuffs with messages
}
The author claims: "The major difference with the previous scenario is that the client downloads new emails only"
but in his code there is a inbox.getMessages() that, if I understood well, actually download all messages. So the point is...what is the real advantage of using fetch(messages,profile) if with getMessages I have already fetched all the headers?
if I only want to download UIDs, how can I do?
and, furthermore, if I only want to download the UIDs of the first N mails...is it possible?
thank you very much to anyone who will help, I'm stuck on this problem since one week.
Cristiano
POP3 isn't the best protocol for this, and you'll understand this better if you understand what the POP3 protocol is capable of doing.
The use of the fetch method that you describe will fetch all the UIDs for all the messages in the mailbox in one operation. If you keep track of which UIDs you've seen, you can then make sure you only fetch the content of the messages that you haven't seen. Use the POP3Folder.getUID method in the loop over all the messages to skip the messages you've already seen. Note that the getMessage methods don't actually fetch the content of the message; the content isn't fetched until you use another method that accesses the content.
Note that the POP3 protocol only lets you fetch the UID for a single message, or the UIDs for all the messages.
Be sure to read the javadocs for the com.sun.mail.pop3 package, and the JavaMail FAQ, especially this item about common mistakes.
Hi i am having problem with reading mails from manually created folder.
i can read mails from INBOX. but when i try to read mail from other than inbox it is giving error.
I hope stackoverflow will give solution.
Thanks in advance...
Error Message:
Exception in thread "main" javax.mail.FolderNotFoundException: folder is not INBOX
at com.sun.mail.pop3.POP3Folder.open(POP3Folder.java:183)
at MailPop3.main(MailPop3.java:24)
My Code:
Properties properties = System.getProperties();
Session session = Session.getDefaultInstance(properties);
Store store = session.getStore("pop3");
store.connect(host, user, password);
Folder inbox = store.getFolder("MyPersonalFolder");
inbox.open(Folder.READ_ONLY);
// search for all "unseen" messages
Flags seen = new Flags(Flags.Flag.SEEN);
FlagTerm unseenFlagTerm = new FlagTerm(seen, false);
Message messages[] = inbox.search(unseenFlagTerm);
Hope you are using POP. By default POP points to INBOX only. As per POP3Folder,
A POP3 Folder (can only be "INBOX").
See the com.sun.mail.pop3 package documentation for further information on the POP3 protocol provider.
To access custom folders you require to use IMAPFolder.
This may sound weird, but I guess if you want to make your custome folder and use it in the code then you need to name it "Store", then it will work. Worked for me....took 3 hrs to research...hope works for you too
I am using JavaMail API to connect to my personal account. I have list of folders (labels) in my Gmail account which I created + the default folders like Inbox, Drafts etc. How can I list all the available folders (the default and the user created)?
I can access the particular folder using this API: Folder inbox = store.getFolder("Inbox");. Is there any other API to get the list of folders available in a mail account?
Sergey is close, but by default JavaMail's list() does a LIST "" %, which gives you only top-level folders. GMail puts its system folders (All Mail, Drafts, Sent Mail, Spam, Starred, and Trash) under the non-selectable folder [Gmail], so you really need to do a LIST "" * instead. Otherwise, you'll just get back INBOX, [Gmail], and your labels.
Here's some sample code that connects to GMail, fetches the folder list, and prints out the name and message count for each non-\NoSelect folder (i.e. the ones that aren't just hierarchy placeholders, like [Gmail]):
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
try {
Session session = Session.getDefaultInstance(props, null);
javax.mail.Store store = session.getStore("imaps");
store.connect("imap.gmail.com", "<username>#gmail.com", "<password>");
javax.mail.Folder[] folders = store.getDefaultFolder().list("*");
for (javax.mail.Folder folder : folders) {
if ((folder.getType() & javax.mail.Folder.HOLDS_MESSAGES) != 0) {
System.out.println(folder.getFullName() + ": " + folder.getMessageCount());
}
}
} catch (MessagingException e) {
e.printStackTrace();
}
Here is the code that works. This will give you handle to all the Labels. To go deeper in a folder, you may perform folder.list() or you can use store.getDefaultFolder().list("*") to retrieve all the folders and sub-folders as suggested in the other answer.
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com", "YOURMAILID#gmail.com", "UR_P#ZZWRD");
System.out.println(store);
Folder[] f = store.getDefaultFolder().list();
for(Folder fd:f)
System.out.println(">> "+fd.getName());
Output:
>> INBOX
>> Personal
>> Receipts
>> Travel
>> Work
>> [Gmail]
OLD ANSWER
Please note this is not correct, it's rightly pointed in this answer by dkarp
These should do:
http://java.sun.com/products/javamail/javadocs/javax/mail/Store.html#getSharedNamespaces%28%29
http://java.sun.com/products/javamail/javadocs/javax/mail/Store.html#getUserNamespaces%28java.lang.String%29
You can access other folders like this
store.getFolder("[Gmail]/Sent Mail");
store.getFolder("[Gmail]/Drafts");
etc.
How about store.getDefaultFolder().list()? Just a guess, though.
You can try this:
Folder[] folderList = store.getDefaultFolder().list("*");
You can right-click on different buttons on the side panel of your mail box and inspect the names for different folders that are used in different mails.
This works for every email client. The list command works but I felt this makes the process quicker.