如何让用户向管理员发送电子邮件

时间:2018-03-31 14:57:47

标签: c# asp.net asp.net-mvc razor

我正在使用ASP.NET MVC进行项目。目前,我想创建一个页面,其中包含用户可以填写的引导表单,单击“发送”,然后将电子邮件发送到目标电子邮件。出于某种原因,我无法做到这一点。

这是我的Email.cshtml文件,向用户显示表单。询问他们的电子邮件,主题和消息。底部有一个发送按钮。



@{
    ViewData["Title"] = "Email";
}

<h2>Send Me An Email!</h2>


<form class="form-horizontal" method="post" action="ProcessFile.cshtml">
    <fieldset>

        <!-- Student email form-->
        <div class="form-group">
            <label class="col-md-2 control-label" for="textinput">Student Email</label>
            <div class="col-md-4">
                <input id="studentEmail" name="studentEmail" type="text" placeholder="Enter Here" class="form-control input-md" required="">

            </div>
        </div>

        <!-- Email subject form-->
        <div class="form-group">
            <label class="col-md-2 control-label" for="textinput">Subject</label>
            <div class="col-md-4">
                <input id="emailSubject" nameemailSubjectsubject" type="text" placeholder="" class="form-control input-md" required="">

            </div>
        </div>

        <!-- Message for email -->
        <div class="form-group">
            <label class="col-md-2 control-label" for="textarea">Message</label>
            <div class="col-md-4">
                <textarea class="form-control" id="emailMessage" name="emailMessage" style="width:600px; height:200px; resize: none;"></textarea>
            </div>
        </div>

        <!-- Send button -->
        <div class="form-group">
            <label class="col-md-2 control-label" for="sendbutton"></label>
            <div class="col-md-4">
                <input id="sendbutton" name="sendbutton" class="btn btn-danger" type="submit" value="Send">
            </div>
        </div>

    </fieldset>
</form>
&#13;
&#13;
&#13;

我将它重定向到名为ProcessFile.cshtml的页面。它的代码如下。出于某种原因,Request上下文未显示,并且在找不到上下文的情况下有一个红色下划线。我究竟做错了什么?我想要完成的工作是否有更简单的途径?

var studentEmail = Request["studentEmail"];
var emailSubject = Request["emailSubject"];
var emailMessage = Request["emailMessage"];

&#13;
&#13;
@using System.Web.Helpers;

@{
    var studentEmail = Request["studentEmail"];
    var emailSubject = Request["emailSubject"];
    var emailMessage = Request["emailMessage"];
    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";

        // Attach file and send email
        WebMail.Send(to: studentEmail, subject: emailSubject, body: emailMessage);
    }
    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>Dad</b>.</p>
    @if (errorMessage == "")
    {
        <p><b>Dad</b>, thank you for your interest.</p>
        <p>
            An email message has been sent to our dad service
            department
        </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>
&#13;
&#13;
&#13;

0 个答案:

没有答案