#r System.Data.Common.dll
#r System.Data.dll
#r System.Net.dll
#r System.Net.Mail.dll
#r System.ComponentModel.TypeConverter.dll
#r System.Net.Primitives.dll
#r System.Runtime.InteropServices.dll
using System;
using System.IO;
using System.Data;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Collections.Generic;
public static bool ExportScript(ExportContext context)
{
try
{
context.Log("Executing Email script");
var data_files = context.ExportFiles.GetDataFiles();
if (data_files.Count == 0)
{
context.Log("No data files found.");
return false;
}
var filename = data_files[0];
var EXPORT_DIR = Path.GetDirectoryName(filename);
if (!File.Exists(filename))
{
context.Log("File does not exist: " + filename);
return false;
}
if (new FileInfo(filename).Length == 0)
{
context.Log("File is empty: " + filename);
return false;
}
context.Log("File is not empty");
var emailBody = generateEmailBody(context, filename);
if (string.IsNullOrEmpty(emailBody))
{
context.Log("Email body is empty, skipping email.");
return false;
}
var dtemail = DateTime.UtcNow.ToString("MM-dd-yyyy");
string subject = "Sequentum Cloud - Woodline Scraper: Progressive for date, " + dtemail;
sent_email(context, subject, emailBody);
}
catch (Exception ex)
{
context.Log("Error in ExportScript: " + ex.Message);
return false;
}
return true;
}
public static void sent_email(ExportContext context, string emailSubject, string emailBody)
{
try
{
if (!string.IsNullOrEmpty(emailBody))
{
var toAddress = context.GlobalData["DistributionList"] as string;
if (string.IsNullOrEmpty(toAddress))
{
context.Log("No recipient address found in DistributionList.");
return;
}
context.Log("Sending email to: " + toAddress);
// Send the email with the required parameters
context.ScriptUtilities.SendEmail(
toAddress,
subject: emailSubject,
body: emailBody,
true, // Treat the body as HTML
null, // Optional: Can be null or set a specific sender address
null, // Optional: CC address
null, // Optional: BCC address
attachments: null, // Optional: No attachments for now
false, // Optional: No read receipt required
false // Optional: Normal priority
);
context.Log("Email sent successfully!");
}
else
{
context.Log("Email body is empty, not sending email.");
}
}
catch (Exception ex)
{
context.Log("Error sending email: " + ex.Message);
}
}
public static string generateEmailBody(ExportContext context, string csvPath)
{
StringBuilder textBody = new StringBuilder();
List<string> emailMessageList = new List<string>();
try
{
var csv_read_all = context.ScriptUtilities.LoadCsvFileIntoDataTable(csvPath);
if (csv_read_all.Rows.Count > 0)
{
textBody.Append("<table border='1' cellpadding='0' cellspacing='0' width='1300'>");
textBody.Append("<tr bgcolor='#4da6ff'>");
context.Log("Reached checkpoint 1.");
// Create table headers
foreach (DataColumn column in csv_read_all.Columns)
{
context.Log("Entered foreach to read columns!");
textBody.Append("<td><b>" + column.ColumnName + "</b></td>");
}
textBody.Append("</tr>");
// Create table rows
foreach (System.Data.DataRow row in csv_read_all.Rows)
{
context.Log("Reached foreach to read records!");
textBody.Append("<tr>");
foreach (var item in row.ItemArray)
{
context.Log("Reached foreach to read values!"+item.ToString());
textBody.Append("<td>" + item.ToString() + "</td>");
}
textBody.Append("</tr>");
}
textBody.Append("</table>");
}
else
{
context.Log("No rows found in CSV file.");
}
}
catch (Exception ex)
{
context.Log("Error generating email body: " + ex.Message);
}
return textBody.ToString();
context.Log("textBody: "+textBody.ToString());
}