Replace hardcoded SMTP credentials with SendGrid service

- Remove inline SMTP method with codevoir.com credentials
- Inject existing SendMessage helper that uses SendGrid via config
- Clean up unused using directives
This commit is contained in:
Adam Moussa 2026-04-16 16:25:11 -04:00
parent 0e3cb761c9
commit 486233076c

View file

@ -4,13 +4,11 @@ using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Net.Mail;
using System.Net;
using System.Security.Claims;
using System.Text;
using Api.SeaHavenIndustries.DTOs;
using Api.SeaHavenIndustries.Helper;
using Microsoft.EntityFrameworkCore;
using System.Diagnostics;
namespace Api.SeaHavenIndustries.Controllers
{
@ -21,12 +19,14 @@ namespace Api.SeaHavenIndustries.Controllers
private readonly UserManager<ApplicationUser> _userManager;
private readonly IConfiguration _configuration;
private readonly ApplicationDbContext _db;
private readonly SendMessage _sendMessage;
public AuthenticationController(UserManager<ApplicationUser> userManager, IConfiguration configuration, ApplicationDbContext db, IWebHostEnvironment webHostEnvironment, IHttpContextAccessor httpContext)
public AuthenticationController(UserManager<ApplicationUser> userManager, IConfiguration configuration, ApplicationDbContext db, SendMessage sendMessage)
{
_userManager = userManager;
_configuration = configuration;
_db = db;
_sendMessage = sendMessage;
}
[AllowAnonymous]
@ -150,7 +150,7 @@ namespace Api.SeaHavenIndustries.Controllers
_db.ForgetPasswordCodes.Add(forgetPasswordCode);
_db.SaveChanges();
string body = $"Your Password Reset Code is: " + forgetPasswordCode.Code;
SendEMail(user.Email, "Forget Password Request.", body);
await _sendMessage.SendEMail(user.Email, "Forget Password Request.", body);
return Ok(new Response { Status = "Success ", Message = "Please check your email for code" });
}
else
@ -211,38 +211,6 @@ namespace Api.SeaHavenIndustries.Controllers
}
}
[HttpGet()]
public bool SendEMail(string emailid, string subject, string body)
{
try
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress("infoesquall@codevoir.com"); //IMPORTANT: This must be same as your smtp authentication address.
mail.To.Add(emailid);
//set the content
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
//send the message
SmtpClient smtp = new SmtpClient("mail.codevoir.com");
//IMPORANT: Your smtp login email MUST be same as your FROM address.
NetworkCredential Credentials = new NetworkCredential("infoesquall@codevoir.com", "Abc123!@#");
smtp.UseDefaultCredentials = false;
smtp.Credentials = Credentials;
smtp.Port = 8889; //25 alternative port number is 8889
smtp.EnableSsl = false;
smtp.Send(mail);
return true;
}
catch (Exception ex)
{
return false;
}
}
private Random _random = new Random();
private string GenerateRandomNo()
{