我正在创建一个主页,我想要一个人们可以填写的表单,然后点击提交时将其发送到一个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有关吗?
答案 0 :(得分:0)
您的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");