How to fetch all headers of several messages in MailKit - c#

In MailKit,
You can fetch the IMessageSummary (containing the specified headers and the full message) for several messages with the following
ImapFolder folder;
List<UniqueId> uids;
var messageSummaries = folder.Fetch(uids,
MessageSummaryItems.Full,
new HashSet<string>{"X-Mailer"});
If a pass an empty HashSet<string>, I get a System.ArgumentException: The set of header fields cannot be empty.
How can I fetch all headers available ?
Note: I know that I could get the headers on each message, one by one, but it is too slow to match my performance needs.

Hacking into MailKit
As MailKit is open-source, I just modified the code of ImapFolderFetch to handle the fetch for all headers and made a pull request. The owner of the library improved it by adding a new value in MessageSummaryItems.
Usage:
var messageSummaries = folder.Fetch(uids, MessageSummaryItems.Headers);
Note: Until a new release is done, you ll have to build your own from github master.

Related

Azure datafactory: Headers in REST dataset with C#

I'm currently building a datafactory that needs to be rolled out to a whole slew of tenants. So I've opted to write C# code to create all the datafactory artifacts using a configuration file.
I've got almost everything done. I just run into a snag when creating the REST stuff. I need to add Headers to a WebActivity and to a REST Dataset.
I've tried doing this using a string which resulted in every character in the string becoming a separate header.
I've tried doing this using the HeaderOption class which resulted in... well... nothing. Empty headers in the DataFactory.
What is the best way to go about this?
Example code for the WebActivity (I'm assuming that getting this to work will mean getting the Dataset to work as well);
new WebActivity
{
Name = config.TokenActionName,
Method = config.TokenMethod,
Url = config.TokenUrl,
Headers = config.TokenHeader,
Body = restBody
},
Where config.TokenHeader is either
config.TokenHeader = "\"Content-Type\": \"application/x-www-form-urlencoded\""
or
config.TokenHeader = new HeaderOption("Content-Type", "application/x-www-form-urlencoded");

How to download the picture of a Google+ user only if it has changed after a certain date?

I am using Google API to get information about an authenticated user. I can get the basic profile information, such as the ID and the full name. From the profile information, I can get the URL to the picture:
var plusMeUri = new Uri($"https://www.googleapis.com/plus/v1/people/me?key=<APP-ID>&access_token=<ACCESS-TOKEN>");
string userResponse = await HttpClient.GetStringAsync(plusMeUri);
JObject userObject = JObject.Parse(userResponse);
...
var imageObject = userObject.GetValue("image") as JObject;
var pictureUrl = imageObject.GetValue("url").Value<string>();
var pictureUri = new Uri(pictureUrl);
string uri = $"{pictureUri.Scheme}://{pictureUri.Host}{pictureUri.AbsolutePath}";
var pictureRequest = new HttpRequestMessage(HttpMethod.Get, uri);
pictureRequest.Headers.IfModifiedSince = <previous-timestamp>;
HttpResponseMessage pictureResponse = await HttpClient.SendAsync(pictureRequest);
if (pictureResponse.StatusCode == HttpStatusCode.NotModified)
// No need to handle anything else
return;
Question
I do not want to download the user's picture if it has not changed. This is why I am using the IfModifiedSince property. It does work with Facebook's API but it does not seem to work with Google's. How can I make it work?
From the information given, it seems like what you're trying to do is determine whether the image you're downloading/about to download is the same image as you've downloaded before. After looking at the Google+ API docs, it looks like the header you've been using isn't officially (at least not obviously) supported by their APIs.
But this is not the only way we can determine whether the image has changed or not (in fact, date last modified isn't necessarily the best way to do this anyway). Alternative methods include:
1) diffing the two images
2) checking the url (if we can assume different resources have different urls)
1 is likely the most accurate but also likely the least efficient, so I'll leave that to you to solve if you decide to go that route. I think the most promising is #2. I went ahead and played around with the API a little bit and it looks like the image.url field changes when you update your profile picture.
For example, here are my last two Google+ profile picture URLs:
https://lh4.googleusercontent.com/-oaUVPGFNkV8/AAAAAAAAAAI/AAAAAAAAAqs/KM7H8ZIFuxk/photo.jpg?sz=50
https://lh4.googleusercontent.com/-oaUVPGFNkV8/AAAAAAAAAAI/AAAAAAAAl24/yHU99opjgN4/photo.jpg?sz=50
As such, instead of waiting for the response from the server and checking its header to decide whether the image has been updated or not, you may be able to short-circuit the entire HTTP request by simply checking whether the last image you pulled down was from the same url or not. If it was from the same URL, it's likely you've already acquired that image otherwise you may not have it so should incur the cost of downloading anyway.
In this case, your code would read something like:
var imageObject = userObject.GetValue("image") as JObject;
var pictureUrl = imageObject.GetValue("url").Value<string>();
if(pictureUrl != <previous-picture-url>)
{
// insert get new picture logic here...
}

