Access gmail inbox messages using Blackberry API - blackberry

Blackberry has the ability to receive gmail messages and they show up in the list of incoming messages along with other email accounts, text messages, etc.. Im trying to access these gmail messages programatically using the Store class but these messages are nowhere to be found.
Below shows basically what I'm trying to do. Why don't gmail messages show up in the inbox folders, but yet they do show up in the mail application?
Store store = null;
Folder[] folderList = null;
Messages[] messageList = null;
int messageCount = 0;
try{
store = Session.waitForDefaultSession().getStore();
folderList = store.list(Folder.Inbox);
for(int i = 0; i < folderList.length; i++){
messageCount += folderList[i].getMessages().length;
}
}
catch(Exception e{
}

Related

Fetching gmail emails using .NET MVC

I'm trying to create a little web application to act as a web mail client for Gmail...
I've used the following code to fetch the emails from my inbox:
public ActionResult Index()
{
using (var client = new ImapClient())
{
using (var cancel = new CancellationTokenSource())
{
ServicePointManager.ServerCertificateValidationCallback += (o, c, ch, er) => true;
client.Connect("imap.gmail.com", 993, true, cancel.Token);
// If you want to disable an authentication mechanism,
// you can do so by removing the mechanism like this:
client.AuthenticationMechanisms.Remove("XOAUTH");
client.Authenticate("********#gmail.com", "****", cancel.Token);
// The Inbox folder is always available...
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadOnly, cancel.Token);
m = new List<string>();
// download each message based on the message index
for (int i = 0; i < inbox.length; i++)
{
var message = inbox.GetMessage(i, cancel.Token);
m.Insert(i, message.TextBody);
}
client.Disconnect(true, cancel.Token);
}
}
return View(m.ToList());
}
The reason why I dislike this is way of doing is that this part of code:
for (int i = 0; i < inbox.length; i++)
{
var message = inbox.GetMessage(i, cancel.Token);
m.Insert(i, message.TextBody);
}
It takes so long to fetch all the emails, approximately 40 emails are fetched each 5 seconds... So if someone has 2000 emails, it'd take 20 minutes to load all the emails...
Is there any faster way to load all the emails into my MVC application? :/
P.S. I've tried doing it with email which has 10000 emails, and it takes forever to fetch all the emails....
If all you want is the text body of the message, you could potentially reduce IMAP traffic by using the following approach:
var messages = inbox.Fetch (0, -1, MessageSummaryItems.UniqueId | MessageSummaryItems.BodyStructure);
int i = 0;
foreach (var message in messages) {
var part = message.TextBody;
if (part != null) {
var body = (TextPart) inbox.GetBodyPart (message.UniqueId, part);
m.Insert (i, body.Text);
} else {
m.Insert (i, null);
}
i++;
}
What this does is send a batched FETCH request to the IMAP server requesting an "outline" (aka body structure) of the message and its unique identifier.
The loop that follows it then looks through the structure of the message to locate which MIME part contains the message's text body and then fetches only that particular sub-section of the message.
In general, you do not watch to download every message over IMAP. The purpose of IMAP is to leave all of the messages on the IMAP server and just fetch the least amount of data possible that you need in order to display whatever it is you want to display to the user.
It should also be noted that you don't actually need to use a CancellationTokenSource unless you are actually planning on being able to cancel the operations.
For example, your code snippet could be replaced with:
public ActionResult Index()
{
using (var client = new ImapClient())
{
ServicePointManager.ServerCertificateValidationCallback += (o, c, ch, er) => true;
client.Connect("imap.gmail.com", 993, true);
// If you want to disable an authentication mechanism,
// you can do so by removing the mechanism like this:
client.AuthenticationMechanisms.Remove("XOAUTH");
client.Authenticate("********#gmail.com", "****");
// The Inbox folder is always available...
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadOnly);
m = new List<string>();
var messages = inbox.Fetch (0, -1, MessageSummaryItems.UniqueId | MessageSummaryItems.BodyStructure);
int i = 0;
foreach (var message in messages) {
var part = message.TextBody;
if (part != null) {
var body = (TextPart) inbox.GetBodyPart (message.UniqueId, part);
m.Insert (i, body.Text);
} else {
m.Insert (i, null);
}
i++;
}
client.Disconnect(true);
}
return View(m.ToList());
}
Since you are writing your own webmail front-end to GMail, you may find the following suggestion useful:
When you look at the GMail webmail user interface or Yahoo Mail!'s user interface, you've probably noticed that they only show you the most recent 50 or so messages and you have to specifically click a link to show the next set of 50 messages and so on, right?
The reason for this is because it is inefficient to query the full list of messages and download them all (or even just the text bodies of all of the messages).
What they do instead is ask for just 50 messages at a time. And in fact, they don't ask for the messages at all, they ask for the summary information like so:
var all = inbox.Search (SearchQuery.All);
var uids = new UniqueIdSet ();
// grab the last 50 unique identifiers
int min = Math.Max (all.Count - 50, 0);
for (int i = all.Count - 1; i >= min; i--)
uids.Add (all[i]);
// get the summary info needed to display a message-list UI
var messages = inbox.Fetch (uids, MessageSummaryItems.UniqueId |
MessageSummaryItems.All | MessageSummaryItems.BodyStructure);
foreach (var message in messages) {
// the 'message' will contain a whole bunch of useful info
// to use for displaying a message list such as subject, date,
// the flags (read/unread/etc), the unique id, and the
// body structure that you can use to minimize your query when
// the user actually clicks on a message and wants to read it.
}
Once the user clicks a message to read it, then you can use the message.Body to figure out which body parts you actually need to download in order to display it to the user (i.e. avoid downloading attachments, etc).
For an example of how to do this, check out the ImapClientDemo sample included in the MailKit GitHub repo: https://github.com/jstedfast/MailKit

