private string getBodyHtml(DocumentPropertiesIP properties) { string body = ""; switch (properties.TemplateType) { case "InstantPot": body = File.ReadAllText(instantPotTemplatePath); if (properties.BillOfLading != null && properties.BillOfLading != "" && double.TryParse(properties.BillOfLading, out double res) == true) { body = body.Replace(instantPotTrackingNumber, properties.BillOfLading); return(body); } else { body = body.Replace(instantPotTrackingDiv, "hidden=\"true\""); body = body.Replace(instantPotTrackingNumber, ""); return(body); } case "Accessory": body = File.ReadAllText(accessoariesTemplatePath); if (properties.BillOfLading != null && properties.BillOfLading != "" && double.TryParse(properties.BillOfLading, out double res2) == true) { body = body.Replace(accessoariesTrackingNumber, properties.BillOfLading); return(body); } else { body = body.Replace(accessoariesTrackingDiv, "hidden=\"true\""); body = body.Replace(accessoariesTrackingNumber, ""); return(body); } case "TBI": body = File.ReadAllText(tbiTemplatePath); if (properties.BillOfLading != null && properties.BillOfLading != "" && double.TryParse(properties.BillOfLading, out double res3) == true) { body = body.Replace(tbiTrackingNumber, properties.BillOfLading); return(body); } else { body = body.Replace(tbiTrackingDiv, "hidden=\"true\""); body = body.Replace(tbiTrackingNumber, ""); return(body); } default: return(body); } }
////////////////////////////////////////////////////// send emails //////////////////////////////////////////////////////////// private void sendEmails(DocumentPropertiesIP properties, string emailTo) { try { var userEmail = tbEmail.Text; var userPw = tbPassword.Text; var userAlias = tbAlias.Text; System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(); SmtpClient SmtpServer = new SmtpClient(smtpServer); mail.From = new MailAddress(userEmail, userAlias); mail.To.Add(emailTo); mail.Subject = mainSubject + " " + properties.OrderNo; //get the right html and edit it accordingly mail.Body = getBodyHtml(properties); mail.IsBodyHtml = isBodyHtml; Attachment attachment = new Attachment(properties.DocFileDir); mail.Attachments.Add(attachment); SmtpServer.Port = port; SmtpServer.EnableSsl = enableSsl; SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network; SmtpServer.UseDefaultCredentials = useDefaultCredentials; SmtpServer.Credentials = new System.Net.NetworkCredential(userEmail, userPw); SmtpServer.Send(mail); /* * using (StreamWriter writer = File.CreateText(logPath)) * { * writer.WriteLine("userMail: " + userEmail); * writer.WriteLine("userAlias: " + userAlias); * writer.WriteLine("emailTo: " + emailTo); * writer.WriteLine("subject: " + mail.Subject); * writer.WriteLine("body: " + mail.Body); * } */ } catch (Exception err) { MessageBox.Show(err.Message, caption, MessageBoxButtons.OK); lblStatus.Text = err.Message.Split('.')[0]; return; } }
////////////////////////////////////////////////////// get doc properties ////////////////////////////////////////////////////// private DocumentPropertiesIP getProperties(Document doc) { object _BuiltInProperties = doc.BuiltInDocumentProperties; Type typeDocBuiltInProps = _BuiltInProperties.GetType(); string docNo; string emails; string billOfLading; string docType; string templateType; string orderNo; try { // Document no prop Object DocNoProp = typeDocBuiltInProps.InvokeMember("Item", BindingFlags.Default | BindingFlags.GetProperty, null, _BuiltInProperties, new object[] { "Title" }); Type typeDocNoProp = DocNoProp.GetType(); docNo = typeDocNoProp.InvokeMember("Value", BindingFlags.Default | BindingFlags.GetProperty, null, DocNoProp, new object[] { }).ToString(); } catch (Exception err) { docNo = ""; } try { // billOfLading prop Object billOfLadingProp = typeDocBuiltInProps.InvokeMember("Item", BindingFlags.Default | BindingFlags.GetProperty, null, _BuiltInProperties, new object[] { "Comments" }); Type typeBillOfLadingProp = billOfLadingProp.GetType(); billOfLading = typeBillOfLadingProp.InvokeMember("Value", BindingFlags.Default | BindingFlags.GetProperty, null, billOfLadingProp, new object[] { }).ToString(); } catch (Exception err) { billOfLading = ""; } try { // Document type prop Object DocTypeProp = typeDocBuiltInProps.InvokeMember("Item", BindingFlags.Default | BindingFlags.GetProperty, null, _BuiltInProperties, new object[] { "Subject" }); Type typeDocTypeProp = DocTypeProp.GetType(); docType = typeDocTypeProp.InvokeMember("Value", BindingFlags.Default | BindingFlags.GetProperty, null, DocTypeProp, new object[] { }).ToString(); } catch (Exception err) { docType = ""; } try { // templateType prop Object templateTypeProp = typeDocBuiltInProps.InvokeMember("Item", BindingFlags.Default | BindingFlags.GetProperty, null, _BuiltInProperties, new object[] { "Category" }); Type typeTemplateTypeProp = templateTypeProp.GetType(); templateType = typeTemplateTypeProp.InvokeMember("Value", BindingFlags.Default | BindingFlags.GetProperty, null, templateTypeProp, new object[] { }).ToString(); } catch (Exception err) { templateType = ""; } try { // orderNo prop Object orderNoProp = typeDocBuiltInProps.InvokeMember("Item", BindingFlags.Default | BindingFlags.GetProperty, null, _BuiltInProperties, new object[] { "Company" }); Type typeTemplateTypeProp = orderNoProp.GetType(); orderNo = typeTemplateTypeProp.InvokeMember("Value", BindingFlags.Default | BindingFlags.GetProperty, null, orderNoProp, new object[] { }).ToString(); } catch (Exception err) { orderNo = ""; } try { // Emails prop Object EmailsProp = typeDocBuiltInProps.InvokeMember("Item", BindingFlags.Default | BindingFlags.GetProperty, null, _BuiltInProperties, new object[] { "Keywords" }); Type typeEmailsProp = EmailsProp.GetType(); emails = typeEmailsProp.InvokeMember("Value", BindingFlags.Default | BindingFlags.GetProperty, null, EmailsProp, new object[] { }).ToString(); } catch (Exception err) { emails = ""; } DocumentPropertiesIP properties = new DocumentPropertiesIP(docNo, emails, billOfLading, docType, templateType, orderNo); return(properties); }