How to render .msg file content in ASPX page? - c#

I have some e-mails that i would like to store in SQL Server database and render within an asp.net web application.
Currently the .msg files are stored in a BLOB field and and a link to download the file to user's system is given.
However, I want to render the content of the message directly without giving the user any options to download it. The messages are in HTML format with embedded images.
All suggestions are welcome.

You need parse MIME. You can find classes for it on the next page: http://www.codeproject.com/KB/IP/ReceivingMail.aspx
You can load mail body to the MailItem:
Pop3Lib.MailItem m = new Pop3Lib.MailItem("mail body here");
Pop3Lib.MailItem m = new Pop3Lib.MailItem(System.IO.File.ReadAllText("C:\\mymail.msg"));
Response.Write(m.GetText());

Related

How do I create a link to a file I have saved as a memory stream in a SQL table?

My web application (C# and ASP.Net) allows someone to upload a jpg/pdf file and save it as a memory stream inside a SQL table. This is what it looks like once written in the table:
0xFFD8FFE000104A4649.....
Now I want to provide this file back on my web interface through a link where the user can download this file. I have retrieved my file by converting the string above back to a byte array using
filedata = Encoding.ASCII.GetBytes(<string above>);
Then I called this:
string filename = "pic.jpg";
File.WriteAllBytes(filename, filedata);
But I have no clue how I should post this back to the user as a downloadable link on my web interface. Do I have to save this file to a temporary folder on my server or is there a way that I could invoke a call to render my file back as a picture where the user will be prompted to save the file or open it?
Thank you!
You will need to create a page / action / function that writes the bits back as the response to an HTTP request. Keep in mind that in doing so you will probably need to set the proper Content-Type header in the response according to what your file is.
So, you generate a link that calls your page / action / function. Then That sends the binary data back in an HTTP response. Something like
pic.jpg
If you some detail as to what framework you're using (MVC, WebForms, etc) then we can give more detailed examples.
Depending on how you want to present the image.
If you want to embed the image in the web return an link to the file with
string filename = Path.Combine(HttpRuntime.AppDomainAppPath, "Content/Images/pic.jpg");
File.WriteAllBytes(filename, filedata);
return filename;
then in the JavaScript side create an img element with thatsrc
Or if you want the user to download the file do what #squillman says in his answer

Getting the content of a .aspx file in a .cs class

What's a good method for getting the content of a .aspx web page as a String. This is from a C# Class which is compiled and deployed to the same machine.
Should I use a HTTP request? Or is there a means of doing the same through a file path, and would this trigger the code behind of the page?
If you use HTTP request, like WebClient and use WebClient.DownloadString(#"http://someSite/somepage.aspx"), that would trigger the server side code and you will get HTML generated by the server. Not the actual aspx page.
But if you use File.ReadAllLines('somepage.aspx') from your current project then you will get the file contents and it will not trigger the server side code. But you can only do that from your current project. You can't access an aspx page over http
This code retrieves the root directory that is hosting the application in IIS. Then, it concatenates the filename. Finally, it reads the contents into a string variable.
Reading a file from disk does not trigger the code-behind; only a request through the ASP.NET pipeline does that.
string path = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "Index.aspx");
string fileContents = File.ReadAllText(path);
An HTTP request would usually not return the content of the aspx file since the server should interpret it (take this with a grain of salt, it all depends on your server and how its configured, maybe you've got a special setup)
If you want to read the content of the aspx file, open the file using its location on the disk. It won't trigger the code behind the page since this is triggered by the web server and you're not using it using a file path
if you want to read the output produced when the aspx file is read, open the url to this file. It will trigger the backing code and you will received the output.

sending email with images

I have a WinForms application where a user can configure email templates that will later get mail merged with client data from the database and then sent via SMTP.
I have everything working fine, but the issue comes when the user saves images inside the template body. I'm using a DevExpress RichEdit control to allow the users to create this email body, and that control converts it to HTML that I use to send. When images are there, it uses data-uri to embed the image directly into the HTML.
The problem now comes when I send the email in this way using data-uri images, not all clients render it properly. I just tested it in Outlook, and it doesn't seem to work. I know some web based email clients will work, but I need this to work all the time, at least for the widely used email clients.
I'm wondering if others have solved this problem and what might be the best solution here? I suppose I could send the images as attachments and then reference the attachment, although I'm not sure if this is a best practice. The other alternative though is to host each image on a server somewhere, then create a service which would have to store the images (like in a DB), and provide a way to query this image over the web using an ID. Then my emails would use an img src=site/getImg?imgID=xxx type thing. However, this seems like a lot of work to do what I want to accomplish and I'm hoping I could avoid it with something easier.
Thanks in advance for any advice!!
Adding images as attachments will work, but has the downside of a larger payload and higher bandwidth utilization both for you and for your recipients. The code for adding attachments in .net is pretty simple:
var message = new MailMessage();
foreach (string path in attachmentPaths)
{
if (File.Exists(path))
{
message.Attachments.Add(new Attachment(path));
}
}
On the other hand, referencing images stored on the web somewhere reduces the size of your emails and also opens up the possibility of gathering tracking data on clients that request images. Of course, most email clients will first ask the user whether they want to download images, so your tracking will only be rough.
you should be try save the images in the local disk , and the next include the who Embedded images.
// Add image attachment from local disk
Attachment oAttachment = oMail.AddAttachment( "d:\\test.gif" );
// Specifies the attachment as an embedded image
// contentid can be any string.
string contentID = "test001#host";
oAttachment.ContentID = contentID;
oMail.HtmlBody = "<html><body>this is a <img src=\"cid:"
+ contentID + "\"> embedded image.</body></html>";
Sorry for not explain, I speak only a little in english

Use HTML string from Server Requets, and create the web page without saving it a file [in C#]

I´m sending the value of a variable via POST to a PHP page in C#. I get the data stream from the server that has all the web page in HTML with the value of the POST. This information is stored in a string variable.
I would like to open a browser and show the web page (maybe using System.Diagnostics.Process.Start("URL")), without having to save it in a file, this is showing the page in the moment and, when the browser is closed, no file is stored in the server.
Any idea?
Drop a WebBrowser control into a new form webBrowser1 and set its DocumentTextProperty to your result html
webBrowser1.DocumentText = ("<html><body>hello world</body></html>");
source:
<html><body>hello world</body></html>
You aren't going to be able to do that in an agnostic way.
If you simply wanted to open the URL in a browser, then using the Process class would work.
Unfortunately, in your case, you already have the content from creating the POST to the server, and you really want to stream that response in your application to the browser.
It's possible among the some browsers, but it's not able to be done in an agnostic way (and it's complicated even when targeting a specific browser).
To complicate matters, you want the browser to believe that the stream you are sending it is really coming from the server, when in reality, it's not.
I believe that your best bet would be to save the response to the file system in a temp file. However, before you do, add the <base> tag to the file with the URL that the file came from. This way, relative URLs will resolve correctly when rendered in the browser.
Then, just open the temporary file in the browser using the Process class.

display csv file as attachment vb.net or C#

I have a csv file as an attachment to the inbox.
They should be able to view the attachment when they click on the link, but how do I display them the csv attachments??! the browser just parses it and displays the content instead of poping the open/save window.
anyone had to open a csv file as an attachment on web before? thanks.
Response.AddHeader "content-disposition","attachment; filename=myfile.csv"
Adding this header to your response will cause the browser to treat the content as an attachment and display the 'File Download' dialog instead of attempting to display the content in the web page.

Categories