c# mvc html.Beginform发送表单到gmail帐户
本文关键字:表单 gmail 帐户 mvc html Beginform | 更新日期: 2023-09-27 18:15:56
我做了一个主页,我想有一个表单,ppl可以填写,然后当点击提交有它发送到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");