added scaffolding for email sending methods

This commit is contained in:
DESKTOP-GENO133\IvanPlex
2024-01-13 11:12:37 -07:00
parent 03b89786ec
commit c9d60910e5
4 changed files with 45 additions and 1 deletions

View File

@@ -2,6 +2,8 @@
using CarCareTracker.Models; using CarCareTracker.Models;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.Net;
using System.Net.Mail;
namespace CarCareTracker.Controllers namespace CarCareTracker.Controllers
{ {
@@ -25,6 +27,8 @@ namespace CarCareTracker.Controllers
public IActionResult GenerateNewToken(string emailAddress) public IActionResult GenerateNewToken(string emailAddress)
{ {
var result = _loginLogic.GenerateUserToken(emailAddress); var result = _loginLogic.GenerateUserToken(emailAddress);
//send an email test block.
SendEmail(emailAddress);
return Json(result); return Json(result);
} }
public IActionResult DeleteToken(int tokenId) public IActionResult DeleteToken(int tokenId)
@@ -32,5 +36,31 @@ namespace CarCareTracker.Controllers
var result = _loginLogic.DeleteUserToken(tokenId); var result = _loginLogic.DeleteUserToken(tokenId);
return Json(result); return Json(result);
} }
private bool SendEmail(string emailAddress)
{
var mailConfig = new MailConfig();
string to = emailAddress;
string from = mailConfig.EmailFrom;
var server = mailConfig.EmailServer;
MailMessage message = new MailMessage(from, to);
message.Subject = "Using the new SMTP client.";
message.Body = @"Using this new feature, you can send an email message from an application very easily.";
SmtpClient client = new SmtpClient(server);
client.EnableSsl = mailConfig.UseSSL;
client.Port = mailConfig.Port;
client.Credentials = new NetworkCredential(mailConfig.Username, mailConfig.Password);
try
{
client.Send(message);
return true;
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in CreateTestMessage2(): {0}",
ex.ToString());
return false;
}
}
} }
} }

View File

@@ -83,7 +83,8 @@ namespace CarCareTracker.Logic
{ {
Id = -1, Id = -1,
UserName = credentials.UserName, UserName = credentials.UserName,
IsAdmin = true IsAdmin = true,
IsRootUser = true
}; };
} }
else else

View File

@@ -0,0 +1,12 @@
namespace CarCareTracker.Models
{
public class MailConfig
{
public string EmailServer { get; set; }
public string EmailFrom { get; set; }
public bool UseSSL { get; set; }
public int Port { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
}

View File

@@ -7,5 +7,6 @@
public string EmailAddress { get; set; } public string EmailAddress { get; set; }
public string Password { get; set; } public string Password { get; set; }
public bool IsAdmin { get; set; } public bool IsAdmin { get; set; }
public bool IsRootUser { get; set; } = false;
} }
} }