Я создал контактную форму, и я не получаю, я не знаю, почему я не могу получать электронные письма после того, как отправка здесь является моим контроллером, я новичок в asp.net, кстати, спасибовам очень нравится
namespace ContactForm.Controllers
{
public class ContactController : Controller
{
static string smtpServer = null;
static int? smtpPort;
static bool? enableSsl = null;
static string smtpUsername = null;
static string smtpPassword = null;
public ActionResult Index()
{
return View();
}
public ActionResult SendForm(string firstName, string email, string subject,
string message)
{
HttpStatusCodeResult result;
try
{
if (string.IsNullOrWhiteSpace(smtpServer))
smtpServer = ConfigurationManager.AppSettings.Get("ContactSMTPServer");
if (!smtpPort.HasValue)
smtpPort = int.Parse(ConfigurationManager.AppSettings.Get("ContactSMTPPort"));
if (!enableSsl.HasValue)
enableSsl = bool.Parse(ConfigurationManager.AppSettings.Get("ContactSMTPEnableSSL"));
if (string.IsNullOrWhiteSpace(smtpUsername))
smtpUsername = ConfigurationManager.AppSettings.Get("ContactSMTPUser");
if (string.IsNullOrWhiteSpace(smtpPassword))
smtpPassword = ConfigurationManager.AppSettings.Get("ContactSMTPPassword");
SmtpClient smtpClient = new SmtpClient(smtpServer, smtpPort.GetValueOrDefault());
smtpClient.EnableSsl = enableSsl.GetValueOrDefault();
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Credentials = new NetworkCredential(smtpUsername, smtpPassword);
smtpClient.Send(email, smtpUsername, string.Format("{0} - {1}", subject, firstName), message);
smtpClient.Dispose();
result = new HttpStatusCodeResult(HttpStatusCode.OK,
"Email successfully sent!");
}
catch (SmtpException)
{
result = new HttpStatusCodeResult(HttpStatusCode.InternalServerError,
"An error occurred while sending the email. Please try again later.");
}
catch (Exception)
{
result = new HttpStatusCodeResult(HttpStatusCode.InternalServerError,
"An error occurred. Please try again later.");
}
return result;
}
}
}
Просмотров
<!DOCTYPE HTML>
<html>
<body>
<header>
<div id="header" class="row">
<a id="title-navbar" class="navbar-brand" href="#">Example Site</a>
<button id="menu-btn" type="button" class="navbar-toggle" data-toggle="collapse" data-target="#contact-navbar">
Menu
</button>
</div>
>
</header>
<img id="banner-image" class="jumbotron" src="~/Images/banner.png" alt="Contact form banner" />
<div id="main-content" class="container">
<h1>Get in Touch</h1>
<p>
If you have any questions or comments, we'd love to hear from you. Just fill
out the form below and we'll get back to you soon. Rest assured that any
information you provide will remain confidential.
</p>
<form id="contact-form">
<div class="input-container">
<label for="first-name-input">First Name</label>
<div>
<input id="first-name-input" class="form-control" type="text" name="name" required />
<span class="glyphicon glyphicon-asterisk"></span>
</div>
</div>
<div class="input-container">
<label for="email-input">Email</label>
<div>
<input id="email-input" class="form-control" type="email" name="email" required novalidate />
<span class="glyphicon glyphicon-asterisk"></span>
</div>
</div>
<div class="input-container">
<label for="subject-input">Subject</label>
<div>
<input id="subject-input" class="form-control" type="text" name="subject" required />
<span class="glyphicon glyphicon-asterisk"></span>
</div>
</div>
<div class="input-container">
<label for="message-input">Message</label>
<span class="glyphicon glyphicon-asterisk"></span>
<textarea id="message-input" class="form-control" rows="10" name="message" maxlength="250" required></textarea>
</div>
<input id="submit-btn" type="submit" value="Submit" />
</form>
</div>
<div class="modal fade" id="alert-modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-body">
<div class="special-alert"></div>
<ul></ul>
</div>
<div class="modal-footer">
<button type="button" data-dismiss="modal">Okay</button>
</div>
</div>
</div>
</div>
<script type="text/javascript" src="~/Scripts/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="~/Scripts/bootstrap.min.js"></script>
<script type="text/javascript" src="~/Scripts/jquery.validate.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var validator = $('#contact-form').validate({
focusInvalid: false,
messages: {
name: "Please specify your name",
email: "We need a valid email address to contact you",
subject: "Please specify a subject for the email",
message: "Please include a message to be sent"
},
wrapper: 'li',
errorPlacement: function (error) {
var errorContainer = $('#alert-modal').find('ul');
if (errorContainer.html().indexOf(error.text()) < 0)
errorContainer.append(error);
}
});
$('#contact-form').on('submit', function (e) {
e.preventDefault();
var form = $(this);
var modal = $('#alert-modal');
if (!validator.form()) {
modal.modal('show');
}
else {
var data = {
"firstName": form.find('input[name="name"]').val().trim(),
"email": form.find('input[name="email"]').val().trim(),
"subject": form.find('input[name="subject"]').val().trim(),
"message": form.find('textarea').val().trim()
};
$.ajax({
method: 'POST',
cache: false,
contentType: 'application/json',
url: '@Url.Action("SendForm", "Contact")',
data: JSON.stringify(data),
success: function (data, jqXHR, textStatus) {
clearAlertModal();
console.log('data: ' + data + ' jqXHR: ' + jqXHR + ' textStatus: ' + textStatus);
if (textStatus.status == 200) {
form.find('input').val('');
form.find('textarea').val('');
}
modal.find('div.special-alert').html(textStatus.statusText);
modal.modal('show');
},
error: function (jqXHR, textStatus, errorThrown) {
clearAlertModal();
modal.find('div.special-alert').html(errorThrown);
modal.modal('show');
}
});
}
});
$('#alert-modal').on('hide.bs.modal', function () {
clearAlertModal();
});
var clearAlertModal = function () {
var modal = $('#alert-modal');
modal.find('ul').html('');
modal.find('div.special-alert').html('');
};
});
</script>
</body>
</html>'
и файл web.config
что я делаю не так?