2

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.

3
  • Why are you using HTML pages in ASP.NET? Commented Sep 4, 2013 at 15:09
  • 2
    @PaulGrimshaw Nothing wrong with that really, we do and have the pages call HTTP handlers. Commented Sep 4, 2013 at 15:09
  • My intention to use HTML pages in ASP.Net is to do server side scripting to send Email only. Commented Sep 4, 2013 at 15:25

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

It worked me. I have another query with regards to HTTPHandler. I am putting SendEmail(email,content) method in try catch blocks to handle exceptions that may arise. as try{ SendEmail(email,content); } catch(exception ex){}.. now how can I pass exception message back to html page ?
3

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.

1 Comment

Can You please through some more light on the first point u mentioned above ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.