Afternoon,
I'm doing an app in which one of the modules includes sending email from the ASP.Net MVC app.
Here's what I already have :
var body = "<p>Email From: {0} ({1})</p><p>Message: Morning. This is a test trying sending emails from ASP.Net MVC. Thanks to tell me if you receive the message. </p><p>{2}</p>";
var message = new MailMessage();
message.To.Add(new MailAddress("nice55kloe@mail.com"));
message.From = new MailAddress(model.FromEmail);
message.Subject = "This is a test trying sending emails with ASP.Net MVC";
message.Body = string.Format(body, model.FromName, model.FromEmail, model.Message);
message.IsBodyHtml = true;
using (var smtp = new SmtpClient())
{
var credential = new NetworkCredential
{
UserName = "myemail@gmail.com",
Password = "password"
};
smtp.Credentials = credential;
smtp.Host = "smtp-mail.outlook.com";
smtp.Port = 465;
smtp.EnableSsl = true;
await smtp.SendMailAsync(message);
return RedirectToAction("Sent");
}
}
return View(model);
HTML form
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<h4>Send your comments.</h4>
<hr />
<div class="form-group">
@Html.LabelFor(m => m.FromName, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.FromName, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.FromName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.FromEmail, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.FromEmail, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.FromEmail)
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Message, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextAreaFor(m => m.Message, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Message)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" value="Send" />
</div>
</div>
}
What I expect is to get the email sent once I fill the form and click on the send button, but actually, once I click on the send button, the page loads forever without returning any result.I have no idea where the problem lies.
Any help will be greatly welcomed.
Thanks in advance!
What I have tried:
Adding breakpoints at some lines in controller, but nothing seems to get it going or even display some error.