C# mvc html.beginform 将表单发送到 gmail 帐户

C# mvc html.beginform send form to gmail account

我正在制作一个主页,其中我希望有一个表格供人们填写,然后在单击“提交”时将其发送到一个 gmail 帐户。我或多或少希望它显示为电子邮件,而无需他们填写自己的电子邮件地址。我以为只要从接收它的同一封电子邮件发送它就可以了。

问题是它不会真正进入低谷。

程序没问题,因为我可以看到我正确填写的所有信息,但是它不会发送邮件。

这是控制器:

public ActionResult InvitationResponseForm()
        {            
            return View();
        }


        [HttpPost]
        public ViewResult InvitationResponseForm(InvitationResponse model)
        {            
            if (ModelState.IsValid)
            {
                MailMessage response = new MailMessage();
                response.From = new MailAddress("sixtofjun@gmail.com");
                response.To.Add("sixtofjun@gmail.com");
                response.Subject = model.Name + " " + model.Surname;
                string Special = model.SpecialConditions;
                string PlusOne = model.PlusOneComment;
                bool OneOrTwo = model.PlusOne;
                response.Body = model.Name + " " + model.Surname + " " + OneOrTwo + "<br><br>" + Special + "<br>" + PlusOne;
                response.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient("smtp.gmail.com", 465);
                NetworkCredential basicAuthInfo = new NetworkCredential("sixtofjun@gmail.com", "My password");              
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = basicAuthInfo;
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.EnableSsl = true;
                return View("InvitationResponseForm", model);
            }
            else
            {
                var errors = ModelState.Values.SelectMany(v => v.Errors);
                return View();
            }

        }

以及部分观点:

<div class="form-group">
        @Html.LabelFor(model => model.PlusOneComment, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.PlusOneComment)
            @Html.ValidationMessageFor(model => model.PlusOneComment)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.SpecialConditions, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.SpecialConditions)
            @Html.ValidationMessageFor(model => model.SpecialConditions)
        </div>
    </div>       

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>
    </div>
</div>

为我即将举行的婚礼做这件事,真的很想得到一些帮助! 随时询问更多信息!

现在尝试了很多,但仍然没有成功。会不会跟我的ssl有关系?

您的 gmail smtp 端口错误。您使用 "smtp.EnableSsl = true;" 但仍使用 465 端口。我使用此代码,它正在发送电子邮件:

var client = new SmtpClient("smtp.gmail.com", 587)
{
    Credentials = new NetworkCredential("myusername@gmail.com", "mypwd"),
    EnableSsl = true
};
client.Send("myusername@gmail.com", "myusername@gmail.com", "test", "testbody");