Send and return variable with c# API call?

I have a c# script task in an ssis package designed to geocode data through my company's proprietary system. It currently works like this:
1) Pull query of addresses and put in data table
2) Loop through that table and Foreach row, build request, send request, wait for response, then insert back into the database.
The issue is that each call takes forever to return, because before going out and getting a new address on the api side, it checks a current database(string match) to ensure the address does not already exist. If not exists, then go out and get me new data from a service like google.
Because I'm doing one at a time, it makes it easy to keep the ID field with the record when I go back to insert it into the database.
Now comes the issue at hand... I was told to configure this as multi-thread or asynchronous. Here is the page I was reading on here about this topic:
ASP.NET Multithreading Web Requests
var urls = new List<string>();
var results = new ConcurrentBag<OccupationSearch>();
Parallel.ForEach(urls, url =>
{
WebRequest request = WebRequest.Create(requestUrl);
string response = new StreamReader(request.GetResponse().GetResponseStream()).ReadToEnd();
var result = JsonSerializer().Deserialize<OccupationSearch>(new JsonTextReader(new StringReader(response)));
results.Add(result);
});
Perhaps I'm thinking about this wrong, but if I send 2 requests(A & B) and lets say B actually returns first, how can I ensure that when I go back to update my database I'm updating the correct record? Can I send the ID with the API call and return it?
My thoughts are to create an array of requests, burn through them without waiting for a response and return those value in another array, that I will then loop through on my insert statement.
Is this a good way of going about this? I've never used Parrallel.ForEach, and all the info I find on it is too technical for me to visualize and apply to my situation.
Perhaps I'm thinking about this wrong, but if I send 2 requests(A & B) and lets say B actually returns first, how can I ensure that when I go back to update my database I'm updating the correct record? Can I send the ID with the API call and return it?
None of your code contains anything that looks like an "ID," but I assume everything you need is in the URL. If that is the case, one simple answer is to use a Dictionary instead of a Bag.
List<string> urls = GetListOfUrlsFromSomewhere();
var results = new ConcurrentDictionary<string, OccupationSearch>();
Parallel.ForEach(urls.Distinct(), url =>
{
WebRequest request = WebRequest.Create(url);
string response = new StreamReader(request.GetResponse().GetResponseStream()).ReadToEnd();
var result = JsonSerializer().Deserialize<OccupationSearch>(new JsonTextReader(new StringReader(response)));
results.TryAdd(url, result);
});
After this code is done, the results dictionary will contain entries that correlate each response back to the original URL.
Note: you might want to use HttpClient instead of WebClient, and you should take care to dispose of your disposable objects, e.g. StreamReader and StringReader.

Azure Mobile Services PullAsync() Not Filling Sync Table

