using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Net.Mail; using System.Threading.Tasks; namespace Justice.Services { public class EmailService { private readonly string _smtpServer = "smtp.gmail.com"; private readonly int _smtpPort = 587; private readonly string _senderEmail = "amritsyangtan1@gmail.com"; private readonly string _senderPassword = "bdcx ycpy cbrr szpm"; // Map incident types to recipient emails private readonly Dictionary _incidentTypeToEmail; public EmailService() { _incidentTypeToEmail = new Dictionary { { "Accident", "ambulancemaiti@gmail.com" }, { "Crime", "crime-authority@example.com" }, { "Fire", "fire-department@example.com" }, { "Other", "general-authority@example.com" } }; } /// /// Gets the recipient email address based on the incident type. /// /// The type of the incident. /// The email address of the recipient. public string GetRecipientEmail(string incidentType) { return _incidentTypeToEmail.TryGetValue(incidentType, out var email) ? email : "amreitsyanf@gmail.com"; } /// /// Sends an email with an optional attachment. /// public async Task SendEmailWithAttachmentAsync(string recipientEmail, string subject, string body, string attachmentPath) { try { using (var smtpClient = new SmtpClient(_smtpServer, _smtpPort)) { smtpClient.Credentials = new NetworkCredential(_senderEmail, _senderPassword); smtpClient.EnableSsl = true; var mailMessage = new MailMessage { From = new MailAddress(_senderEmail), Subject = subject, Body = body, IsBodyHtml = true }; mailMessage.To.Add(recipientEmail); if (!string.IsNullOrWhiteSpace(attachmentPath) && File.Exists(attachmentPath)) { mailMessage.Attachments.Add(new Attachment(attachmentPath)); } await smtpClient.SendMailAsync(mailMessage); } Console.WriteLine("Email sent successfully."); return true; } catch (Exception ex) { Console.WriteLine($"Error sending email: {ex.Message}"); return false; } } } }