laatzen/LaaProductionWeb/PendingEmails/Program.cs
2024-07-01 10:28:12 +02:00

160 lines
5.9 KiB
C#

namespace PendingEmails
{
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Threading;
using System.Timers;
using SystemTimersTimer = System.Timers.Timer;
class Program
{
static readonly SystemTimersTimer timer = new SystemTimersTimer();
static string logfile = $@"C:\Temp\PendingEmails\emailversand_{DateTime.Now:yyyyMMdd}.txt";
static string connectionString = "Server=SLASQL01.emea.sensus.net; Database=Auftrag; User ID=ServiceParingfile; Password=ServiceParingfile;";
static void Main()
{
try
{
timer.AutoReset = true;
timer.Interval = 300_000;
timer.Elapsed += Timer_Elapsed;
while (true)
{
Timer_Elapsed(null, null);
timer.Start();
var awaiter = new ManualResetEventSlim();
awaiter.Wait();
timer.Stop();
}
}
catch (Exception e)
{
Console.WriteLine($"{Environment.NewLine}{e}{Environment.NewLine}");
File.AppendAllText(logfile, $"{Environment.NewLine}{e}{Environment.NewLine}");
}
}
private static void Timer_Elapsed(object _, ElapsedEventArgs _1)
{
logfile = $@"C:\Temp\PendingEmails\emailversand_{DateTime.Now:yyyyMMdd}.txt";
Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] PREPEARING EMAILS TO SEND ...");
File.AppendAllText(logfile, $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] PREPEARING EMAILS TO SEND ... {Environment.NewLine}");
using (var connection = new SqlConnection(connectionString))
{
connection.Open();
var updated = new List<int>();
using (var command = connection.CreateCommand())
{
command.CommandText = @"
SELECT [Sender]
, [Subject]
, [Body]
, [Recipients]
, [CC]
, [Id]
FROM [Messages]
WHERE [StationNr] >= 2000
AND [OutDate] IS NULL
ORDER BY [InDate] DESC";
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
try
{
var email = new MailMessage();
email.From = new MailAddress("Stoyan.Zlatev@xylem.com");
email.Subject = $"{reader.GetValue(1)}";
email.Body = $"{reader.GetValue(2)}";
$"{reader.GetValue(3)}"
.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => new MailAddress(x))
.ToList()
.ForEach(email.To.Add);
$"{reader.GetValue(4)}"
.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => new MailAddress(x))
.ToList()
.ForEach(email.CC.Add);
email.CC.Add(new MailAddress("Stoyan.Zlatev@xylem.com"));
if (SendEmail(email))
{
updated.Add(reader.GetInt32(5));
}
}
catch (Exception e)
{
File.AppendAllText(logfile, $"{Environment.NewLine}{e}{Environment.NewLine}");
}
}
}
}
if (updated.Count > 0)
{
try
{
File.AppendAllText(logfile, $"UPDATEING: {string.Join(", ", updated)}{Environment.NewLine}");
using (var command = connection.CreateCommand())
{
command.CommandText = $@"
UPDATE [Messages]
SET [OutDate] = GETDATE()
WHERE [Id] IN ({string.Join(", ", updated)})";
command.ExecuteNonQuery();
}
}
catch (Exception e)
{
File.AppendAllText(logfile, $"{Environment.NewLine}{e}{Environment.NewLine}");
}
}
else
{
File.AppendAllText(logfile, $"... NO PENDING EMAILS FOUND {Environment.NewLine}");
}
}
}
private static bool SendEmail(MailMessage email)
{
try
{
using (var smtpClient = new SmtpClient("smtp.xylem.com", 587))
{
smtpClient.Timeout = 5000;
smtpClient.UseDefaultCredentials = true;
smtpClient.Send(email);
}
return true;
}
catch (Exception e)
{
File.AppendAllText(logfile, $"{Environment.NewLine}{e}{Environment.NewLine}");
}
return false;
}
}
}