So I have a remote table called Profile that has multiple entries made already. Now I'm trying to integrate offline capabilities into my application. As of right now I'm having issues with PushAsync and PullAsync methods in my code.
I would like to be able to copy all the data from my remote table into my local table just by calling PullAsync, and although it doesn't throw any exceptions it also doesn't populate my table. Here's an example of what I mean.
var db = new SQLiteConnection("syncstoreTEMP.db");
var store = new MobileServiceSQLiteStore("syncstoreTEMP.db");
store.DefineTable<Profile>();
MobileService.SyncContext.InitializeAsync(store).Wait();
var remoteValues = MobileService.GetTable<Profile>()
.ToListAsync()
.Result;
MobileService.GetSyncTable<Profile>()
.PullAsync(null, MobileService.GetSyncTable<Profile>().CreateQuery())
.Wait();
var localValues = MobileService.GetSyncTable<Profile>()
.ToListAsync()
.Result;
When I run this code remoteValues has a total length of 5, but localValues still shows 0 entries even after issuing a Pull from remote. How can I get the sync table to match my remote table?
It's strange that you're not getting any exceptions, but you seem to be creating two connections to your local store. You should not have the line for new SQLiteConnection, only the line that creates the MobileServiceSQLiteStore.
Here's a tutorial that walks through how to set things up: https://azure.microsoft.com/en-us/documentation/articles/mobile-services-xamarin-ios-get-started-offline-data/
I also recommend that you add a DelegatingHandler to your MobileServiceClient in order to log all of the requests and responses. Then you can see exactly what's going wrong. Here's a sample that shows this, and also includes a logging extension to the SQLite store so you can see exactly what is happening locally: https://github.com/paulbatum/FieldEngineerLite/blob/master/FieldEngineerLite.Client/FieldEngineerLite/Helpers/LoggingHelpers.cs

Exchange EWS Managed API Error while Updating EmailMessage on certain properties

I am working against the Exchange 2010 EWS Managed API and trying to update emails (EmailMessage).
While updating the EmailMessage's Sender.Name property, I get an exception upon Update(), but if I try to update the EmailMessage's Subject, it works just fine.
private void UpdateEmail(ItemId itemId)
{
try
{
EmailMessage emailMessage = EmailMessage.Bind(service, itemId, new PropertySet(EmailMessageSchema.Sender, EmailMessageSchema.Subject));
// Test 1 - this works:
emailMessage.Subject = "Testing";
emailMessage.Update(ConflictResolutionMode.AlwaysOverwrite);
// Test 2 - this does NOT work (if I comment out the previous 2 lines btw):
emailMessage.Sender.Name = "John Smith";
emailMessage.Update(ConflictResolutionMode.AlwaysOverwrite); // exception thrown
...
I get the following error from Test 2:
The request failed schema validation: The element 'Updates' in namespace 'http:/
/schemas.microsoft.com/exchange/services/2006/types' has incomplete content. Lis
t of possible elements expected: 'AppendToItemField, SetItemField, DeleteItemFie
ld' in namespace 'http://schemas.microsoft.com/exchange/services/2006/types'.
EWS doesn't support changing the Sender address via the Strongly typed properties like you are trying. The only way they maybe successful is to modify the underlying extended properties and generating oneoff or wrapped entry-id where applicable the props you need to update are
PR_SENDER_ADDRTYPE_W
PR_SENDER_EMAIL_ADDRESS_W
PR_SENDER_NAME_W
PR_SENDER_ENTRYID
PR_SENDER_SEARCH_KEY
PR_SENT_REPRESENTING_EMAIL_ADDRESS_W
PR_SENT_REPRESENTING_ADDRTYPE_W
PR_SENT_REPRESENTING_NAME_W
PR_SENT_REPRESENTING_ENTRYID
PR_SENT_REPRESENTING_SEARCH_KEY
Note there maybe others as well you need to use a MAPI editor like OutlookSpy of MFCMapi to look at an item yourself.
Cheers
Glen

Categories