Commit: Contact Honeypot
This commit is contained in:
@@ -3,6 +3,7 @@ using System.Net;
|
||||
using clivelancaster.Models;
|
||||
using MailKit.Net.Smtp;
|
||||
using MimeKit;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace clivelancaster.Controllers
|
||||
{
|
||||
@@ -31,32 +32,40 @@ namespace clivelancaster.Controllers
|
||||
|
||||
var bodyBuilder = new BodyBuilder
|
||||
{
|
||||
TextBody = $"Name: {model.Name}\nEmail: {model.Email}\n\nSubject: {model.Subject}\n\nMessage:\n{model.Message}"
|
||||
TextBody = $"Name: {model.NameIncognito}\nEmail: {model.Email}\n\nSubject: {model.SubjectIncognito}\n\nMessage:\n{model.MessageIncognito}"
|
||||
};
|
||||
|
||||
message.Body = bodyBuilder.ToMessageBody();
|
||||
|
||||
try
|
||||
// Honeypot Check
|
||||
if (model.Name != null | model.Subject != null | model.Message != null)
|
||||
{
|
||||
using (var client = new SmtpClient())
|
||||
{
|
||||
// Retrieve SMTP settings from appsettings.json
|
||||
var smtpHost = _configuration["SmtpSettings:Host"];
|
||||
int smtpPort = int.Parse(s: _configuration["SmtpSettings:Port"]);
|
||||
var smtpUsername = _configuration["SmtpSettings:Username"];
|
||||
var smtpPassword = _configuration["SmtpSettings:Password"];
|
||||
|
||||
client.Connect(smtpHost, smtpPort, true);
|
||||
client.Authenticate(smtpUsername, smtpPassword);
|
||||
await client.SendAsync(message);
|
||||
client.Disconnect(true);
|
||||
}
|
||||
|
||||
ViewBag.Message = "Message sent successfully!";
|
||||
}
|
||||
catch
|
||||
else
|
||||
{
|
||||
ViewBag.Message = "Error sending message.";
|
||||
try
|
||||
{
|
||||
using (var client = new SmtpClient())
|
||||
{
|
||||
// Retrieve SMTP settings from appsettings.json
|
||||
var smtpHost = _configuration["SmtpSettings:Host"];
|
||||
int smtpPort = int.Parse(s: _configuration["SmtpSettings:Port"]);
|
||||
var smtpUsername = _configuration["SmtpSettings:Username"];
|
||||
var smtpPassword = _configuration["SmtpSettings:Password"];
|
||||
|
||||
client.Connect(smtpHost, smtpPort, true);
|
||||
client.Authenticate(smtpUsername, smtpPassword);
|
||||
await client.SendAsync(message);
|
||||
client.Disconnect(true);
|
||||
}
|
||||
|
||||
ViewBag.Message = "Message sent successfully!";
|
||||
}
|
||||
catch
|
||||
{
|
||||
ViewBag.Message = "Error sending message.";
|
||||
}
|
||||
}
|
||||
|
||||
return View();
|
||||
|
||||
Reference in New Issue
Block a user