How to read mbox email messages using mstor - java

I am using mstor to read the mbox email messages, but i am not able to connect to the store using the urlName name which i m passing, by default its connecting to other
location on my macbine.Do i need to create the store using mstor JCR before proceed to connect to the store?
Session session = Session.getDefaultInstance(new Properties());
Store store = session.getStore(new URLName("mstor:C:/mailbox/MyStore/Inbox"));
store.connect();
Folder inbox = store.getDefaultFolder().getFolder("inbox");
inbox.open(Folder.READ_ONLY);
Message m = inbox.getMessage(0);
Any suggetions are helpful
Thanks in advance..

//Set the Properties as shown below:
Properties 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.cache.disabled", "true");
this.properties.setProperty("mstor.mbox.bufferStrategy", "mapped");
this.properties.setProperty("mstor.metadata", "disabled");
//Also mstor count for messages start from 1 and not 0. so change it to 1.
Store store = session.getStore(new URLName("mstor:C:/mailbox/MyStore/Inbox"));
store.connect();
Folder inbox = store.getDefaultFolder().getFolder("inbox");
inbox.open(Folder.READ_ONLY);
Message m = inbox.getMessage(1);

Related

Javax Mail fetching is very slow

final Session session = Session.getDefaultInstance(new Properties());
try (Store store = session.getStore("imaps"))
{
store.connect("smtp.gmail.com", "email#gmail.com", "password");
try (Folder inbox = store.getFolder("Inbox"))
{
inbox.open(Folder.READ_WRITE);
for (final Message message : inbox.getMessages())
{
final Multipart multipart = (Multipart) message.getContent();
System.out.println(multipart.getBodyPart(0).getContent());
}
}
}
The fetching of each mail takes around 0.5 second.
So if i do a loop over all my mail and let's say i have around 100 it can take up to minutes. Is there something wrong with my code or the javaxmail works like that? I realise it open a connection per mail but this seem wrong. It should fetch all data once and then cache them.

How to retrieve only 40-50 email like Gmail using Javamail?

I am trying to retrieve my inbox using javamail and I have done that but now problem arises when I try to load the code it take lot of time in reading all emails as it read the whole inbox but I want that only few emails should be fetched that are most recent.
Properties properties = new Properties();
String host = "pop.gmail.com";// change accordingly
properties.put("mail.pop3.host", host);
properties.put("mail.pop3.port", "995");
properties.put("mail.pop3.starttls.enable", "true");
Session emailSession = Session.getDefaultInstance(properties);
//create the POP3 store object and connect with the pop server
Store store = emailSession.getStore("pop3s");
store.connect(host, username, password);
//create the folder object and open it
Folder emailFolder = store.getFolder("INBOX");
emailFolder.open(Folder.READ_ONLY);
// retrieve the messages from the folder in an array and print it
Message[] messages = emailFolder.getMessages();
LOG.info("messages.length---" + messages.length);
for (int i = 0; i < messages.length; i++) {
Message message = messages[i];
leadJSONObject = new JSONObject();
LOG.info("Email Number " + (i + 1));
LOG.info("This log is executed successfully");
leadJSONObject.put("emailFrom", message.getFrom()[0].toString());
leadJSONObject.put("emailSubject", message.getSubject());
leadJSONObject.put("emailText", message.getContent().toString());
planJSONArray.put(leadJSONObject);
}
//close the store and folder objects
emailFolder.close(false);
store.close();
Please Help how can I read only few emails. Thanks in Advance.
You may want to count the messages, then retrieve only a part of the total , e.g for the 50 first messages (or less if there isn't enough):
int total = emailFolder.getMessageCount();
Message[] messages = emailFolder.getMessages(1,Math.min(total,50));
cf. the javadoc of javax.mail.Folder.

JavaMail - Issue with Multiple And Search Terms

I would like to fetch recent, unread emails with a specific subject in a particular folder from my gmail account. I am using JavaMail API as below but it returns 0 results. However if I just use subjectTerm alone, I see results. Please let me know where am I going wrong. Thank you.
Please note that I used messages[0] below instead of looping through messages array for code simplicity to paste it here.
public void openMailBox(String hostname, String username, String password, String folderName, String subject) throws MessagingException, GeneralSecurityException, IOException{
props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
props.setProperty("mail.imaps.host", "imap.gmail.com");
props.setProperty("mail.imaps.port", "993");
props.setProperty("mail.imaps.ssl.enable", "true");
props.put("mail.imaps.ssl.socketFactory", new MailSSLSocketFactory());
session = Session.getInstance(props);
store = session.getStore();
store.connect(username, password);
folder = store.getFolder(folderName);
folder.open(Folder.READ_ONLY);
messages = folder.search(getSearchTerm(subject));
if (messages[0].isMimeType("multipart/*")){
Multipart multipart = (Multipart) messages[0].getContent();
for(int i=0;i<multipart.getCount();i++) {
BodyPart bodyPart = multipart.getBodyPart(0);
if (bodyPart.isMimeType("text/*")) {
msg = msg+bodyPart.getContent().toString();
}
}
}else{
msg = messages[0].getContent().toString();
}
System.out.println(msg);
folder.close(true);
store.close();
}
public SearchTerm getSearchTerm(String subject){
subjectTerm = new SubjectTerm(subject);
unseenFlagTerm = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
recentFlagTerm; = new FlagTerm(new Flags(Flags.Flag.RECENT), true);
return new AndTerm(subjectTerm, new AndTerm(unseenFlagTerm, recentFlagTerm));
}
}
What mail server are you using?
Some mail servers don't implement the RECENT flag in any useful way, so messages might not be marked RECENT. Try leaving out the RECENT term and see if you get more results.
If that doesn't help, add code to dump out the flags for all messages and then post the JavaMail debug output that shows the flags for all messages along with the search request and response.
Note also that some IMAP servers don't fully or correctly implement the SEARCH command and so can't handle the kind of search you're doing.
Finally, note that you don't need to set the socketFactory property unless you're using MailSSLSocketFactory in a more interesting way than you've show in your example code above.

