diff --git a/general/create-a-fancy-html-email-template-and-send-it-to-multiple-receivers-dynamically-fetched-from-an-excel-sheet.cs b/general/create-a-fancy-html-email-template-and-send-it-to-multiple-receivers-dynamically-fetched-from-an-excel-sheet.cs new file mode 100644 index 00000000..1e234c95 --- /dev/null +++ b/general/create-a-fancy-html-email-template-and-send-it-to-multiple-receivers-dynamically-fetched-from-an-excel-sheet.cs @@ -0,0 +1,130 @@ +using System; +using System.IO; +using System.Collections.Generic; +using Aspose.Email; +using Aspose.Email.Clients.Smtp; + +class Program +{ + static void Main() + { + try + { + // Paths for the template and the recipients list + const string recipientsPath = "recipients.csv"; + + // Ensure the recipients file exists; create a minimal placeholder if missing + try + { + if (!File.Exists(recipientsPath)) + { + var placeholderLines = new[] + { + "john.doe@example.com", + "jane.smith@example.com" + }; + File.WriteAllLines(recipientsPath, placeholderLines); + } + } + catch (Exception ex) + { + Console.Error.WriteLine($"Failed to prepare recipients file: {ex.Message}"); + return; + } + + // Read recipient email addresses from the CSV file + List recipientEmails = new List(); + try + { + using (StreamReader reader = new StreamReader(recipientsPath)) + { + string line; + while ((line = reader.ReadLine()) != null) + { + string email = line.Trim(); + if (!string.IsNullOrEmpty(email)) + { + recipientEmails.Add(email); + } + } + } + } + catch (Exception ex) + { + Console.Error.WriteLine($"Failed to read recipients: {ex.Message}"); + return; + } + + if (recipientEmails.Count == 0) + { + Console.Error.WriteLine("No recipient addresses found."); + return; + } + + // Create a fancy HTML email template + const string htmlTemplate = @" + + + + + +

Welcome to Our Newsletter

+
+

Dear {{Name}},

+

We are excited to bring you the latest updates.

+

Best regards,
The Team

+
+ + + "; + + // Build the MailMessage (using a generic placeholder name for the sender) + MailMessage message = new MailMessage(); + message.From = new MailAddress("sender@example.com", "Sender Name"); + message.Subject = "Your Monthly Update"; + message.HtmlBody = htmlTemplate; + + // Add all recipients to the To collection + foreach (string email in recipientEmails) + { + message.To.Add(new MailAddress(email)); + } + + // Prepare SMTP client (placeholder credentials) + const string smtpHost = "smtp.example.com"; + const int smtpPort = 587; + const string smtpUser = "username"; + const string smtpPass = "password"; + + // Guard against executing real network calls with placeholder data + if (smtpHost.Contains("example.com")) + { + Console.WriteLine("Placeholder SMTP settings detected. Skipping actual send."); + return; + } + + // Send the email + try + { + using (SmtpClient client = new SmtpClient(smtpHost, smtpPort, smtpUser, smtpPass)) + { + client.Send(message); + } + Console.WriteLine("Email sent successfully to all recipients."); + } + catch (Exception ex) + { + Console.Error.WriteLine($"Failed to send email: {ex.Message}"); + return; + } + } + catch (Exception ex) + { + Console.Error.WriteLine($"Unexpected error: {ex.Message}"); + } + } +}