using sib_api_v3_sdk.Api; using sib_api_v3_sdk.Client; using sib_api_v3_sdk.Model; using System; using System.Collections.Generic; using System.Linq; namespace MisIngredientesVue.Core.Services { public interface IEmailService { void SendEmail(bool includeAds, string from, string[] to, string subject, string body, bool isBodyHtml = false, params MailAttachment[] attachments); } public class EmailService : IEmailService { public void SendEmail(bool includeAds, string from, string[] to, string subject, string body, bool isBodyHtml = false, params MailAttachment[] attachments) { //Console.WriteLine("----------------\n\n\n" + body); //return; if (to != null) to = to.Where(x => x.Contains("@")).Select(x => x.ToLower().Trim()).ToArray(); var apiKey = "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"; // Configure API key authorization: api-key Configuration.Default.AddApiKey("api-key", apiKey); // Uncomment below to setup prefix (e.g. Bearer) for API key, if needed // Configuration.Default.AddApiKeyPrefix("api-key", "Bearer"); // Configure API key authorization: partner-key Configuration.Default.AddApiKey("partner-key", apiKey); // Uncomment below to setup prefix (e.g. Bearer) for API key, if needed // Configuration.Default.AddApiKeyPrefix("partner-key", "Bearer"); if (isBodyHtml && includeAds) { body += $@"

Este correo fue generado por Mis Ingredientes. Visite el sitio web para conocer más: https://misingredientes.mx

"; } var apiInstance = new SMTPApi(); var sendSmtpEmail = new SendSmtpEmail( new SendSmtpEmailSender("Mis Ingredientes", from), to.Select(x => new SendSmtpEmailTo(x)).ToList(), null, null, isBodyHtml ? body : null, isBodyHtml ? null : body, subject); if (attachments != null && attachments.Length > 0) { sendSmtpEmail.Attachment = new List(); foreach (var attachment in attachments) { sendSmtpEmail.Attachment.Add(new SendSmtpEmailAttachment() { Content = attachment.Data, Name = attachment.Name }); } } try { // Send a transactional email CreateSmtpEmail result = apiInstance.SendTransacEmail(sendSmtpEmail); } catch (Exception e) { Console.WriteLine($"From: {from}, To: {(to != null ? string.Join(", ", to) : "")}. Error al enviar correo electrónico: {e.Message}" + e.Message); } } //public void SendEmail(string from, string[] to, string subject, string body, bool isBodyHtml = false, params MailAttachment[] attachments) //{ // if (to != null) // to = to.Where(x => x.Contains("@")).ToArray(); // var apiKey = "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"; // var client = new SendGridClient(apiKey); // var fromEmailAddress = new EmailAddress(from); // var toEmailAddress = to.Select(email => new EmailAddress(email)).ToList(); // var messageToSend = toEmailAddress.Count == 1 ? // MailHelper.CreateSingleEmail(fromEmailAddress, toEmailAddress[0], subject, isBodyHtml ? string.Empty : body, isBodyHtml ? body : string.Empty) : // MailHelper.CreateSingleEmailToMultipleRecipients(fromEmailAddress, toEmailAddress, subject, isBodyHtml ? string.Empty : body, isBodyHtml ? body : string.Empty); // if (attachments != null) // { // var attachmentsToAdd = new List(); // foreach (var a in attachments) // { // if (a.Data != null) // { // var attachment = new SendGrid.Helpers.Mail.Attachment(); // attachment.Content = Convert.ToBase64String(a.Data); // attachment.Filename = a.Name; // attachmentsToAdd.Add(attachment); // } // } // if (attachmentsToAdd.Count > 0) // messageToSend.AddAttachments(attachmentsToAdd); // } // var response = client.SendEmailAsync(messageToSend); // response.Wait(); // var stringResponse = response.Result.Body.ReadAsStringAsync(); // stringResponse.Wait(); //} } public class MailAttachment { public string Name { get; set; } public byte[] Data { get; set; } } }