using System; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.Net.Mail; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; /// /// Summary description for Class PPSNotification /// /// Class contains methods for sending email notifications for PPS /// public class PPSNotification { public PPSNotification() { // // TODO: Add constructor logic here // } public static string SendNewPPCreatedEmail(string coeRefNum, string projectTitle, string projectDate, string projectStartTime, string projectEndTime, string boardPortfolioName, int boardPortfolioID, string committeeName, string ppStatus, string statusUpdatedBy) { bool result; string NewPPEmail; string sendStatusMsg = ""; // get username of logged on user if not passed in if (statusUpdatedBy.Length == 0) { System.Security.Principal.IPrincipal User; User = System.Web.HttpContext.Current.User; statusUpdatedBy = Utilities.GetLoggedInUserID(User.Identity.Name); } NewPPEmail = BuildNewPPEmail(coeRefNum, projectTitle, projectDate, projectStartTime, projectEndTime, boardPortfolioName, committeeName, ppStatus, statusUpdatedBy); //prepare email MailMessage msg = new MailMessage(); msg.From = new MailAddress("ppsadmin@ijkc.com", "PPS Admin"); //Add recipients // This call adds recipients from PlanningAdmin, PlanningFinance, and members of the particular board result = AddStatusUpdateRecipients(boardPortfolioID, ref msg); if (result == false) { //there was a problem adding recipients, so can't send email. sendStatusMsg = "Recipients could not be added. Email not sent."; } else { //create the email subject msg.Subject = "PPS - New Project: " + boardPortfolioName + " (" + coeRefNum + "-" + projectTitle + ") - " + ppStatus; //add the body of the email & set the format to HTML msg.Body = NewPPEmail; msg.IsBodyHtml = true; //set the priority msg.Priority = MailPriority.High; //send the email result = SendEmail(msg, out sendStatusMsg); } if (result == false) { //there was a problem sending the email. } return sendStatusMsg; } private static string BuildNewPPEmail(string coeRefNum, string projectTitle, string projectDate, string projectStartTime, string projectEndTime, string boardPortfolioName, string committeeName, string ppStatus, string statusUpdatedBy) { string statusUpdateEmailMsg = "New PP Email" + "

The following project has been created:

" + "Project Number: " + coeRefNum + "
" + "Title: " + projectTitle + "
" + "Date: " + projectDate + "
" + "Time: " + projectStartTime + " - " + projectEndTime + "
" + "Board/Portfolio: " + boardPortfolioName + "
" + "Status: " + ppStatus + "
" + "Created By: " + statusUpdatedBy + "
" + ""; return statusUpdateEmailMsg; } public static string SendStatusUpdateEmail(string coeRefNum, string projectTitle, string projectDate, string projectStartTime, string projectEndTime, string boardPortfolioName, int boardPortfolioID, string committeeName, string ppStatus, string statusUpdatedBy) { bool result; string statusUpdateEmail; string sendStatusMsg = ""; // get username of logged on user if not passed in if (statusUpdatedBy.Length == 0) { System.Security.Principal.IPrincipal User; User = System.Web.HttpContext.Current.User; statusUpdatedBy = Utilities.GetLoggedInUserID(User.Identity.Name); } statusUpdateEmail = BuildStatusUpdateEmail(coeRefNum, projectTitle, projectDate, projectStartTime, projectEndTime, boardPortfolioName, committeeName, ppStatus, statusUpdatedBy); //prepare email MailMessage msg = new MailMessage(); msg.From = new MailAddress("ppsadmin@ijkc.com", "PPS Admin"); //Add recipients // This call adds recipients from PlanningAdmin, PlanningFinance, and members of the particular board result = AddStatusUpdateRecipients(boardPortfolioID, ref msg); if (result == false) { //there was a problem adding recipients, so can't send email. sendStatusMsg = "Recipients could not be added. Email not sent."; } else { //create the email subject msg.Subject = "PPS - Status Update: " + boardPortfolioName + " (" + coeRefNum + "-" + projectTitle + ") - " + ppStatus; //add the body of the email & set the format to HTML msg.Body = statusUpdateEmail; msg.IsBodyHtml = true; //set the priority msg.Priority = MailPriority.High; //send the email result = SendEmail(msg, out sendStatusMsg); } if (result == false) { //there was a problem sending the email. } return sendStatusMsg; } private static string BuildStatusUpdateEmail(string coeRefNum, string projectTitle, string projectDate, string projectStartTime, string projectEndTime, string boardPortfolioName, string committeeName, string ppStatus, string statusUpdatedBy) { string statusUpdateEmailMsg = "Status Update Email" + "

