C# Open Byte Array in Default Mail Application - c#

I have an application that will save items in the DB as a blob. Users have been using it to save emails (.msg files).
The app also allows users to view uploaded files. The app gets the file as a byte array and displays it.
However it is failing when trying to open .msg files.
It used to work using the following:
Response.Body.WriteAsync(bytes, 0, bytes.Length);
However that has stopped working recently and now just downloads the file to their PC instead of opening it.
I have tried:
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(imageModel.imageString); // the string that gets converted to a byte array
System.Diagnostics.Process.Start(startInfo);
But I get an error saying the string is too long.
Does anyone know how I can open a byte array in the default email application? It will be outlook for these uers. I'd prefer to not first save it as a file on their PC and send the byte array directly to outlook.

The workaround I used for this it to simple have the app download the .msg file to the users comoputer, then open in the default mail app.
I was not able to get it to open in Outlook from a byte array.

Related

Saving byte array to file

I have a byte array and need to save it to a file.
I have tried the below code:
File.WriteAllBytes("form.txt", byteArray);
string filename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "form.txt");
I have referred https://stackoverflow.com/a/19455387/15265496 for the implementation.
I am looking for the file in Android emulator. Where will the file get saved from first line?
Should I create a form.txt in the application local folder?
Is there any alternative way to do the same?
You can find the appropriate folder using Environment.SpecialFolder.LocalApplicationData
string fileExactLocation = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "form.txt");
You can find more information at https://learn.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/data/files?tabs=windows
You cannot access the file system of an Android emulator instance via explorer or finder.
As per the Microsoft docs, if you want to store a file in Android, you can always use the application's files folder which is accessible through:
var path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
A file saved to that folder will go to
/data/user/0/com.companyname/files

How to get windows explorer full path of opened outlook MSG file?

This question is related to outlook add-in.
I have one MSG file in my file system. lets start with example.
one msg file is located in #"d:\MSG Files\MailItemSubject.msg".
When I open that msg file in the windows explorer, my office plug-in will
be started with outlook application. I register my event handler for
Outlook.Application.Inspectors.NewInspector. And try to get the full path of
the msg file in the handler function.
I want to know where this message file comes.
But I have no idea about how to get the full path.
Does anyone meet the same problem?
Does anyone have any idea about it?
How to get windows explorer full path?
That information is not exposed through any API. More than that, Outlook creates a temporary message in the default message store and creates a copy of the MSG file there.

How do you open a file on a server using different user account credentials

I'm currently developing a C# WinForms application in VS2012 (.NET 4.0). The application is a bit like an Email client and displays a list of messages. And each message can have one or one files attached.
The file attachments are stored on a different server, which can only be accessed using another user account (and not by the logged on user).
When the user double clicks on a File Attachment I need to open the file and display it in the associated application. i.e. TXT files in Notepad, DOCX files in Word, etc.
I was hoping I could just pass the full file name into the System.Diagnostics.Process.Start method, with the correct username, password and domain values and it would open the file in the associated application.
e.g.
SecureString password = new SecureString();
"MyPassword".ToCharArray().ToList().ForEach(password.AppendChar);
System.Diagnostics.Process.Start(#"\\MyServer\MyFolder\MySubFolder\MyFile.docx",
"MyUsername",
password,
"MyDomain");
But an exception is thrown with the message:
The specified executable is not a valid application for this OS platform.
I've also tried creating a ProcessStartInfo object instance and setting the FileName property to "Explorer.exe" and the Arguments property to #"\\MyServer\MyFolder\MySubFolder\MyFile.docx", and passing it as the parameter into the System.Diagnostics.Process.Start method. But this doesn't work either. No exception is thrown and the file isn't displayed either.
But if you look at the object returned by the System.Diagnostics.Process.Start method you see that an InvalidOperationException has occurred.
My C# question is, how can I open and display a file (in it's associated application), when the file is stored on a server that can only be accessed using different user credentials?
I think this should work
Step 1. Find the App registered with the extension
Step 2. Use the same Api that was mentioned
System.Diagnostics.Process.Start("App from the current Machine that was found in step 1",
#"\MyServer\MyFolder\MySubFolder\MyFile.docx",
"MyUsername",
password,
"MyDomain");
See the following links of how to find the register app for an extension
How to get recommended programs associated with file extension in C#
http://www.codeproject.com/Articles/17023/System-File-Association

how to read mail from Google mail and mark as save and save mail in local folder with C# .NET

I am trying to build a winform app to
read mails from google app
save mail and attachment to local folder
mark mail as read
Save the email body and attachments to a SQL-Server database
The latter is ok but I'm really having trouble with the first 3.
I've look everywhere and people are telling me how to send mail on a winform app but not how to read, mark and save content and attachment.
CodeProject is probably the next best resource online after SO (IMHO.) Here are some well-reviewed sample projects that will get you a long way:
http://www.codeproject.com/Articles/14304/POP3-Email-Client-NET-2-0
http://www.codeproject.com/Articles/6062/A-POP3-Client-in-C-NET
http://www.codeproject.com/Articles/188349/Read-Gmail-Inbox-Message-in-ASP-NET
http://www.codeproject.com/Articles/34495/Building-your-own-Mail-Client-using-C
http://www.codeproject.com/Articles/15611/POP3-Email-Client-with-full-MIME-Support-NET-2-0

Opening up PDFs and other documents from Silverlight out of browser

I'm having a little problem figuring out the best way to open up a file that I have stored in a database. The file is being stored as a byte array in a nvarbinary field in the database. Currently when I want to open up a file I use an ASP.NET webpage that I pass a variable to and write the file stream to the page. This works fine when using the in browser version of the Silverlight application, but when out of browser I cannot invoke a browser window to open because I don't have access to dom.
How can I open the bytearray from Silvelright without invoking a browser window? I'm able to pass the bytearray and file type to the Silverlight app no problem. I just don't know how to display it once I have it there..
Thanks!
If you are targeting windows (with full trust enabled, and not mac), you can do this out-of-browser by first writing the file to disk (either in isolated storage or in My Documents), then using a WScript.Shell COM object to have the OS open the file.
After you have saved the byte stream to a file and have the file location, you can do:
using (dynamic shell = AutomationFactory.CreateObject("WScript.Shell"))
{
shell.Run(fileLocation); //works similar to start -> run -> filename
}
If you want to leverage your existing ASP page, you can pass its URL to shell.Run and the OS will use the user's default browser to open that page.
On a mac, the best you could do is save the file to their user directory, and have them manually navigate there with finder and double-click it.

Categories

Resources