Pages

Search Code Shode

Tuesday, July 26, 2011

How to Email a Web Page As HTML in ASP.NET using C# VB.NET

This post explains How to Email a Web Page As HTML From Your Site or in fact any web page you want (though i would not recommend any with lots of scripts etc in it). It is very useful for email forms to customers etc from your site.

Personally I create an plainer version of the form just for email. I remove any menu functions and other things that are there as part of the website and are not useful in the email - but that is a personal preference.


I have already written a couple of posts regarding How to Send Email in ASP.NET. You can check them here:




However, to keep things simple, this is how an email could be send:

C#



using System.Net.Mail;

public static class Emailer
{
    public static void SendEmail(string from, string to, string subject, string body)
    {
        SmtpClient smtp = new SmtpClient("hostname");
        MailMessage email = new MailMessage(from, to, subject, body);
        email.IsBodyHtml = true;
        smtp.Send(email);
    }
}

VB.NET

Imports System.Net.Mail

Public NotInheritable Class Emailer
    Private Sub New()
    End Sub
    Public Shared Sub SendEmail(from As String, [to] As String, subject As String, body As String)
        Dim smtp As New SmtpClient("hostname")
        Dim email As New MailMessage(from, [to], subject, body)
        email.IsBodyHtml = True
        smtp.Send(email)
    End Sub
End Class


Now to send the page. Remember it can be any page and so you can do any code behind processing first that you wish. In essence we call Server.Execute which runs the page as if it were in a browser, but without opening the page in the browser for the user to see. The lines of code are simple as follows:

C#
using System.Net;

using System.IO:

protected void btnSendEmail_Click(object sender, EventArgs e)
{
    ////*** somewhere in an aspx page - preferably a different one to the one you are emailing but doesnt have to be******
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    Server.Execute("YourWebPage.aspx", htw); // remember, it could be any web page
    //the page has now processed and the markup is in the textwriter inner text which is the stringwriter
    //I also set a status in the called aspx page and check to see all was ok before continuing but will keep it simple here
    Emailer.SendEmail("from@here.com", "to@there.com", "Your aspx page", sw.ToString());
}

VB.NET

  
Imports System.Net
Imports System.IO

    Protected Sub btnSendEmail_Click(sender As Object, e As EventArgs)
        '''/*** somewhere in an aspx page - preferably a different one to the one you are emailing but doesnt have to be******
        Dim sw As New StringWriter()
        Dim htw As New HtmlTextWriter(sw)
        Server.Execute("YourWebPage.aspx", htw)
        ' remember, it could be any web page
        'the page has now processed and the markup is in the textwriter inner text which is the stringwriter
        'I also set a status in the called aspx page and check to see all was ok before continuing but will keep it simple here
        Emailer.SendEmail("from@here.com", "to@there.com", "Your aspx page", sw.ToString())
    End Sub


You could pass parameters in the header of Server.Execute if needed or anything just as calling a normal aspx page. It you wanted to call a third party page instead then you cant use Response.Redirect.

Remember if you want to place the code above into the page you want to email then do not put it in the page load without some form of checking (say a flag) otherwise you will go into an never-ending loop (infinite loop) and overflow error.

اشتہارات