Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<string> recipientEmails = new List<string>();
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 = @"
<html>
<head>
<style>
.header { background:#4CAF50;color:white;padding:10px;text-align:center; }
.content { margin:20px;font-family:Arial,sans-serif; }
.footer { background:#f1f1f1;color:#555;padding:10px;text-align:center;font-size:12px; }
</style>
</head>
<body>
<div class='header'><h1>Welcome to Our Newsletter</h1></div>
<div class='content'>
<p>Dear {{Name}},</p>
<p>We are excited to bring you the latest updates.</p>
<p>Best regards,<br/>The Team</p>
</div>
<div class='footer'>© 2026 Company Inc.</div>
</body>
</html>";

// 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}");
}
}
}
Loading