-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathSmartMassMailer.cs
100 lines (88 loc) · 3.66 KB
/
SmartMassMailer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Net;
using System.Net.Mail;
using Microsoft.Extensions.Configuration;
using System.IO;
using System.Text;
using OfficeOpenXml;
using System.Collections.Generic;
using System.Threading;
class SmartMassMailer
{
static IConfigurationRoot Configuration { get; set; }
static SmtpClient SmtpClient { get; set; }
static SmartMassMailer()
{
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json");
Configuration = builder.Build();
SmtpClient = new SmtpClient(Configuration["smtpSettings:host"])
{
UseDefaultCredentials = false,
Credentials = new NetworkCredential(
Configuration["smtpSettings:username"],
Configuration["smtpSettings:password"])
};
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
}
static void SendEmail(string recipientEmail, string subject, string bodyHtml)
{
var msg = new MailMessage
{
From = new MailAddress(
Configuration["fromEmail"],
Configuration["fromName"]),
IsBodyHtml = true,
Body = bodyHtml,
BodyEncoding = Encoding.UTF8,
SubjectEncoding = Encoding.UTF8,
HeadersEncoding = Encoding.UTF8,
Subject = subject
};
msg.To.Add(recipientEmail);
SmtpClient.Send(msg);
}
static void Main()
{
int delayBetweenEmails = int.Parse(
Configuration["delayBetweenEmailsMilliseconds"]);
int startRow = int.Parse(Configuration["startRow"]);
if (startRow < 2)
startRow = 2;
var excelFile = new FileInfo(Configuration["recipientsExcelFile"]);
if (! excelFile.Exists)
throw new FileNotFoundException($"The file '{excelFile.FullName}' does not exist.", excelFile.FullName);
using (var package = new ExcelPackage(excelFile))
{
ExcelWorksheet workSheet = package.Workbook.Worksheets[1];
int mailsLeft = workSheet.Dimension.End.Row - startRow;
TimeSpan timeLeft = TimeSpan.FromMilliseconds(
mailsLeft * (delayBetweenEmails + 100));
Console.WriteLine("Estimated time: {0} hours", timeLeft);
// Read Excel file column titles (the first worhsheet row)
var columnIndexByName = new Dictionary<string, int>();
for (int col = 1; col <= workSheet.Dimension.End.Column; col++)
{
string columnName = workSheet.Cells[1, col].Value
.ToString().ToLower();
columnIndexByName[columnName] = col;
}
// Process all rows from the Excel file --> send email for each row
for (int row = startRow; row <= workSheet.Dimension.End.Row; row++)
{
string email = (string) workSheet.Cells[row, columnIndexByName["email"]].Value;
string personName = (string) workSheet.Cells[row, columnIndexByName["name"]].Value;
string subject = Configuration["emailSubject"];
string bodyHtml = File.ReadAllText(
Configuration["emailHtmlTemplate"], Encoding.UTF8);
bodyHtml = bodyHtml.Replace("[name]", personName);
Console.Write($"[{row-1} of {workSheet.Dimension.End.Row-1}] Sending email to: {email} (Name = {personName}) ... ");
SendEmail(email, subject, bodyHtml);
Console.WriteLine("Done.");
Thread.Sleep(delayBetweenEmails);
}
Console.WriteLine();
Console.WriteLine("Done.");
}
}
}