using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Web;
namespace Fab2ApprovalSystem.Misc
{
public class EmailNotification
{
#region Variabls
protected string _subject = null;
protected string _TemplatesPath = null;
#endregion
///
/// Email subject
///
public string EmailSubject
{
set { _subject = value; }
}
///
/// This function will read the content of a file name
///
/// File Name
/// String: Containing the Entire content of the file
protected string ReadEmailFile(string FileName)
{
string retVal = null;
try
{
//setting the file name path
string path = _TemplatesPath + FileName;
FileInfo TheFile = new FileInfo(System.Web.HttpContext.Current.Server.MapPath(path));
//check if the file exists in the location.
if (!TheFile.Exists)
throw new Exception("Could Not Find the file : " + FileName + " in the location " + _TemplatesPath); // throw an exception here.
//start reading the file. i have used Encoding 1256 to support arabic text also.
StreamReader sr = new StreamReader(System.Web.HttpContext.Current.Server.MapPath(@path), System.Text.Encoding.GetEncoding(1256));
retVal = sr.ReadToEnd(); // getting the entire text from the file.
sr.Close();
}
catch (Exception ex)
{
throw new Exception("Error Reading File." + ex.Message);
}
return retVal;
}
///
/// this function will send email. it will read the mail setting from the web.config
///
/// Sender Email ID
/// Sender Name
/// Recepient Email ID
/// CC ids
/// Email Subject
/// Email Body
protected void SendEmail(string SenderEmail, string SenderName, string Recep, string cc, string email_title, string email_body)
{
// creating email message
MailMessage msg = new MailMessage();
msg.IsBodyHtml = true;// email body will allow html elements
// setting the Sender Email ID
//msg.From = new MailAddress(SenderEmail, SenderName);
msg.From = new MailAddress("MesaFabApproval@infineon.com", "Mesa Fab Approval");
msg.Sender = new MailAddress("MesaFabApproval@infineon.com", "Mesa Fab Approval");
// adding the Recepient Email ID
msg.To.Add(Recep);
//msg.To.Add("Jonathan.Ouellette@infineon.com");
// add CC email ids if supplied.
if (!string.IsNullOrEmpty(cc))
msg.CC.Add(cc);
//setting email subject and body
msg.Subject = email_title;
msg.Body = email_body;
//create a Smtp Mail which will automatically get the smtp server details from web.config mailSettings section
SmtpClient SmtpMail = new SmtpClient("mailrelay-external.infineon.com");
// sending the message.
try
{
SmtpMail.Send(msg);
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in CreateTestMessage2(): {0}",
ex.ToString());
}
}
///
///
///
///
///
///
///
///
///
///
protected void SendEmailWithAttachment(string SenderEmail, string SenderName, string Recep, string cc, string email_title, string email_body, string attachmentPath)
{
// creating email message
MailMessage msg = new MailMessage();
msg.IsBodyHtml = true;// email body will allow html elements
// setting the Sender Email ID
msg.From = new MailAddress(SenderEmail, SenderName);
// adding the Recepient Email ID
msg.To.Add(Recep);
// add CC email ids if supplied.
if (!string.IsNullOrEmpty(cc))
msg.CC.Add(cc);
//setting email subject and body
msg.Subject = email_title;
msg.Body = email_body;
msg.Attachments.Add(new Attachment(attachmentPath));
//create a Smtp Mail which will automatically get the smtp server details from web.config mailSettings section
SmtpClient SmtpMail = new SmtpClient();
#if(!DEBUG)
// sending the message.
SmtpMail.Send(msg);
#endif
}
///
///
///
///
///
///
///
///
///
protected void SendEmail(string SenderEmail, string SenderName, List RecepientList, string cc, string email_title, string email_body)
{
// creating email message
MailMessage msg = new MailMessage();
msg.IsBodyHtml = true;// email body will allow html elements
// setting the Sender Email ID
//msg.From = new MailAddress(SenderEmail, SenderName);
msg.From = new MailAddress("MesaFabApproval@infineon.com", "Mesa Fab Approval");
// adding the Recepient Email ID
foreach (string recepient in RecepientList)
{
msg.To.Add(recepient);
}
// add CC email ids if supplied.
if (!string.IsNullOrEmpty(cc))
msg.CC.Add(cc);
//setting email subject and body
msg.Subject = email_title;
msg.Body = email_body;
//create a Smtp Mail which will automatically get the smtp server details from web.config mailSettings section
SmtpClient SmtpMail = new SmtpClient();
// sending the message.
try
{
SmtpMail.Send(msg);
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in CreateTestMessage2(): {0}",
ex.ToString());
}
}
///
///
///
///
///
///
///
///
///
///
protected void SendEmailWithAttachment(string SenderEmail, string SenderName, List RecepientList, string cc, string email_title, string email_body, string attachmentPath)
{
// creating email message
MailMessage msg = new MailMessage();
msg.IsBodyHtml = true;// email body will allow html elements
// setting the Sender Email ID
msg.From = new MailAddress(SenderEmail, SenderName);
// adding the Recepient Email ID
foreach (string recepient in RecepientList)
{
if (recepient != null)
msg.To.Add(recepient);
}
// add CC email ids if supplied.
if (!string.IsNullOrEmpty(cc))
msg.CC.Add(cc);
//setting email subject and body
msg.Subject = email_title;
msg.Body = email_body;
msg.Attachments.Add(new Attachment(attachmentPath));
//create a Smtp Mail which will automatically get the smtp server details from web.config mailSettings section
SmtpClient SmtpMail = new SmtpClient();
// sending the message.
SmtpMail.Send(msg);
}
///
///
///
///
///
///
///
///
///
///
protected void SendEmailWithAttachments(string SenderEmail, string SenderName, string Recep, string cc, string email_title, string email_body, List attachments)
{
// creating email message
MailMessage msg = new MailMessage();
msg.IsBodyHtml = true;// email body will allow html elements
// setting the Sender Email ID
msg.From = new MailAddress(SenderEmail, SenderName);
// adding the Recepient Email ID
msg.To.Add(Recep);
// add CC email ids if supplied.
if (!string.IsNullOrEmpty(cc))
msg.CC.Add(cc);
//setting email subject and body
msg.Subject = email_title;
msg.Body = email_body;
foreach (string attachment in attachments)
{
msg.Attachments.Add(new Attachment(attachment));
}
//create a Smtp Mail which will automatically get the smtp server details from web.config mailSettings section
SmtpClient SmtpMail = new SmtpClient();
#if(!DEBUG)
// sending the message.
SmtpMail.Send(msg);
#endif
}
///
///
///
///
///
///
///
///
///
///
protected void SendEmailWithAttachments(string SenderEmail, string SenderName, List RecepientList, string cc, string email_title, string email_body, List attachments)
{
// creating email message
MailMessage msg = new MailMessage();
msg.IsBodyHtml = true;// email body will allow html elements
// setting the Sender Email ID
msg.From = new MailAddress(SenderEmail, SenderName);
// adding the Recepient Email ID
foreach (string recepient in RecepientList)
{
if (recepient != null)
msg.To.Add(recepient);
}
// add CC email ids if supplied.
if (!string.IsNullOrEmpty(cc))
msg.CC.Add(cc);
//setting email subject and body
msg.Subject = email_title;
msg.Body = email_body;
foreach (string attachment in attachments)
{
msg.Attachments.Add(new Attachment(attachment));
}
//create a Smtp Mail which will automatically get the smtp server details from web.config mailSettings section
SmtpClient SmtpMail = new SmtpClient();
// sending the message.
SmtpMail.Send(msg);
}
public EmailNotification()
{
}
///
/// The Constructor Function
///
/// Email Header Subject
/// Emails Files Templates
public EmailNotification(string EmailHeaderSubject, string TemplatesPath)
{
_subject = EmailHeaderSubject;
_TemplatesPath = TemplatesPath;
}
///
/// This function will send the email notification by reading the email template and substitute the arguments
///
/// Email Template File
/// Sender Email
/// Sender Name
/// Recepient Email ID
/// CC IDs
/// EMail Subject
/// Arguments
/// String: Return the body of the email to be send
public string SendNotificationEmail(string EmailTemplateFile, string SenderEmail, string SenderName, string RecepientEmail, string CC, string Subject, params string[] Args)
{
string retVal = null;
//reading the file
string FileContents = ReadEmailFile(EmailTemplateFile);
string emailBody = FileContents;
//setting formatting the string
retVal = string.Format(emailBody, Args);
try
{
//check if we are in debug mode or not. to send email
if (GlobalVars.DBConnection.ToUpper() == "TEST" || GlobalVars.DBConnection.ToUpper() == "QUALITY")
{
SendEmail(SenderEmail, SenderName, GetTestRecipientsList(), CC, (!string.IsNullOrEmpty(Subject) ? Subject + " for: " + RecepientEmail : _subject), retVal);
}
else
{
#if(!DEBUG)
SendEmail(SenderEmail, SenderName, RecepientEmail, CC, (!string.IsNullOrEmpty(Subject) ? Subject : _subject), retVal);
#endif
}
}
catch (Exception ex)
{
throw ex;
}
return retVal;
}
///
/// This function will send the email notification by reading the email template and substitute the arguments along with the attachments
///
///
///
///
///
///
///
///
///
///
public string SendNotificationEmailWithAttachment(string EmailTemplateFile, string SenderEmail, string SenderName, string RecepientEmail, string CC, string Subject, string attachmentPath, params string[] Args)
{
string retVal = null;
//reading the file
string FileContents = ReadEmailFile(EmailTemplateFile);
string emailBody = FileContents;
//setting formatting the string
retVal = string.Format(emailBody, Args);
try
{
//check if we are in debug mode or not. to send email
if (GlobalVars.DBConnection.ToUpper() == "TEST" || GlobalVars.DBConnection.ToUpper() == "QUALITY")
{
SendEmailWithAttachment(SenderEmail, SenderName, GetTestRecipientsList(), CC, (!string.IsNullOrEmpty(Subject) ? " TESTING ONLY -IGNORE : " + Subject + RecepientEmail : _subject), retVal, attachmentPath);
}
else
{
#if(!DEBUG)
SendEmailWithAttachment(SenderEmail, SenderName, RecepientEmail, CC, (!string.IsNullOrEmpty(Subject) ? Subject : _subject), retVal, attachmentPath);
#endif
}
}
catch (Exception ex)
{
throw ex;
}
return retVal;
}
///
///
///
///
///
///
///
///
///
///
///
///
public string SendNotificationEmailWithAttachment(string EmailTemplateFile, string SenderEmail, string SenderName, List RecepientEmail, string CC, string Subject, string attachmentPath, params string[] Args)
{
string retVal = null;
//reading the file
string FileContents = ReadEmailFile(EmailTemplateFile);
string emailBody = FileContents;
//setting formatting the string
retVal = string.Format(emailBody, Args);
try
{
//check if we are in debug mode or not. to send email
if (GlobalVars.DBConnection.ToUpper() == "TEST" || GlobalVars.DBConnection.ToUpper() == "QUALITY")
{
foreach(string email in RecepientEmail)
{
Subject += email + ";";
}
RecepientEmail.Clear();
SendEmailWithAttachment(SenderEmail, SenderName, GetTestRecipientsList(), CC, (!string.IsNullOrEmpty(Subject) ? " TESTING ONLY -IGNORE : " + Subject : _subject), retVal, attachmentPath);
}
else
{
#if (!DEBUG)
SendEmailWithAttachment(SenderEmail, SenderName, RecepientEmail, CC, (!string.IsNullOrEmpty(Subject) ? Subject : _subject), retVal, attachmentPath);
#endif
}
}
catch (Exception ex)
{
throw ex;
}
return retVal;
}
/////////============================================================
public string SendNotificationEmailWithAttachments(string EmailTemplateFile, string SenderEmail, string SenderName, string RecepientEmail, string CC, string Subject, List attachments, params string[] Args)
{
string retVal = null;
//reading the file
string FileContents = ReadEmailFile(EmailTemplateFile);
string emailBody = FileContents;
//setting formatting the string
retVal = string.Format(emailBody, Args);
try
{
//check if we are in debug mode or not. to send email
if (GlobalVars.DBConnection.ToUpper() == "TEST" || GlobalVars.DBConnection.ToUpper() == "QUALITY")
{
SendEmailWithAttachments(SenderEmail, SenderName, GetTestRecipientsList(), CC, (!string.IsNullOrEmpty(Subject) ? " TESTING ONLY -IGNORE : " + Subject + RecepientEmail : _subject), retVal, attachments);
}
else
{
#if(!DEBUG)
SendEmailWithAttachments(SenderEmail, SenderName, RecepientEmail, CC, (!string.IsNullOrEmpty(Subject) ? Subject : _subject), retVal, attachments);
#endif
}
}
catch (Exception ex)
{
throw ex;
}
return retVal;
}
///
///
///
///
///
///
///
///
///
///
///
///
public string SendNotificationEmailWithAttachments(string EmailTemplateFile, string SenderEmail, string SenderName, List RecepientEmail, string CC, string Subject, List attachments, params string[] Args)
{
string retVal = null;
//reading the file
string FileContents = ReadEmailFile(EmailTemplateFile);
string emailBody = FileContents;
//setting formatting the string
retVal = string.Format(emailBody, Args);
try
{
//check if we are in debug mode or not. to send email
if (GlobalVars.DBConnection.ToUpper() == "TEST" || GlobalVars.DBConnection.ToUpper() == "QUALITY")
{
foreach(string email in RecepientEmail)
{
Subject += email + ";";
}
RecepientEmail.Clear();
SendEmailWithAttachments(SenderEmail, SenderName, GetTestRecipientsList(), CC, (!string.IsNullOrEmpty(Subject) ? " TESTING ONLY -IGNORE : " + Subject : _subject), retVal, attachments);
}
else
{
#if (!DEBUG)
SendEmailWithAttachments(SenderEmail, SenderName, RecepientEmail, CC, (!string.IsNullOrEmpty(Subject) ? Subject : _subject), retVal, attachments);
#endif
}
}
catch (Exception ex)
{
throw ex;
}
return retVal;
}
///
///
///
///
///
///
///
///
///
///
///
public string SendNotificationEmail(string EmailTemplateFile, string SenderEmail, string SenderName, List RecepientEmail, string CC, string Subject, params string[] Args)
{
string retVal = null;
//reading the file
string FileContents = ReadEmailFile(EmailTemplateFile);
string emailBody = FileContents;
//setting formatting the string
retVal = string.Format(emailBody, Args);
try
{
//check if we are in debug mode or not. to send email
if (GlobalVars.DBConnection.ToUpper() == "TEST" || GlobalVars.DBConnection.ToUpper() == "QUALITY")
{
foreach (string email in RecepientEmail)
{
Subject += email + ";";
}
RecepientEmail.Clear();
SendEmail(SenderEmail, SenderName, GetTestRecipientsList(), CC, (!string.IsNullOrEmpty(Subject) ? Subject : _subject), retVal);
}
else
{
#if (!DEBUG)
SendEmail(SenderEmail, SenderName, RecepientEmail, CC, (!string.IsNullOrEmpty(Subject) ? Subject : _subject), retVal);
#endif
}
}
catch (Exception ex)
{
throw ex;
}
return retVal;
}
///
///
///
///
///
///
public void SendNotificationEmailToAdmin(string subject, string body, MailPriority importance)
{
try
{
System.Configuration.ConfigurationManager.RefreshSection("appSettings");
SmtpClient client = new SmtpClient(ConfigurationManager.AppSettings["SMTP Server"]);
MailMessage msg = new MailMessage();
string toList = ConfigurationManager.AppSettings["Admin Notification Recepient"];
msg.From = new MailAddress(ConfigurationManager.AppSettings["Notification Sender"]); ;
string[] recipients = toList.Split(',');
foreach (string recipient in recipients)
msg.To.Add(new MailAddress(recipient.Trim()));
DateTime now = DateTime.Now;
string temp = now.ToString() + " - " + body;
msg.Subject = subject;
msg.Body = temp;
msg.Priority = importance;
//#if(!DEBUG)
client.Send(msg);
//#endif
}
catch (Exception ex)
{
throw ex;
}
}
public List GetTestRecipientsList()
{
var r = new List();
try
{
string emails = ConfigurationManager.AppSettings["Test Email Recipients"];
foreach (string s in emails.Split(';', ','))
{
if (!String.IsNullOrWhiteSpace(s))
r.Add(s);
}
}
catch
{
r.Add("dhuang2@infineon.com");
}
return r;
}
}
}