public static string CreateBody(KCSAssessmentEntity kcs) { string returvalue = string.Empty; string filename = AppDomain.CurrentDomain.BaseDirectory + @"\EmailTemplate.html"; MessageBox.Show(filename); using (StreamReader reader = new StreamReader(filename)) returvalue = reader.ReadToEnd(); returvalue = returvalue.Replace("{TSOwner}", kcs.techSupportOwner); returvalue = returvalue.Replace("{NPI}", kcs.npiNumber); returvalue = returvalue.Replace("{ProjManager}", kcs.projectLead); returvalue = returvalue.Replace("{Backup}", kcs.techSupportexception); returvalue = returvalue.Replace("{Title}", kcs.npiTitle); return(returvalue); }
private bool SendEmail(KCSAssessmentEntity kcs, out string exception) { try { MailMessage message = new MailMessage(); SmtpClient smtp = new SmtpClient(); message.IsBodyHtml = true; string htmlbody = string.Empty; htmlbody += "<h1> </h1>"; string fromEmailaddress = ReturnConfigValue("FromEmailAddress"); fromEmailaddress = (fromEmailaddress == string.Empty ? "*****@*****.**" : fromEmailaddress); if (fromEmailaddress.Substring(fromEmailaddress.Length - 1, 1) == ";") { fromEmailaddress = fromEmailaddress.Remove(fromEmailaddress.Length, 1); } if (fromEmailaddress != null || fromEmailaddress != string.Empty) { if (IsValidEmail(fromEmailaddress) == false) { exception = "Invalid From Email Address!"; return(false); } } message.From = new MailAddress(fromEmailaddress); message.To.Add(kcs.empEmailAddress); message.CC.Add(kcs.empEmailAddressesCc); foreach (var Ccaddress in ConfigurationManager.AppSettings["CCEmailAddress"].ToString().Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries)) { if (IsValidEmail(Ccaddress) == false) { exception = "Invalid CC Email Address!"; return(false); } message.CC.Add(Ccaddress); } foreach (var Bccaddress in ConfigurationManager.AppSettings["BCCEmailAddress"].ToString().Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries)) { if (IsValidEmail(Bccaddress) == false) { exception = "Invalid BCC Email Address!"; return(false); } message.Bcc.Add(Bccaddress); } string subject = ReturnConfigValue("EmailSubject"); message.Subject = (subject == string.Empty ? "KCS Assessment" : subject); message.Body = CreateBody(kcs); smtp.Port = 25; smtp.Host = "smtp.honeywell.com"; smtp.EnableSsl = true; smtp.UseDefaultCredentials = false; string eusername = ReturnConfigValue("EmailUserName"); string epassword = ReturnConfigValue("EmailPassword"); //MessageBox.Show(eusername + " - " + epassword); if (eusername == string.Empty) { exception = "Configure EMail UserName!"; return(false); } if (epassword == string.Empty) { exception = "Configure EMail Password!"; return(false); } smtp.Credentials = new NetworkCredential(eusername, epassword); smtp.DeliveryMethod = SmtpDeliveryMethod.Network; //MessageBox.Show("Post NetworkCredential"); smtp.Send(message); exception = "Success"; return(true); } catch (Exception ex) { exception = ex.Message.ToString() + " - " + ex.TargetSite.Module + " - " + ex.TargetSite.Name; //MessageBox.Show(exception); return(false); } }
private void cmdEmail_Click(object sender, EventArgs e) { /* * service account to send email * shared mail box - give gene * do not use personal account to send email * Use HEmployee ID to retrieve emp info DO NOT use FN or LN * talk with Enterprise Arch team - https://honeywellprod.sharepoint.com/sites/DWP-Functions/information-technology/Enterprise-Architecture/Pages/Home.aspx * look for MS graph API - * Send email to DL-AdminConSents * */ try { string internalToEmailAddress = string.Empty; string internalCcEmailAddresses = string.Empty; string internalTsOwner = string.Empty; string internalTSBackup = string.Empty; for (int rowcount = 0; rowcount <= gridData.Rows.Count - 1; rowcount++) { if (gridData.Rows[rowcount].Tag.ToString() != "Email Sent") { if (gridData[10, rowcount].Value.ToString() == string.Empty) { MessageBox.Show(string.Format("Please assign AMERICAS TS Owner for the NPI:{0}", gridData[0, rowcount].Value.ToString()), "Information", MessageBoxButtons.OK); return; } if (gridData[11, rowcount].Value.ToString() == string.Empty) { MessageBox.Show(string.Format("Please assign AMERICAS TS Owner for the NPI:{0}", gridData[0, rowcount].Value.ToString()), "Information", MessageBoxButtons.OK); return; } if (gridData[12, rowcount].Value.ToString() == string.Empty) { MessageBox.Show(string.Format("Please assign AMERICAS TS Owner for the NPI:{0}", gridData[0, rowcount].Value.ToString()), "Information", MessageBoxButtons.OK); return; } if (gridData[9, rowcount].Value.ToString().ToUpper() == "AMERICAS") { internalTsOwner = gridData[10, rowcount].Value.ToString(); internalToEmailAddress = gridData[10, rowcount].Tag.ToString(); internalCcEmailAddresses = gridData[11, rowcount].Tag.ToString() + "," + gridData[12, rowcount].Tag.ToString(); internalTSBackup = gridData[11, rowcount].Value.ToString() + ";" + gridData[12, rowcount].Value.ToString(); } else if (gridData[9, rowcount].Value.ToString().ToUpper() == "EMEA") { internalTsOwner = gridData[11, rowcount].Value.ToString(); internalToEmailAddress = gridData[11, rowcount].Tag.ToString(); internalCcEmailAddresses = gridData[10, rowcount].Tag.ToString() + "," + gridData[12, rowcount].Tag.ToString(); internalTSBackup = gridData[10, rowcount].Value.ToString() + ";" + gridData[12, rowcount].Value.ToString(); } if (gridData[9, rowcount].Value.ToString().ToUpper() == "APAC") { internalTsOwner = gridData[12, rowcount].Value.ToString(); internalToEmailAddress = gridData[12, rowcount].Tag.ToString(); internalCcEmailAddresses = gridData[10, rowcount].Tag.ToString() + "," + gridData[11, rowcount].Tag.ToString(); internalTSBackup = gridData[10, rowcount].Value.ToString() + ";" + gridData[11, rowcount].Value.ToString(); } MessageBox.Show(internalToEmailAddress + " - " + internalCcEmailAddresses); KCSAssessmentEntity kcs = new KCSAssessmentEntity() { npiNumber = gridData[0, rowcount].Value.ToString(), npiTitle = gridData[2, rowcount].Value.ToString(), techSupportOwner = internalTsOwner, techSupportexception = internalTSBackup, projectLead = gridData[4, rowcount].Value.ToString(), regionalImpact = gridData[9, rowcount].Value.ToString(), empEmailAddress = internalToEmailAddress, empEmailAddressesCc = internalCcEmailAddresses }; if (MessageBox.Show("Do you want to send email to the TSE?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { if (SendEmail(kcs, out string exception) == false) { //MessageBox.Show(exception +" from Form1.cs"); MessageBox.Show(exception + " Error in sending email! - Ensure HON network is connected through VPN", "Email-Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { gridData.Rows[rowcount].DefaultCellStyle.BackColor = Color.Beige; gridData.Rows[rowcount].Tag = "Email Sent"; } } } } } catch (Exception ex) { MessageBox.Show(ex.StackTrace.ToString(), "NPI Tracker - FirstForm", MessageBoxButtons.OK); } }