Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have an ASP.NET website. I want the users to send an email to their friends with url of the page they like. I do not want to use email from site but i would prefer the users to use their own email id for sending the url of current page to their friends.
To solve this issue i generated a word document which has link to the current page as a body of the Ms Word document, My code is simple as follows:
C#
string url = HttpContext.Current.Request.Url.ToString();
        string strBody = "<html>" +
            "<body>" + 
                "<div>Please visit this site</div>" +
                "<div> <a href='" +url+"'>Requested Page</a></div>"+
                
            "</body>" +
            "</html>";
        string fileName = "RecommendedSite.doc";
        // You can add whatever you want to add as the HTML and it will be generated as Ms Word docs
        Response.AppendHeader("Content-Type", "application/msword");
        Response.AppendHeader ("Content-disposition", "attachment; filename="+ fileName);
       Response.Write(strBody);

The word document is generated properly but there are two problems where i need help:
. firstly the word document that is generated, is downloaded to download folder. I want this to ope automatically as word document.
secondly i want the word document to open in Email mode so that the user can just write email address and send it. I do not want to open in Home mode, as it opens by default there.

Many thanks to all for help

What I have tried:

I have tried creating a word document it work successfully.
For the problems i face, i have tried the following code:
System.Diagnostics.ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.FileName = "WINWORD.EXE";
startinfo.Arguments = fileName;
Process.Start(startinfo);
but it get the error page not found.
I wonder if there is any way i can save the generated word document on the site itself and open it for users.
for the second problem i have tried some options for Response.AppendHeader but those i guess do not appear to work.
Posted
Updated 21-Apr-16 6:43am
v2

1 solution

That's a perverse way to do it! What if the user doesn't have Word installed? What if they they don't have this mythical "email" mode within Word? What if they use a different email client? Etc.

And the reason you can't use Process.Start to launch Word is because your code is running on the server. Even if Word is installed on the server, it would open on the server, where nobody would be able to see it.

What's wrong with a straight mailto: link?
C#
string subject = "Look at this page";
string url = "http://www.codeproject.com/Questions/1094998/How-do-I-get-the-word-file-opened-dynamically-that";
string body = "Please visit this page\n" + url;

hyperlink.NavigateUrl = string.Format("mailto:?subject={0}&body={1}",
    HttpUtility.UrlEncode(subject),
    HttpUtility.UrlEncode(body));

HTML
<a href="mailto:?subject=Look%20at%20this%20page&body=Please%20visit%20this%20page%0Ahttp%3A%2F%2Fwww.codeproject.com%2FQuestions%2F1094998%2FHow-do-I-get-the-word-file-opened-dynamically-that">Send a link</a>

Send a link

You can't specify an HTML body, but most email clients automatically turn URLs into hyperlinks in plain-text messages.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 22-Apr-16 0:18am    
My 5, with a lot of doubt that the inquirer understands all that. But it's still useful.
—SA
Member 10235977 22-Apr-16 11:06am    
thanks. both the alternatives work. I would prefer the first one, as it uses code behind. but there is a little problem there. The body and subject is displayed with '+' mark after every word like Please + visit + this + page in stead of just giving 'Please visit this page'. How can i get over it.
Many thanks for your help.
Richard Deeming 22-Apr-16 11:35am    
Try replacing HttpUtility.UrlEncode with Uri.EscapeDataString. The former uses + for spaces, whereas the latter uses %20; the difference doesn't matter for most protocols, but the mailto: protocol doesn't seem to like +.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900