0

こんにちは私は自分のページにフォームを設定しています。指定したメールアドレスにメールを送信するにはどうすればよいですか?

<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>

私はそれがフォームアクションと関係があることを知っています。しかし、webmatrix 2(?)にはコントローラーなどはありません。どうすればそれを機能させることができますか?

4

2 に答える 2

2

これは、Webforms 2から、あなたが求めることを正確に行うためのガイドです。

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

新しいWebサイトを作成します。

EmailRequest.cshtmlという名前の新しいページを追加し、次のマークアップを追加します。

<!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>

form要素のaction属性がProcessRequest.cshtmlに設定されていることに注意してください。これは、フォームが現在のページに戻るのではなく、そのページに送信されることを意味します。

ProcessRequest.cshtmlという名前の新しいページをWebサイトに追加し、次のコードとマークアップを追加します。

@{
    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>
于 2012-08-19T09:24:45.567 に答える
1

次のWebページを作成し、 SmtpClientクラスを使用して電子メールを送信できます。

@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>

web.configでSMTPサーバーを構成します。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>
于 2012-08-19T09:35:57.353 に答える