The following code adds an attachment to an email if the attachment file exists. If the file does not exist the the email informs the recipient that the file was not found. The recipients are read in from a text file. This sample sets the priority of the email to high and sets the body of the email to html so you can do some formatting. You will need a "Using" statement for System.IO and System.Configuration since things like email server and path to the list of recipients are stored in a config file. This particular example is from a console application. "EmailBody" is a string builder that as added to as various processes are checked. Note of caution: be the email can be sent from "valid" looking email address and be beware of getting your "Send" portion of the code in a loop. I know how embarrasing that can be. Have fun, be careful.
protected static void EmailNotification(string FileName)
{
//name of email server
string emailserver = ConfigurationSettings.AppSettings["emailserver"];
//path to file that will be added as an attachment if it exists.
string gl_daily_transactions = ConfigurationSettings.AppSettings ["gl_daily_transactions"];
var devemail = new System.Net.Mail.MailMessage();
var mailClient = new System.Net.Mail.SmtpClient(emailserver);
devemail.From = new System.Net.Mail.MailAddress("XYZInterfaceMonitor@notarealcompany.com");
string subjectline;
if (Success)
{
subjectline = "XYZ Interface process completed successfully. ";
}
else
{
subjectline = "XYZ Interface process did not complete successfully. ";
}
devemail.Subject = subjectline;
devemail.Priority = System.Net.Mail.MailPriority.High;
if (File.Exists(gl_daily_transactions))
{
System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(gl_daily_transactions);
devemail.Attachments.Add(attachment);
}
else
{
//append to string builder object
emailBody.Append("<hr />The GL Daily Transaction File was not found.");
}
devemail.IsBodyHtml = true;
emailBody.Append("<hr/>End of Notification");
devemail.Body = emailBody.ToString();
//FileReader returns an array of recipients from a text file
string[] recipients = FileReader.ReadFile(FileName);
for (int index = 0; index < recipients.Length; index++)
{
if (recipients.Length > 0)
{
devemail.To.Add(recipients[index].Trim());
}
}
mailClient.Send(devemail);
}