Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
Posts: 15
Registered: ‎05-14-2011
My Device: Bold 2 9700
My Carrier: Airtel

How to populate emails in list using java api ?

Hi,

 

I want to create an application which polulate emails from all folders in list. How do I acheive it ?

 

Thanks in advance.

Developer
Posts: 571
Registered: ‎04-11-2009
My Device: BB 10 Dev Alpha

Re: How to populate emails in list using java api ?

[ Edited ]

Hi,

 

You will need to implement FolderListener interface in your application.  Please check : http://docs.blackberry.com/en/developers/deliverables/11935/Add_listener_to_folder_565436_11.jsp

 

Regards,
Kanak Sony
------------------------------------------------------------------------------------------
http://dodevelopnshare.wordpress.com/ | http://www.linkedin.com/profile/view?id=188131481&trk=tab_pro
Contributor
Posts: 15
Registered: ‎05-14-2011
My Device: Bold 2 9700
My Carrier: Airtel

Re: How to populate emails in list using java api ?

Till now I wrote following code but it retrieve only One email from INBOX and One from OUTBOX, but I am having 59 emails in my INBOX.

package pkgHTTBBAgent;
import java.util.Date;
import net.rim.blackberry.api.mail.Address;
import net.rim.blackberry.api.mail.BodyPart;
import net.rim.blackberry.api.mail.Folder;
import net.rim.blackberry.api.mail.FolderNotFoundException;
import net.rim.blackberry.api.mail.Message;
import net.rim.blackberry.api.mail.MessagingException;
import net.rim.blackberry.api.mail.MimeBodyPart;
import net.rim.blackberry.api.mail.Multipart;
import net.rim.blackberry.api.mail.NoSuchServiceException;
import net.rim.blackberry.api.mail.Session;
import net.rim.blackberry.api.mail.Store;
import net.rim.blackberry.api.mail.TextBodyPart;
import net.rim.blackberry.api.mail.Transport;
import net.rim.blackberry.api.mail.BodyPart.ContentType;

import org.kxml2.kdom.*;

public class BBMails {

private Document doc = null;
String plainTextMessage = null;
String htmlMessage = null;
Element eMails = null;
Element eMail = null;

public BBMails(Document m_doc){ doc = m_doc; }

public Element geteMails()
{
eMails = doc.createElement("", "eMails");
Store store;
try
{
store = Session.waitForDefaultSession().getStore();
Folder[] folders = store.list();
for(int fol=0; fol<folders.length; fol++)
{
Folder storeFolder = store.getFolder(folders[fol].getId());
ScanFolderForMail(storeFolder);
ScanFolder(store, folders[fol]);
}
return eMails;
} catch (Exception e)
{ return null; }
}

private void ScanFolder(Store store, Folder folder)
{
try
{
Folder[] folders = folder.list();
for(int x=0; x<folders.length; x++)
{
Folder fol = store.getFolder(folders[x].getId());
ScanFolderForMail(fol);
ScanFolder(store, fol);
}
}
catch (Exception ex)
{

}
}

private void ScanFolderForMail(Folder Folder2Scan)
{
try {
Message[] Msgs = Folder2Scan.getMessages();

//int msgCount = 0;
//msgCount = Msgs.length;

eMail = doc.createElement("", "eMail");

for(int i=0; i<Msgs.length; i++)
{
Message msg = Msgs[i];

eMail.setAttribute("", "Folder", Folder2Scan.getName());

Address[] recipients = msg.getRecipients(Message.RecipientType.TO);
if(recipients != null)
{
getRecipients(recipients, eMail);
}

String strSent = "-";
Date sent = msg.getSentDate();
if (sent != null)
{
strSent = isFieldNull(sent.toString());
}
eMail.setAttribute("", "Sent_Date", strSent);

Address from = msg.getFrom();
String getAddr = "-";
String getName = "-";
if(from != null)
{
getAddr = isFieldNull(from.getAddr());
getName = isFieldNull(from.getName());
}
eMail.setAttribute("", "From_Address", getAddr);
eMail.setAttribute("", "From_Name", getName);

String subject = msg.getSubject();
eMail.setAttribute("", "Subject", isFieldNull(subject));

Object o = msg.getContent();
String strPlainTextMsg = "-";
String strhtmlMsg = "-";
if (o != null)
{
getMailBody(o, eMail);
strPlainTextMsg = isFieldNull(plainTextMessage);
strhtmlMsg = isFieldNull(htmlMessage);
}
eMail.setAttribute("", "Content_Plain", strPlainTextMsg);
eMail.setAttribute("", "Content_Other", strhtmlMsg);
eMails.addChild(Node.ELEMENT, eMail);
}
} catch (FolderNotFoundException fnfe) {

} catch (MessagingException me) {

}
}

private void getRecipients(Address[] adds, Element eMail)
{
for(int i=0; i<adds.length; i++)
{
Address add = adds[i];
eMail.setAttribute("", "Recipient_Address_Display_Name" + String.valueOf(i), isFieldNull(add.getName()));
eMail.setAttribute("", "Recipient_Address" + String.valueOf(i), isFieldNull(add.getAddr()));
}
}

private void getMailBody(Object mailContent, Element eMail)
{
if(mailContent instanceof Multipart)
{
Multipart mp = (Multipart)mailContent;
for(int count=0; count<mp.getCount();count++)
{
getMailBody(mp.getBodyPart(count),eMail);
}
}
else if(mailContent instanceof TextBodyPart)
{
try
{
TextBodyPart tbp = (TextBodyPart)mailContent;
plainTextMessage = (String)tbp.getContent();
if(tbp.hasMore() && !tbp.moreRequestSent())
{
Transport.more(tbp, true);
}
}
catch(MessagingException me)
{

}
}
else if(mailContent instanceof MimeBodyPart)
{
MimeBodyPart mbp = (MimeBodyPart)mailContent;
readMimeBodyPart(mbp);
}
}

private void readMimeBodyPart(MimeBodyPart mbp)
{
try
{
Object obj = mbp.getContent();
String mimeType = mbp.getContentType();
String body = null;

if(obj instanceof String) { body = (String)obj; }
else if(obj instanceof byte[]) { body = new String((byte[])obj); }

if(mimeType.indexOf(ContentType.TYPE_TEXT_PLAIN_STRING)!=-1)
{
plainTextMessage = body;
if(mbp.hasMore() && !mbp.moreRequestSent())
{
Transport.more((BodyPart)mbp, true);
}
}
else if(mimeType.indexOf(ContentType.TYPE_TEXT_HTML_STRING)!=-1)
{
htmlMessage = body;
if(mbp.hasMore() && !mbp.moreRequestSent())
{
Transport.more((BodyPart)mbp, true);
}
}
}
catch(MessagingException me)
{

}
}

private String isFieldNull(String FieldValue)
{
if (FieldValue == null || FieldValue.length()==0)
{
return "-";
}
return FieldValue;
}
}
Contributor
Posts: 15
Registered: ‎05-14-2011
My Device: Bold 2 9700
My Carrier: Airtel

Re: How to populate emails in list using java api ?

please repy :Helpsmilie: