Programing a app to send massive emails

I know the following is not an app (app mobile, app web,.. ) it is a little program to PC, but in this days I’m not sure how to say it, whatever.

In my work we needed a way to send emails from a list, but we didn’t not want to use an external service like mailchimp for example, then we need a simple way to send those emails.

The requeriments

The email’s list will be in csv format, looks like this:

email1@email.com,Jon Doe

emial2@email.com, Rick Doe

Why need two fields? because we need into the email a greeting, for example:

Hi Jon Doe

Loremp ipsum

So each email will have the name of the person.

The mails should be sended with the credentials of the user who at that moment send this emails

In this case I thought in json files, so my format looks like this:

{
 "From": "genitalico",
 "Email": "myemail@mycompany.com",
 "Password": "password",
 "SmtpServer": "smtp.server.com",
 "Port": 587,
 "Ssl": 0
}

if you see, I need specific properties for configuring the sender.

We need a template to email

Why do I need a template? well because we need automatize the text to send, in this case the text should be html format since the email have to have a format nice.

Here a template example, is very simple:

<h1>Hi , ##Name</h1>

<p>Lorem ... </p>
Why does my template have “##Name”? well, I need a specific word that I can replace with the specific name of the person.
Finally, I need a “Subject” to the email
I just add this to my json file, now it looks like this:
{

"From": "genitalico",

"Email": "myemail@mycompany.com",

"Password": "password",

"SmtpServer": "smtp.server.com",

"Port": 587,

"Ssl": 0,

"Subject": "subject email"

}
Now I have the settings ready.
The program’s flow have to be:
  1. Read csv file
  2. Read json file
  3. Read template
  4. Send each email from the csv list

if you see, I have not said anything about of the technology or language that I could be use, in my case I used C# and NetCore.

Of course you can build the program with any technology that meets the requeriments.

Why do I use those technologies? because I use Linux, Windows and Mac, then I need execute it on anywhere system, I also had open my Visual Studio at that time.

How to works the program?

Read Csv File, that is easy.

var linesFile = System.IO.File.ReadAllLines(filePath);
foreach (var line inlinesFile)

{

string[] values =line.Split(',');

}

 

The above maybe it doesn’t have sentive now, but I’m going to leave the github link of the full code.

Read Json file, also it is easy
var textFile =System.IO.File.ReadAllText(filePath);

var model = JsonConvert.DeserializeObject<JsonModel>(textFile);
For this case I use the library NewtonJson
Now that I have a json data in the memory (of code), I need send the email.
Send Emails with MailKit

MailKit is a cross-platform mail client library built on top of MimeKit.

This library is easy to use, in the github web page there is an example to send email.

Finally I built the program to execute in the console, the writing code to obtain parameters, the parameters are:

-c file.csv Read csv file

-j file.json Read json file

-t template.html Read html file

In the shell write something like this:

MassiveEmailSend -c file.csv -j file.json -t template.html

Well this post is getting long so I just write the link to github, you can review the code, copy and send me improvements.

Note: you don’t use this program to send SPAM, I don’t take responsability with it

 

GitHub Link

Happy Coding!!

Leave a Reply