Karthik Dheeraj
Karthik Dheeraj

Reputation: 1049

Sending Email from HTML Using ASP.Net

I have developed a website using HTML pages in VisualStudio2012 ASP.Net empty website template. In my web site I looking for Contact us page, which will send an email. Is it Possible to send email using ASP.Net from .HTML PAGE?

I am aware of sending email from .ASPX pages which may contain either ASP controls or HTML controls using ASP.Net.

Here I am looking for, to send email from .HTML Pages only.

To be more specific, I have sendemail() method to which I need to pass email address and email content from a .HTMl page. How can it be achievable ?

Thanks in advance.

Upvotes: 2

Views: 2266

Answers (2)

Avitus
Avitus

Reputation: 15958

there are a couple different ways you can do this:

  1. You can modify the action of your form to point to a .aspx page and then in the code behind of that page you can catch the request and send the email from there. ( this would be similar to sending to a HTTP handler ). The http handler would give you greater performance than the .aspx page.

  2. You can perform an ajax call on submit of your form that will then pass the form data to a web method that would then send the email. This will require you to create an .asmx service to be called from the javascript.

Upvotes: 3

Lloyd
Lloyd

Reputation: 29668

Personally I would have the form POST to a HTTP handler (they end in .ashx) which does the processing in ASP.NET. From this handler you can read in the passed fields from the form and send an e-mail via your SendEmail() method.

For example, in your HTML:

<form action="email.ashx" method="POST">
    <input type="text" name="email" />
    <input type="text" name="content" />
    <button type="submit">Submit</button>
</form>

Then a HTTP handler:

public class EmailHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
       string email = context.Request.Form["email"];
       string content = context.Request.Form["content"];

       SendEmail(email,content);
    }

    private void SendEmail(string address, string content)
    {
        ...
    }

}

This is very rough but gives you a little more to go on.

Upvotes: 5

Related Questions