static void Main(string[] args) { try { // Envia default email con .NET new EmailNetService().SendAsync(EmailNetConfiguration.CreateDefault()).Wait(); // Envia custom email con .NET //new EmailNetService().SendAsync(new EmailNetConfiguration( //// Valores..... //)).Wait(); // Envia default email con SendGrid new EmailSendGridService().SendAsync(EmailSendGridConfiguration.CreateDefault()).Wait(); // Envia custom email con SendGrid //new EmailSendGridService().SendAsync(new EmailSendGridConfiguration( ////Valores..... //)).Wait(); } catch (ApplicationException ex) { Utils.Show("Ocurrio el siguiente error:" + ex.Message, false, true); } catch (Exception ex) { Utils.Show("Ocurrio un error inesperado:" + ex.Message, false, true); } Utils.Show("Oprima una tecla para terminar...", true, readLine: true); }
private static async Task <bool> SendByExchangeAsync(EmailNetConfiguration emailConfig) { Exchange.ExchangeService service = new Exchange.ExchangeService(emailConfig.EmailExchange.ExchangeVersion); service.Credentials = new NetworkCredential(emailConfig.Username, emailConfig.Password); if (emailConfig.EmailExchange.IsTraceEnabled) { service.TraceEnabled = true; service.TraceFlags = emailConfig.EmailExchange.TraceFlags; } service.AutodiscoverUrl(emailConfig.EmailAddress.FromEmail, RedirectionUrlValidationCallback); Exchange.EmailMessage message = new Exchange.EmailMessage(service); message.Subject = emailConfig.Subject; message.Body = new Exchange.MessageBody(emailConfig.Body.Value); emailConfig.To.ForEach(to => message.ToRecipients.Add(new Exchange.EmailAddress(to.FromEmail))); if (emailConfig.Bcc?.Count > 0) { emailConfig.Bcc.ForEach(bcc => message.BccRecipients.Add(new Exchange.EmailAddress(bcc.FromEmail))); } if (emailConfig.Cc?.Count > 0) { emailConfig.Cc.ForEach(cc => message.CcRecipients.Add(new Exchange.EmailAddress(cc.FromEmail))); } if (emailConfig.Attachement.IsAttachement) { Utils.IsDirectoryExistsThrowException(emailConfig.Attachement.AttachementPathDirectory); if (emailConfig.Zip.IsCompressed) { Utils.ZipFiles(emailConfig.Attachement.AttachementPathDirectory, emailConfig.Zip.ZipPathDirectory, emailConfig.Zip.IsDelete); var file = Utils.ReadAllBytes(emailConfig.Zip.ZipPathDirectory).FirstOrDefault(); message.Attachments.AddFileAttachment(file.filename); if (emailConfig.Zip.IsDelete) { Utils.DeleteFilesDirectory(emailConfig.Zip.ZipPathDirectory, true); } } else { foreach (var file in Utils.ReadAllBytes(emailConfig.Attachement.AttachementPathDirectory)) { message.Attachments.AddFileAttachment(file.filename); } } } Utils.Show($"\nEnviando email con {nameof(EmailNetService)}..."); await message.Save(); await message.SendAndSaveCopy(); return(await Task.FromResult(true)); }