How to export web page to word file and send it as an emailattachment?

A

Author #1

In my asp.net 3.5 application, I need to export part of a web page to
MS Word format and send it as email attachment.

I know how to export to MS Word and download it to desktop. But, I am
not sure how I can capture the Word file stream in my code behind and
send it as an email attachment.

Any hint is highly appreciated. Thanks.
 
A

Author #1

How are you doing that...?

Thank you. I am using the example given at http://aspalliance.com/794

I can put the string value (e.g. strMyAttachment) into a MemoryStream
object and create an Attachment as

UTF8Encoding utf8 = new UTF8Encoding();
byte[] myAttachmentBytes = utf8.GetBytes(strMyAttachment);
MemoryStream myAttachmentStream = new MemoryStream
(myAttachmentBytes );
Attachment attachment = new Attachment(myAttachmentStream ,
"myattachment.doc");
attachment.ContentType = new System.Net.Mime.ContentType
("application/msword");

Then add this attachment object to my MailMessage object and send it.
This is OK.

The problem: After I receive the email, I download the attachment to
my desktop, open it in MS Word, only to see the file presented in raw
HTML code with HTML tags. Users don't want to see this, they want to
see it rendered properly.

The button click event handler code is pasted here (same as that in
http://aspalliance.com/794).

protected void btnCreateWordDoc_Click(object sender, EventArgs e)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.ContentType = "application/
msword";

string strFileName = "GenerateDocument" + ".doc";
HttpContext.Current.Response.AddHeader("Content-
Disposition",
"inline;filename=" + strFileName);

StringBuilder strHTMLContent = new StringBuilder();

strHTMLContent.Append(" <h1 title='Heading'
align='Center'style='font-family: verdana; font -size: 80 % ; color:
black'><u>Document Heading</u> < / h1 > ".ToString());

strHTMLContent.Append("<br>".ToString());
strHTMLContent.Append(
"<table align='Center'>".ToString());

// Row with Column headers
strHTMLContent.Append("<tr>".ToString());
strHTMLContent.Append("<td style='width:100px; background:
#99CC00'><b>Column 1 < / b > < / td >".ToString());
strHTMLContent.Append("<td style='width:100px;background:
#99CC00'><b>Column 2 </b></td>".ToString());
strHTMLContent.Append("<tdstyle='width:100px; background:
#99CC00'><b>Column 3</b></td>".ToString());
strHTMLContent.Append("</tr>".ToString());

// First Row Data
strHTMLContent.Append("<tr>".ToString());
strHTMLContent.Append("<td style='width:100px'>a</
td>".ToString());
strHTMLContent.Append("<td style='width:100px'>b</
td>".ToString());
strHTMLContent.Append("<td style='width:100px'>c</
td>".ToString());
strHTMLContent.Append("</tr>".ToString());

// Second Row Data
strHTMLContent.Append("<tr>".ToString());
strHTMLContent.Append("<td style='width:100px'>d</
td>".ToString());
strHTMLContent.Append("<td style='width:100px'>e</
td>".ToString());
strHTMLContent.Append("<td style='width:100px'>f</
td>".ToString());
strHTMLContent.Append("</tr>".ToString());
strHTMLContent.Append("</table>".ToString());

strHTMLContent.Append("<br><br>".ToString());
strHTMLContent.Append("<p align='Center'> Note : This is
adynamically generated word document </p> ".ToString());
HttpContext.Current.Response.Write(strHTMLContent);
HttpContext.Current.Response.End();
HttpContext.Current.Response.Flush();
}
 
A

Author #1

Just to be clear, that method doesn't actually create a Word document - it
creates an HTML document and gives it a .doc extension for displaying in a
web browser. This technique fools the browser into thinking that it's
receiving a Word document, but it most certainly does *not* create a native
Word document...

This is fine for creating a Word "document" for viewing in a web browser.
However, whenever I need to create a proper Word document (e.g. for
emailing), I use this:http://www.aspose.com/categories/file-format-components/aspose.words-...

Under no circumstances contemplate using server-side Office automation for
this, as it's not supported by Microsoft because Office wasn't designed
(currently, anyway!) to be used this way:http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2Note
specifically the bold paragraph.

Thank you. Yes, I agree with you. That code sample simply .doc-
extension-s an HTML file. (I am using .doc-extension as a verb).

To give you more details. On my web page in question, I have two
buttons:

[ Export to Word ] [ Send To Boss ]

If a user clicks the [ Export to Word ] button, the browser asks the
user to save the file to hard drive. After the user saves it to the
hard drive, if s/he opens it in MS Word, the file displays perfectly
just like in a browser. In other words, the raw HTML tags are not
shown.

If the user clicks the [ Send To Boss ] button, the HTML string gets
sent to the boss's email as an .doc-extensioned attachment. Perfect.
But, after the boss saves the attachment, he opens it in MS Word, MS
Word displays it as raw HTML string with the ugly HTML tags!

The aspose.com tool is certainly a solution to this problem, but I
don't have the funds.

So, I am running into a dead end? There is no solution to this
problem? Any further ideas are highly appreciated. Thanks.
 
A

Author #1

I've never found one. Having said that, since using Aspose I've never needed
to look for one...

OK, thanks. I am curious about this: If we save it to desktop from
the [Export to Word] button and open it in Word, MS Word displays it
just fine. But if we send the same HTML string as an .doc-extensioned
email attachment, then we save it from our email client, open it in MS
Word, MS Word displays raw HTML tags. Do you have an idea about this
discrepancy? Thanks.
 
A

Author #1

OK, thanks.  I am curious about this: If we save it to desktop from
the [Export to Word] button and open it in Word, MS Word displays it
just fine.  But if we send the same HTML string as an .doc-extensioned
email attachment, then we save it from our email client, open it in MS
Word, MS Word displays raw HTML tags.  Do you have an idea about this
discrepancy?

I can only assume that it's something that your email client is doing.

I'd suggest using a file comparison utility to compare the two different
files and see what the differences are and then contact the supplier of your
email client...

I doubt it has anything to do with the email client. I tried both
gmail and Mozilla ThunderBird. Same behavior: download it from email,
open it in MS Word and MS Word displays raw HTML tags.

*My guess* (I am not sure how MS Word works)

When a user clicks [Export to Word], the code behind adds some
response header to it as shown in the code I pasted. Such header
information is stored in the downloaded file, and when MS Word opens
it, such information actually instructs MS Word to display it as a web
page.

However, when we simply send this HTML string as an attachment, such
response header information is missing in this attachment, and
therefore, nothing tells MS Word to display the file as a web page.
As a result, MS Word displays as a pure text file.

That said, I've tried adding content type and content disposition into
the attachment, but it didn't help. Maybe something else is missing.

Does my conjecture sound reasonable?
 
A

Author #1

What were the results of that...?

I tried Diff Doc, but the only thing it does is to compare the text.
So I gave it up.

Then, I checked the HTML source of the two copies of files:

1) file_thru_export_button.doc
2) file_thru_email_attachment.doc

Guess what, file_thru_export_button.doc is a nicely formatted HTML
file like this: <html><head></head><body> my HTML contents are here </
body></html>
whereas file_thru_email_attachment.doc does not have <html><head></
head><body> and </body></html> to wrap up my HTML contents.

Once I edit file_thru_email_attachment.doc to wrap up my HTML contents
with <html><head></head><body> and </body></html>, MS Word displays it
perfectly. It is obvious, as as long as your document is a well-
formatted HTML doc, MS Word will work as a browser.

Demystified and easy! Thank you very much!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top