0

Hi I have a form set up on my page. How can I make it email my given email address?

<form action="#">
    <p>Please contact us</p>
    <input type="text" maxlength="30" value="Name" class="textcontact" />
    <input type="text" maxlength="30" value="E-mail Address" class="textcontact" />
    <input type="text" maxlength="30" value="Subject" class="textcontact" />
    <textarea name="message" id="message" cols="30" rows="10"></textarea>
    <input type="submit" value="" class="submit" />
</form>

I know it is something to do with form action. But there are no controllers or anything in webmatrix 2(?). How do I make it work?

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
Mcloving
  • 1,390
  • 1
  • 13
  • 30

2 Answers2

2

Here is a guide for doing exactly what you ask, from Webforms 2.

http://www.asp.net/web-pages/tutorials/email-and-search/11-adding-email-to-your-web-site

Create a new website.

Add a new page named EmailRequest.cshtml and add the following markup:

<!DOCTYPE html>
<html>
<head>
   <title>Request for Assistance</title>
</head>
<body>
  <h2>Submit Email Request for Assistance</h2>
  <form method="post" action="ProcessRequest.cshtml">
    <div>
        Your name:
        <input type="text" name="customerName" />
    </div>

    <div>
        Your email address:
        <input type="text" name="customerEmail" />
    </div>

    <div>
        Details about your problem: <br />
        <textarea name="customerRequest" cols="45" rows="4"></textarea>
    </div>

    <div>
        <input type="submit" value="Submit" />
    </div>
  </form>
</body>
</html>

Notice that the action attribute of the form element has been set to ProcessRequest.cshtml. This means that the form will be submitted to that page instead of back to the current page.

Add a new page named ProcessRequest.cshtml to the website and add the following code and markup:

@{
    var customerName = Request["customerName"];
    var customerEmail = Request["customerEmail"]; 
    var customerRequest = Request["customerRequest"];
    var errorMessage = "";
    var debuggingFlag = false;
    try {
        // Initialize WebMail helper
        WebMail.SmtpServer = "your-SMTP-host";
        WebMail.SmtpPort = 25;
        WebMail.UserName = "your-user-name-here";
        WebMail.Password = "your-account-password";
        WebMail.From = "your-email-address-here";

        // Send email
        WebMail.Send(to: customerEmail,
                subject: "Help request from - " + customerName,
            body: customerRequest
        );
    }
    catch (Exception ex ) {
        errorMessage = ex.Message;
    }
}
<!DOCTYPE html>
<html>
<head>
  <title>Request for Assistance</title>
</head>
<body>
  <p>Sorry to hear that you are having trouble, <b>@customerName</b>.</p>
    @if(errorMessage == ""){
      <p>An email message has been sent to our customer service
         department regarding the following problem:</p>
      <p><b>@customerRequest</b></p>
    }
    else{
        <p><b>The email was <em>not</em> sent.</b></p>
        <p>Please check that the code in the ProcessRequest page has 
           correct settings for the SMTP server name, a user name, 
           a password, and a "from" address.
        </p>
        if(debuggingFlag){
            <p>The following error was reported:</p>
            <p><em>@errorMessage</em></p>
        }
    }
</body>
</html>
IAmGroot
  • 13,760
  • 18
  • 84
  • 154
1

You could have the following WebPage and use the SmtpClient class to send an email:

@using System.Net.Mail;
@{
    if (IsPost)
    {
        var email = Request["Email"];       
        var subject = Request["Subject"];       
        var message = Request["Message"];       
        using (var client = new SmtpClient())
        {
             var msg = new MailMessage();
             msg.To.Add(email);
             msg.Subject = subject;
             msg.Body = message;
             client.Send(msg);
             <text>The email has been successfully sent</text>
         }
     }
}

<form action="" method="post">
    <p>Please contact us</p>
    <input type="text" name="email" maxlength="30" value="to@gmail.com" />
    <input type="text" name="subject" maxlength="30" value="Subject" />
    <textarea name="message" cols="30" rows="10"></textarea>
    <input type="submit" value="Send" class="submit" />
</form>

and in your web.config configure your SMTP server. Here's an example with Gmail:

<system.net>
    <mailSettings>
        <smtp from="youraccount@gmail.com">
            <network 
                host="smtp.gmail.com" 
                password="secret" 
                port="587" 
                userName="youraccount@gmail.com"
                enableSsl="true"
            />
        </smtp>
    </mailSettings>
</system.net>
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Does this avoid the need to have an SMTP on the IIS? I only have IIS Express installed which apparntly doesnt support it. It wont let me upvote these answers. – Mcloving Aug 19 '12 at 09:40
  • Yes, in my example I have configured Gmail's SMTP server to be used. You cannot upvote answers but you could mark them as answer by clicking on the tick next to it if they helped you solve the problem you were having. – Darin Dimitrov Aug 19 '12 at 09:41
  • I put my (gmail) email, in place of both `youraccount@gmail.com` and my pw in place of `secret`. But am recieving no emails. – Mcloving Aug 19 '12 at 09:57
  • Is an exception being thrown when you submit the form? When you put a breakpoint inside the send method does it get hit? – Darin Dimitrov Aug 19 '12 at 09:57
  • No. I have a try catch around it, where it would print off the expection. Like in doomsknights answer but it seems to work fine. How do you put breakpoints. I removed the isPost, and put it on a seperate page, so it sends you to new page. Again like doomsknight. I see `The email has been successfully sent` on the page after send. – Mcloving Aug 19 '12 at 09:58