登录用户的电子邮件地址并将其传递到电子邮件中

时间:2017-08-24 05:59:14

标签: c# asp.net-mvc entity-framework

我有一个MVC应用程序,允许客户登录并提交他们遇到的任何查询,他们输入他们的名字并描述他们的问题。然后通过我们的服务器电子邮件将此查询发送到我们的支持电子邮件地址,这样做我需要将用户登录的电子邮件地址包含在电子邮件中作为字符串。我尝试过几种方法但遇到运行时错误,因为电子邮件字段返回null。

我的控制器:这是我的电子邮件控制器方法

  Email.Body = $"<p><b>Name:</b> {support.UserNameSurname}</p> <p><b>Email:</b> {support.Email=User.Identity.GetUserName()}</p> <p><b>Message:</b> {support.SupportMessage}</p>";

视图

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="col-sm-offset-3 col-sm-6">
        <div class="form-horizontal">

            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            

            <div class="form-group">
                @Html.LabelFor(model => model.UserNameSurname, "Full Name" , htmlAttributes: new { @class = "control-label col-md-3" })
                <div class="col-md-9">
                    @Html.EditorFor(model => model.UserNameSurname, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.UserNameSurname, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.SupportMessage,"Describe Issue", htmlAttributes: new { @class = "control-label col-md-3" })
                <div class="col-md-9">
                    @Html.TextAreaFor(model => model.SupportMessage, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.SupportMessage, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Request  Help" class="btn btn-default" style="background-color: #0a9dbd;-webkit-text-fill-color: white; border-color: #0a9dbd; height: 40px; float:right;" />
                </div>
                <div>
                    @Html.ActionLink("Back to List", "CustomerView")
                </div>
            </div>
        </div>
    </div>
}

1 个答案:

答案 0 :(得分:0)

如果我必须把我写的答案作为答案,你可以去

public static string GetUserEmail(this HttpSessionState session)
{
    if(Session["Email"] != null)
      {
          Session["email"] = ..Set Session email here
      }

    return session["Email"]
}

然后你可以创建一个集合会话方法,或者只使用Session [&#34; key&#34;]来设置值,然后你就可以访问你的电子邮件了。