added scaffolding for email sending methods
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
using CarCareTracker.Models;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Net;
|
||||
using System.Net.Mail;
|
||||
|
||||
namespace CarCareTracker.Controllers
|
||||
{
|
||||
@@ -25,6 +27,8 @@ namespace CarCareTracker.Controllers
|
||||
public IActionResult GenerateNewToken(string emailAddress)
|
||||
{
|
||||
var result = _loginLogic.GenerateUserToken(emailAddress);
|
||||
//send an email test block.
|
||||
SendEmail(emailAddress);
|
||||
return Json(result);
|
||||
}
|
||||
public IActionResult DeleteToken(int tokenId)
|
||||
@@ -32,5 +36,31 @@ namespace CarCareTracker.Controllers
|
||||
var result = _loginLogic.DeleteUserToken(tokenId);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,7 +83,8 @@ namespace CarCareTracker.Logic
|
||||
{
|
||||
Id = -1,
|
||||
UserName = credentials.UserName,
|
||||
IsAdmin = true
|
||||
IsAdmin = true,
|
||||
IsRootUser = true
|
||||
};
|
||||
}
|
||||
else
|
||||
|
||||
12
Models/Configuration/MailConfig.cs
Normal file
12
Models/Configuration/MailConfig.cs
Normal 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; }
|
||||
}
|
||||
}
|
||||
@@ -7,5 +7,6 @@
|
||||
public string EmailAddress { get; set; }
|
||||
public string Password { get; set; }
|
||||
public bool IsAdmin { get; set; }
|
||||
public bool IsRootUser { get; set; } = false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user