The status of the following project has been updated:

" + "Project Number: " + coeRefNum + "
" + "Title: " + projectTitle + "
" + "Date: " + projectDate + "
" + "Time: " + projectStartTime + " - " + projectEndTime + "
" + "Board/Portfolio: " + boardPortfolioName + "
" + "Committee: " + committeeName + "
" + "New Status: " + ppStatus + "
" + "Updated By: " + statusUpdatedBy + "
" + ""; return statusUpdateEmailMsg; } //private static string BuildPPCreateErrorEmail(string coeRefNum, string projectTitle, string projectDate, //string projectStartTime, string projectEndTime, string boardPortfolioName, string committeeName, //string ppStatus, string statusUpdatedBy, errMsg) //{ // string ppCreateErrorEmailMsg = "Status Update Email" + // "

TEST EMAIL, NOT REAL DATA. PLEASE DO NOT REPLY IF YOU RECEIVED THIS.

" + // "

The status of the following project has been updated:

" + // "Project Number: " + coeRefNum + "
" + // "Title: " + projectTitle + "
" + // "Date: " + projectDate + "
" + // "Time: " + projectStartTime + " - " + projectEndTime + "
" + // "Board/Portfolio: " + boardPortfolioName + "
" + // "Committee: " + committeeName + "
" + // "New Status: " + ppStatus + "
" + // "Updated By: " + statusUpdatedBy + "

" + // "Error Message:
" + errMsg + "
" + // ""; // return ppCreateErrorEmailMsg; //} private static bool AddStatusUpdateRecipients(int boardPortfolioID, ref MailMessage msg) { bool returnValue; try { // Retrieve the Aspnet role for the board/portfolio owning this project string aspnetRole = GetAspnetRoleForBoardPortfolio(boardPortfolioID); //retrieve email addresses of recipients from aspnetdb database SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ToString()); cn.Open(); SqlCommand cmd = new SqlCommand("mspMembershipGetEmailsInRoles", cn); cmd.CommandType = CommandType.StoredProcedure; SqlParameter param = cmd.Parameters.Add("@RoleNames", SqlDbType.VarChar, 256); param.Direction = ParameterDirection.Input; param.Value = aspnetRole + " | AdminPlanning | AdminFinance"; SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); if (dr.HasRows) { while (dr.Read()) { //add recipients msg.To.Add(new MailAddress(dr["LoweredEmail"].ToString())); } } //clean up dr.Close(); dr = null; cmd.Dispose(); cmd = null; cn.Close(); cn = null; //return success returnValue = true; } catch (Exception e) { //return failure returnValue = false; } return returnValue; } private static string GetAspnetRoleForBoardPortfolio(int boardPortfolioID) { string aspnetRole; // Get Board/Portfolio row for this BoardPortfolioID from DB dsProject.tluBoardPortfolioDataTable dtBP = new dsProject.tluBoardPortfolioDataTable(); dsProject.tluBoardPortfolioRow BPTableRow; dsProjectTableAdapters.tluBoardPortfolioTableAdapter bpTableAdapter = new dsProjectTableAdapters.tluBoardPortfolioTableAdapter(); dtBP = bpTableAdapter.GetByID(boardPortfolioID); // We should have a single row returned for the ID BPTableRow = (dsProject.tluBoardPortfolioRow)dtBP.Rows[0]; // Retrieve the Aspnet role for the board/portfolio owning this project aspnetRole = BPTableRow.AspnetUserRoleName; return aspnetRole; } private static bool SendEmail(MailMessage msg, out string sendStatusMsg) { bool sendStatus; try { SmtpClient smtp = new SmtpClient(); smtp.Send(msg); sendStatus = true; sendStatusMsg = "Email notification sent."; } catch(Exception e) { sendStatus = false; sendStatusMsg = "Unable to send email: " + e.ToString(); } return sendStatus; } }