Restore emails from local file system to exchange account in ews api

I am trying to restore emails from local file system to any exchange account in java, I have tried with copyItems(ItemId itemId, Destination folderId) and moveItems(ItemId itemId, Destination folderId) methods of EWS(Exchange api) but its throws an exception as "ErrorInvalidIdMalformed", so can you please help me to resolve this..
for (Object obj : emlFiles) {
File file = (File) obj;
InputStream source = null;
EmailMessage message = null;
source = new FileInputStream(file);
MimeMessage msg = new MimeMessage(null, source);
message = new EmailMessage(service);
// Set properties on the email message.
message.ParentId = msg.getMessageID();
ItemId id = (ItemId.getItemIdFromString(message.ParentId));
//service is exchange service object..
service.moveItem(id,destinationFolder.getId());
}
Thanks in advance!!
Using MoveItem won't work in this instance because you can only move an existing store item from one place to other. Just call save on the Item you created and use the destination Folder you want to save it to. eg
message.save(destinationFolder.getId());
You may also want to set the SentFlag so the message won't show as a draft eg see the example in https://msdn.microsoft.com/en-us/library/office/dn672319(v=exchg.150).aspx
ExtendedPropertyDefinition PR_MESSAGE_FLAGS_msgflag_read = new ExtendedPropertyDefinition(3591, MapiPropertyType.Integer);
email.SetExtendedProperty(PR_MESSAGE_FLAGS_msgflag_read, 1);
Cheers
Glen

java read MS Outlook inbox

I would like to use java (SE) to read my Inbox in MS Outlook (2010) and then move message/email to another folder. I have tried to search on web, but found only licensed solutions or posts old couple of years. Does anyone have solution for this step? Thank you very much for your help!
Can be done using javax.mail, but a lot depends on the protocol of the server and authentication etc.
Anyways, here is a snippet (assuming imap):
Set your properties:
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imap");
props.setProperty("mail.imap.user", <user>);
props.setProperty("mail.imap.host", <host>);
props.setProperty("mail.imap.port", <port 143>);
...
Get a session and connect
Session mailSession = Session.getInstance(props);
Store mailStore = mailSession.getStore("imap");
mailStore.connect(<host>, <user>, <passwd>);
Folder dFolder = mailStore.getDefaultFolder();
Folder inbox = dFolder.getFolder(<connectFolder=INBOX?>);
inbox.open(Folder.READ_WRITE);
// Open destination folder, create if reqd
Folder destfolder = mailStore.getFolder(<destination folder>);
if (!destfolder.exists())
destfolder.create(Folder.HOLDS_MESSAGES);
Message []inMessages = inbox.getMessages();
if (inMessages .length != 0) {
inbox.copyMessages(inMessages , destfolder);
for (int i=0; i< inMessages.length; i++) {
// Custom Processor which readsMessages and performs some action.
// getProcessor().readMessage(inMessages[i]);
inMessages[i].setFlag(Flags.Flag.DELETED, true);
}
}
Hope this helps

Categories