Sending a web page with SMTP

I have developed an Web App that summarizes all of the information that I want to present to the user.  I have a submit button that I want to have send the resulting page to the end user for confirmation.

I have been working with SmtpClient and it successfully sends a test message but I cant figure out how to embed the resulting web page in the email.

Can this be done from the server   Does anyone have any sample code

Vincent Fournier




Answer this question

Sending a web page with SMTP

  • maqk

    You are correct..

    The question I have is how do I set the MailMessage.Body to the value of page2.aspx   Also, is it necessary to send it to page2.aspx.  Why couldn't I just make MailMessage.Body be page1.aspx

    Vincent Fournier



  • Nick Swan

    WebRequest MyWebRequest = HttpWebRequest.Create("Page1.aspx");
    yes
    Then read the response. This is the HTML that the user would have seen
    Then send the HTML as part of the email body


  • Cortexbomber

    Set the IsBodyHTML property on the MailMessage

  • Tony Rodriguez

    We should define the question a little more clearly.

    1) User is looking at the HTML page prodcuded by an ASPX page (lets say) page1.aspx

    2) You want to send the HTML the user is looking at as an EMAIL to some one else
    when the user clicks the submit button.

    Is this correct

    If I understood the problem correctly, then you can do the following.

    1. When the user clicks the submit button, you can direct it to page2.aspx
    and let the page2.aspx generate an HTML string that was originally presented to the user from Page1.aspx - perhaps a simplified version

    2. Then you can just set the MailMessage.Body and set
    MailMessage.IsBodyHTML = true and send it



  • webdevsam

    Well you see, the page1.aspx is an HTML generator. If you send the Page1.aspx source it will be useless. What you really need is the HTML generated by Page1.aspx.  You really don't need page2.aspx. In page1.aspx itself you can detect the postback and generate the HTML needed.

    One neat trick you can do is in Page1.aspx, Use the HttpWebRequest to call
    page1.aspx itself. Like you are making a call to Page1.aspx from a browser.
    You would be using System.Net HTTPRequest class. When you do this the
    response would contain the exact HTML that the page1.aspx would have generated for the actual user. Then you can get the HTML and send it in an EMAIL.

    Does this work



  • Israel Hilerio

    I wanted to add a little more to the description:

    This is an internal app and and basically all it will need to do is send the aspx page that the current user is looking at to their manager for notification of the configuration.

    I don't really have anything more than Sending a test message with smtpClient.

    I reviewed your code, and a couple things stood out:

    -It looks like you are sending as an Alternate View.  I would like the web page to be the primary.  Since its internal, I know that all will be able to read the HTML.

    -You are using linked resources for the pictures.  In my case, would I still need to do this since it is internal and the site will be available

    -You attach an HTML.  Mine is ASPX that is dynamically created.

    Vicnent Fournier



  • Keyur

    yes You can.
    Send me a sample of what you are doing and then I can send you a sample of
    how to accomplish this.
    For starters, you can do something like



    using System;

    using System.Threading;

    using System.Net;

    using System.Net.Mail;

    using System.Net.Mime;

    using System.Net.Sockets;

    using System.Net.NetworkInformation;

    using System.Text;

    using System.Text.RegularExpressions;

    using System.Reflection;

    using System.IO;

    using System.Security;

    using System.Security.Permissions;

    using System.Xml;

    using System.Diagnostics;

    using System.Collections;

    using System.Collections.Generic;

    using System.Collections.Specialized;

    using System.Security.Principal;

    using System.Security.Policy;

    using Microsoft.Win32;

    public class MailTest

    {

    public static void Main()

    {

    try

    {

    //CREATE A MAIL CLIENT

    SmtpClient MailClient = new SmtpClient("localhost");

    //SET FROM, TO

    MailMessage Message = new MailMessage("<from>", "<TO>");

    //SET SUBJECT

    Message.Subject = "System.Net.Mail is easy";

    //LINKED RESOURCES

    LinkedResource expresssql = new LinkedResource("expresssql.jpg", "image/jpeg");

    expresssql.ContentLink = new Uri("http://lab.msdn.microsoft.com/vs2005/art/expresssql.jpg");

    LinkedResource expressvb = new LinkedResource("expressvb.jpg", "image/jpeg");

    expressvb.ContentLink = new Uri("http://lab.msdn.microsoft.com/vs2005/art/expressvb.jpg");

    LinkedResource expressvcplusplus = new LinkedResource("expressvcplusplus.jpg", "image/jpeg");

    expressvcplusplus.ContentLink = new Uri("http://lab.msdn.microsoft.com/vs2005/art/expressvcplusplus.jpg");

    LinkedResource expressvcsharp = new LinkedResource("expressvcsharp.jpg", "image/jpeg");

    expressvcsharp.ContentLink = new Uri("http://lab.msdn.microsoft.com/vs2005/art/expressvcsharp.jpg");

    LinkedResource expressvjsharp = new LinkedResource("expressvjsharp.jpg", "image/jpeg");

    expressvjsharp.ContentLink = new Uri("http://lab.msdn.microsoft.com/vs2005/art/expressvjsharp.jpg");

    LinkedResource expressvwd = new LinkedResource("expressvwd.jpg", "image/jpeg");

    expressvwd.ContentLink = new Uri("http://lab.msdn.microsoft.com/vs2005/art/expressvwd.jpg");

    //ADD Linked Resources

    AlternateView htmlBody = new AlternateView("VS2005.htm", "text/html");

    htmlBody.LinkedResources.Add(expresssql);

    htmlBody.LinkedResources.Add(expressvb);

    htmlBody.LinkedResources.Add(expressvcplusplus);

    htmlBody.LinkedResources.Add(expressvcsharp);

    htmlBody.LinkedResources.Add(expressvjsharp);

    htmlBody.LinkedResources.Add(expressvwd);

    //Add it to the Message

    Message.AlternateViews.Add(htmlBody);

    //ATTACHMENTS

    Message.Attachments.Add(new Attachment("TeamSystem.gif", "image/gif"));

    //SEND IT!!!

    MailClient.Send(Message);

    Console.WriteLine("Mail Successfully Sent");

    }

    catch(Exception e)

    {

    Console.WriteLine(e);

    }

    finally

    {

    }

    }

    }

     

     



     


  • Florian Kr&amp;#38;&amp;#35;252&amp;#59;sch

    I'm not sure I understand what you are recommending..

    Are you saying in Page1.aspx when the users clicks Button1 do the following:

    WebRequest MyWebRequest = HttpWebRequest.Create("Page1.aspx");

    Then do I send this to another page or do can I attach this to my message.body

    Could you provide a quick code snipit of what you are refering to



  • andreas-fsc

    Hi,

    This is a really good example of using linked resources -the best I've seen to date. You may be able to help me with a couple of questions...

    A couple of newbie questions....

    I've completed basic example ( based on system.net.mail) and this appears to work OK in Outlook but appears as an attachment in Yahoo, is there anyway around this.

    Can I use a script variable to format an address based on a variable passed from a calling page, e.g. I've a link from a product page that provides an image and a message, hence I'm sending the relevant image from the calling 'product' page.

    I understand that I could format a string, e,g, string "VS2005" send this as an email...

    At any rate I'd appreciate your help, - I am writing a e-Commerce Web Site and this looks like a really good technique to use.

    Kind Regards

    Bryan


  • AGSP

    I figured out another way to get the HTML of the page from the same page...

    While it does send the HTML in the body of the message, Outlook isnt rendering it.  What do I need to do from here to make outlook understand that it is HTML

    protected override void Render(HtmlTextWriter writer)

    {

    String webPage = null;

    StringBuilder sb = new StringBuilder();

    HtmlTextWriter newWriter = new HtmlTextWriter(new StringWriter(sb));

    base.Render(newWriter); //render the webpage

    webPage = sb.ToString(); //save the web page

    writer.Write(webPage); //make sure we return what was rendered back to the system

    if (btnSubmit.Visible==false)

    {

    //User Clicked Submit button

    SmtpClient smtpClient = new SmtpClient("localhost", 25);

    smtpClient.Host = "USEA-EXCH4.mycompany.com";

    smtpClient.Port = 25;

    smtpClient.UseDefaultCredentials = true;

    MailMessage mm = new MailMessage("me@mycompany.com", "manager@mycompany.com", "Request for Approval", webPage);

    smtpClient.Send(mm);

    }

    }



  • Sending a web page with SMTP