How to convert MimeEntity (message/rfc822) to MimeMessage (MimeKit) - mimekit

I have an email message that contains an envelope and I want to access the contents of the envelope as a MimeMessage to retrieve the from,to,subject,body & attachment data.
The problem is; once I retrieve the enveloped message, its of type MimeEntity and I don't seem able to convert this MimeEntity into a MimeMessage.
Is it possible to convert a MimeEntity into a MimeMessage or is it possible to get the MimeEntity's message data?
var message = await MimeMessage.Load(#"C:\temp\MimeKitTesting\sample.eml");
MimeEntity? envelopeMessage = message.BodyParts.FirstOrDefault(x => x.ContentType.MimeType == "message/rfc822");
if (envelopeMessage != null)
{
// When debugging I can see envelopeMessage.Message property but cannot access it from code.
}

After fiddling around and reading the documentation, I see that you have to cast the MimePart to MessagePart and then you can access the Message property.
var message = await MimeMessage.Load(#"C:\temp\MimeKitTesting\sample.eml");
MimeEntity? envelopeMessage = this.Message.BodyParts.FirstOrDefault(x => x.ContentType.MimeType == "message/rfc822");
if (envelopeMessage != null && envelopeMessage is MessagePart)
{
var rfc822 = envelopeMessage as MessagePart;
if (rfc822 != null)
this.Message = rfc822.Message;
}

Related

Message properties on Mqtt protocol

I'm having problems receiving message properties and message system properties on the Mqtt protocol. But it works fine when using TransportType.Amqp.
Is the explanation to be found in section "Receiving messages" at this page?
https://azure.microsoft.com/da-dk/documentation/articles/iot-hub-mqtt-support/
If yes, what does it mean and how do I do that?
Receiver code:
I'm using 1.0.12 of Microsoft.Azure.Devices.Client from nuget and all the dependencies.
//connectString: HostName=<host name>;DeviceId=<device id>;SharedAccessKey=<key>
//var deviceClient = DeviceClient.CreateFromConnectionString(connectString,TransportType.Amqp);
var deviceClient = DeviceClient.CreateFromConnectionString(connectString, TransportType.Mqtt);
while (true)
{
var message = await deviceClient.ReceiveAsync(TimeSpan.FromSeconds(2));
if (message != null)
{
//message.MessageId is null on Mqtt but not for Amqp!
}
}
Sender code:
I’m using version 1.0.11 of Microsoft.Azure.Devices.dll
string messageId = Guid.NewGuid().ToString("D");
var iotHubConnection = "HostName=<host>;SharedAccessKeyName=<...>;SharedAccessKey=<...>";
var serviceClient = ServiceClient.CreateFromConnectionString(iotHubConnection);
if (serviceClient == null) throw new Exception("ServiceClient create failed");
var serviceMessage = new Microsoft.Azure.Devices.Message(Encoding.UTF8.GetBytes("Hello World"));
serviceMessage.Ack = DeliveryAcknowledgement.Full;
serviceMessage.MessageId = messageId;
serviceMessage.Properties["message-id"] = messageId;
serviceClient.SendAsync("0123", serviceMessage).Wait();
The C sample code reads properties, but but the csharp sample code does not:
https://github.com/Azure/azure-iot-sdks/blob/master/c/iothub_client/samples/iothub_client_sample_mqtt/iothub_client_sample_mqtt.c

How to get profile picture(Image) from People App

I'm tring to get the profile picture from People App. I used
Windows.ApplicationModel.Contacts.Contact contact = new Contact();
I got Thumbnail from propety contact.Thumbnail.
I need to convert this Thumbnail to StorageFile. Could you please give inputs to solve this issue?
And, while using the following code:
IRandomAccessStreamWithContentType stream = awaitcontactInfo.Thumbnail.OpenReadAsync();
if(stream != null && stream.Size > 0)
{
//
}
Sometimes I'm getting RPC Server is unavailable Exception. Sometimes the streamSize is Zero.
You are creating new instance of Contact class. You don't have to do that to pick contact from people. You should use ContactPicker.
var contactPicker = new Windows.ApplicationModel.Contacts.ContactPicker();
contactPicker.CommitButtonText = "Select";
ContactInformation contact = await contactPicker.PickSingleContactAsync();
if (contact != null)
{
IRandomAccessStreamWithContentType stream = await contact.GetThumbnailAsync();
if (stream != null && stream.Size > 0)
{
var file = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("MyContactThumb.png", CreationCollisionOption.GenerateUniqueName);
// You can also use FileSavePicker to save file in user defined location.
Windows.Storage.Streams.Buffer MyBuffer = new Windows.Storage.Streams.Buffer(Convert.ToUInt32(stream.Size));
IBuffer iBuf = await stream.ReadAsync(MyBuffer, MyBuffer.Capacity, InputStreamOptions.None);
await FileIO.WriteBufferAsync(file, iBuf);
}
}

WinRT No mapping for the Unicode character exists in the target multi-byte code page

I am trying to read a file in my Windows 8 Store App. Here is a fragment of code I use to achieve this:
if(file != null)
{
var stream = await file.OpenAsync(FileAccessMode.Read);
var size = stream.Size;
using(var inputStream = stream.GetInputStreamAt(0))
{
DataReader dataReader = new DataReader(inputStream);
uint numbytes = await dataReader.LoadAsync((uint)size);
string text = dataReader.ReadString(numbytes);
}
}
However, an exeption is thrown at line:
string text = dataReader.ReadString(numbytes);
Exeption message:
No mapping for the Unicode character exists in the target multi-byte code page.
How do I get by this?
I managed to read file correctly using similar approach to suggested by duDE:
if(file != null)
{
IBuffer buffer = await FileIO.ReadBufferAsync(file);
DataReader reader = DataReader.FromBuffer(buffer);
byte[] fileContent = new byte[reader.UnconsumedBufferLength];
reader.ReadBytes(fileContent);
string text = Encoding.UTF8.GetString(fileContent, 0, fileContent.Length);
}
Can somebody please elaborate, why my initial approach didn't work?
Try this instead of string text = dataReader.ReadString(numbytes):
dataReader.ReadBytes(stream);
string text = Convert.ToBase64String(stream);
If, like me, this was the top result when search for the same error regarding UWP, see the below:
The code I had which was throwing the error (no mapping for the unicode character exists..):
var storageFile = await Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.GetFileAsync(fileToken);
using (var stream = await storageFile.OpenAsync(FileAccessMode.Read))
{
using (var dataReader = new DataReader(stream))
{
await dataReader.LoadAsync((uint)stream.Size);
var json = dataReader.ReadString((uint)stream.Size);
return JsonConvert.DeserializeObject<T>(json);
}
}
What I changed it to so that it works correctly
var storageFile = await Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.GetFileAsync(fileToken);
using (var stream = await storageFile.OpenAsync(FileAccessMode.Read))
{
T data = default(T);
using (StreamReader astream = new StreamReader(stream.AsStreamForRead()))
using (JsonTextReader reader = new JsonTextReader(astream))
{
JsonSerializer serializer = new JsonSerializer();
data = (T)serializer.Deserialize(reader, typeof(T));
}
return data;
}

asp.net web api file upload without saving

Ok, so I am writing a service to recieve file uploads from an iPhone application through phonegap. They send me a file and I am trying to grab the actual file without saving it to any type of file system. Currently this is what I have
[HttpPost]
public string processRequest()
{
string ext = "Entered";
Request.Content.ReadAsMultipartAsync<MultipartMemoryStreamProvider>(new MultipartMemoryStreamProvider()).ContinueWith((tsk) =>
{
ext = "Request";
MultipartMemoryStreamProvider prvdr = tsk.Result;
foreach (HttpContent ctnt in prvdr.Contents)
{
ext = "Foreach";
// You would get hold of the inner memory stream here
Stream stream = ctnt.ReadAsStreamAsync().Result;
if (stream == null)
{
ext = "Null Stream";
}
Image img = Image.FromStream(stream);
if (ImageFormat.Jpeg.Equals(img.RawFormat))
{
ext = "jpeg";
}
else if (ImageFormat.Png.Equals(img.RawFormat))
{
ext = "Png";
}
else if (ImageFormat.Gif.Equals(img.RawFormat))
{
ext = "Gif";
}
// do something witht his stream now
}
});
return ext;
}
I have put various responses in there so I can see where the function is getting to. Right now it always returns "Entered" which means its not even reading the content of the request, the end game is for me to grab the file object, convert it into an image and then to base 64. Any direction would be appreciated. Remember I want to do this without any file system so no solutions that involve mapping a path to a server folder.
Ok so a little update, I have edited my code according to my first response and at least it attempts to execute now but it just gets infinitely stuck inside the code. This happens during the ReadAsMultipartAsync function
[HttpPost]
public string processRequest()
{
string ext = "Entered";
Request.Content.ReadAsMultipartAsync(new MultipartMemoryStreamProvider()).ContinueWith((tsk) =>
{
ext = "Request";
MultipartMemoryStreamProvider prvdr = tsk.Result;
foreach (HttpContent ctnt in prvdr.Contents)
{
ext = "Foreach";
// You would get hold of the inner memory stream here
Stream stream = ctnt.ReadAsStreamAsync().Result;
if (stream == null)
{
ext = "Null Stream";
}
Image img = Image.FromStream(stream);
if (ImageFormat.Jpeg.Equals(img.RawFormat))
{
ext = "jpeg";
}
else if (ImageFormat.Png.Equals(img.RawFormat))
{
ext = "Png";
}
else if (ImageFormat.Gif.Equals(img.RawFormat))
{
ext = "Gif";
}
// do something witht his stream now
}
}).Wait();
return ext;
}
The block inside ContinueWith also runs asynchronously (if you look at the signature for ContinueWith, you'll see that it returns a Task as well). So, with the above code, essentially you're returning before any of that has a chance to execute.
Try doing:
Request.Content.ReadAsMultipartAsync().ContinueWith(...).Wait();
Also, not sure you need to go to the trouble of doing Request.Content.ReadAsMultipartAsync<MultipartMemoryStreamProvider>(new MultipartMemoryStreamProvider()); I believe Request.Content.ReadAsMultipartAsync() should suffice.
Hope that helps!

Cannot use SendTweetWithMediaOptions in windows phone

I am using TweetSharp in a Windows Phone project and no matter what I do, I can't post a tweet with media.
I am getting the exception 195: Missing or invalid parameter.
I read that usually this can be a cause of invalid data, like the stream that I provide is invalid.
I have tried other way but nothing works , I get the same exception ...
The sharing code, simplified is like this:
MediaLibrary library = new MediaLibrary();
var picture = library.Pictures[0];
var options = new SendTweetWithMediaOptions
{
Images = new Dictionary<string, Stream> { { picture.Name, picture.GetImage() } },
Status = TweetTextBox.Text,
};
AutentificateTwitterService().SendTweetWithMedia(options, (status, response) =>
_dispatcher.BeginInvoke(() =>
{
DonePosting();
if (response.StatusCode == HttpStatusCode.OK)
{
_lastPostId = status.Id;
}
else
{
MessageBox.Show(String.Format(
"There was an error sending image to Twitter{0}{1}",
Environment.NewLine,
response.Error));
}
}));
I tried sharing with linqtotwitter and worked but TweetSharp is more appropriate for my project.
Finally after some time I've found the problem to this and I am sure to other more WP and SendTweetWithMediaOptions related problems.
The thing is that if you dig into SendTweetWithMedia the way it is now you will get to TwitterService.cs where WithHammock will be called, is just the images are not passed as parrameters, so they get lost right there :)
I did fix this passing the parameters and adding
private void WithHammock<T>(WebMethod method, Action<T, TwitterResponse> action, string path, IDictionary<string, Stream> files, params object[] segments) where T : class
{
var url = ResolveUrlSegments(path, segments.ToList());
var request = PrepareHammockQuery(url);
request.Method = method;
request.QueryHandling = QueryHandling.AppendToParameters;
foreach (var file in files)
{
request.AddFile("media[]", file.Key, file.Value);
}
WithHammockImpl(request, action);
}
I will try and see if I can Pull this so that everyone else can have this fix.
Hope this helps.