using SeaHaven.Services.Interfaces; using SendGrid; using SendGrid.Helpers.Mail; namespace Api.SeaHavenIndustries.Helper { public class SendMessage : IEmailSender { private IConfiguration _configuration; private readonly ILogger _logger; //string keysapi = ""; public SendMessage(IConfiguration configuration, ILogger logger) { _configuration = configuration; _logger = logger; //keysapi = _configuration.GetValue("SendGrid:ApiKey"); } protected virtual ISendGridClient CreateClient(string? apiKey) => new SendGridClient(apiKey); /// /// SendGrid reports a rejected message through the status code, not an exception. /// Only the status is logged: never the recipient, subject or response body. /// private bool Delivered(Response response) { if (response.IsSuccessStatusCode) return true; _logger.LogWarning("SendGrid rejected an email with status {StatusCode}.", (int)response.StatusCode); return false; } private bool Failed(Exception ex) { _logger.LogWarning("SendGrid email send failed with {ExceptionType}.", ex.GetType().Name); return false; } public async Task SendEMail(string emailTo, 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(emailTo); //mail.Subject = subject; //mail.Body = body; //mail.IsBodyHtml = true; //SmtpClient smtp = new SmtpClient("mail.codevoir.com"); //NetworkCredential Credentials = new NetworkCredential("", ""); //smtp.UseDefaultCredentials = false; //smtp.Credentials = Credentials; //smtp.Port = 8889; //25 alternative port number is 8889 //smtp.EnableSsl = false; //smtp.Send(mail); //var apiKey = Environment.GetEnvironmentVariable("\r\n"); //var apiKey1 = Environment.GetEnvironmentVariable(""); // var apiKey2 = Environment.GetEnvironmentVariable("SENDGRID_API_KEY"); var apiKey = _configuration.GetValue("SendGrid:ApiKey"); //var apiKey = ""; var client = CreateClient(apiKey); var from = new EmailAddress("tech@seahavenind.com", "Sea haven Industries"); //var subject = "Sending with SendGrid is Fun"; var to = new EmailAddress(emailTo, ""); var apiKey3 = Environment.GetEnvironmentVariable("SendGrid:ApiKey"); var plainTextContent = ""; var htmlContent = body; var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent); var response = await client.SendEmailAsync(msg); return Delivered(response); } catch (Exception ex) { return Failed(ex); } } public async Task SendEmailAsync(string emailTo, string subject, string htmlBody) => await SendEMail(emailTo, subject, htmlBody); public async Task SendEMailAttachment(string emailTo, string subject, byte[] pdfBytes) { try { var apiKey = _configuration.GetValue("SendGrid:ApiKey"); //var apiKey = ""; var client = CreateClient(apiKey); var from = new EmailAddress("tech@seahavenind.com", "Sea haven Industries"); //var subject = "Sending with SendGrid is Fun"; var to = new EmailAddress(emailTo, ""); var apiKey3 = Environment.GetEnvironmentVariable("SendGrid:ApiKey"); var plainTextContent = "Please find the attached PDF"; var htmlContent = ""; var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, null); // Specify the content type for the attachment var attachment = new SendGrid.Helpers.Mail.Attachment { Content = Convert.ToBase64String(pdfBytes), Filename = "Workorder.pdf", Type = "application/pdf", // Use the correct MIME type for PDF Disposition = "attachment", ContentId = "Attachment" // Optional: ContentId for inline attachments }; msg.Attachments = new List { attachment }; var response = await client.SendEmailAsync(msg); return Delivered(response); } catch (Exception ex) { return Failed(ex); } } public async Task SendDispatchEmail(string emailTo, string subject, string htmlBody, string? replyTo) { try { var apiKey = _configuration.GetValue("SendGrid:ApiKey"); var client = CreateClient(apiKey); var from = new EmailAddress("tech@seahavenind.com", "Sea Haven Industries"); var to = new EmailAddress(emailTo, ""); var msg = MailHelper.CreateSingleEmail(from, to, subject, "", htmlBody); if (!string.IsNullOrWhiteSpace(replyTo)) msg.SetReplyTo(new EmailAddress(replyTo)); var response = await client.SendEmailAsync(msg); return Delivered(response); } catch (Exception ex) { return Failed(ex); } } } }