How to get LinkedIn Updates detail from it "updateKey"?

I got following JSON from https://api.linkedin.com/v1/people/~/network/updates
(
{
isCommentable = 0;
isLikable = 0;
timestamp = "-1";
updateContent = {
........
};
updateKey = "UPDATE-c2858344-12394303";
updateType = CMPY;
}
,......
)
In above all the array JSON,updateKey is unique like any ID.
I need to show Linkedin Update URL based on this unique updateKey to show it in webview of iOS application.
I try to found this in LinkedIn api But I not found proper solution.
Thanks in advance.

Push notification not sent to iPhone device

I get the tokenUpdate from the device, and then I try to send a push notification. But I don't think that it is working as when I send a push notification, I do not get any response from the device. Isn't it suppose to poll the server to check for any MDM Commands? Instead I keep receiving token update.
String cToken = token;
String cAlert = message;
// Ready to create the push notification
byte[] buf = new byte[256];
MemoryStream ms = new MemoryStream();
BinaryWriter bw = new BinaryWriter(ms);
char[] tokenChars = token.ToCharArray();
byte[] deviceTokenBytes = new byte[tokenChars.Length];
for (int i=0; i < deviceTokenBytes.Length; i++)
{
deviceTokenBytes[i] = Convert.ToByte(tokenChars[i]);
}
// byte[] deviceToken = HexToData(cToken);
bw.Write(deviceTokenBytes);
// Create the APNS payload - new.caf is an audio file saved in the application bundle on the device
//string msg = "{\"aps\":{\"alert\":\"" + cAlert + "\",\"badge\":" + iBadge.ToString() + ",\"sound\":\"new.caf\"}}";
string msg = "{\"mdm\":\"+ mPushMagic +"\"}";
// Write the data out to the stream
// bw.Write((byte)msg.Length);
bw.Write(msg.ToCharArray());
bw.Flush();
if (sslStream != null)
{
sslStream.Write(ms.ToArray());
return true;
}
return false;
}
UPDATED: I removed the '<' from the pushmagic id
I'd recommend removing the chevrons from your mdm message to start with.

How to find forground application Unique ID/Name in Blackberry

I have problem in finding the current opened application name.
I used ApplicationManager class for getting the visible applications.
The application descriptors are showing the same name for all these apps Email,Messaging,Sms,call log.
They are displaying "net_rim_bb_messaging_app" for all the above apps.
I need to find a unique identifier/name for these applications(Email,messaging,sms,calllog) when they are opened.
I tried this for the past 3 days and can't find a solution.
Can you please let me know a solution for this?
ApplicationDescriptor [] appDis = manager.getVisibleApplications();
int currentForgroundAppID = manager.getForegroundProcessId();
for(int i=0;i<appIDs.length;i++)
{
if(appDis[i].getModuleName().equals("net_rim_bb_messaging_app"))
{
//print Messaging app in foreground...
}
}
the case with in the for loop above is true for every app in this list.
Email,
Text message
Call log...
But, I need to find a unique way to find the application that was opened.
Thanks In Adv,
Satish.k
following code can display current foregroundApplication name
ApplicationDescriptor[] mAppDes;
ApplicationManager appMan = ApplicationManager.getApplicationManager();
appMan.getForegroundProcessId();
mAppDes = appMan.getVisibleApplications();
for (int i = 0; i < mAppDes.length; i++) {
boolean isFG = appMan.getProcessId(mAppDes[i]) == appMan.getForegroundProcessId();
if(isFG)
{
System.out.println("This is your Foteground application Name"+mAppDes[i].getName());
}else{
System.out.println("This is your Background application Name"+mAppDes[i].getName());
}
}

Access denied when asking for the count of all webs - SharePoint WSS 3.0 SP2

I'm trying to list all the links on all the SharePoint web sites (parent and sub). When I'm logged in as the admin user it works just fine. However, when I sign on as a normal user I get an access denied error. And the error is thrown when I check the count property of the SPWebCollection.
SPSite oSiteCollection = SPContext.Current.Site;
SPWebCollection collWebsite = oSiteCollection.AllWebs;
StringBuilder labelText = new StringBuilder();
for (int i = 0; i < collWebsite.Count; i++) // <---- Access denied on count
{ // get links }
I tried adding giving my normal user full site control and I still received the access denied error. Any idea what access rule is being checked when accessing the count property?
You can try and use your application pool's account using the RunWithElevatedPrivileges method on SPSecurity.
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite oSiteCollection = SPContext.Current.Site;
SPWebCollection collWebsite = oSiteCollection.AllWebs;
StringBuilder labelText = new StringBuilder();
for (int i = 0; i < collWebsite.Count; i++) // <---- Access denied on count
{ // get links }
});

Resources