private static string GetHtmlFromReportRecord(ReportInfoRecord reportRecord) { try { string lineBegin = "<p>"; string lineEnd = "</p>"; string messageBody = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"> <font>有关您的报告信息: </font><br><br>"; messageBody += lineBegin + "检测人姓名:" + reportRecord.PatientName + lineEnd; messageBody += lineBegin + "报告日期:" + reportRecord.ReportDate + lineEnd; messageBody += lineBegin + "检测项目:" + reportRecord.ReportTypes + lineEnd; messageBody += lineBegin + "报告注释:" + reportRecord.ReportComments + lineEnd; messageBody += lineBegin + "销售员:" + reportRecord.SalespersonName + lineEnd; return(messageBody); } catch (Exception) { return(null); } }
/// <summary> /// Send all emails to relevant salesReps and medReps for each report. Report PDFs are attached separately in each email. /// </summary> /// <param name="useremail">Email account (without domain name) of the person operating the program.</param> /// <param name="domain">Domain name (@geneseeq.com) of the email address of the sender.</param> /// <param name="password">Operating user's email password.</param> /// <param name="reportFolderPath">The main folder containing all the report directories, selected by the operator.</param> /// <param name="sendEmail">Operator can select whether to actually send the emails or not.</param> internal static string SendEmails(string useremail, string domain, string password, string reportFolderPath, bool sendEmail) { userMessages = ""; int sentMailCount = 0; string mailserver = "smtp.exmail.qq.com"; SmtpClient client = new SmtpClient(mailserver); client.Credentials = new NetworkCredential(useremail + domain, password); //foreach (ReportInfoRecord reportRecord in reportInfoRecordList) for (int i = 0; i < reportInfoRecordList.Count(); i++) { ReportInfoRecord reportRecord = reportInfoRecordList[i]; string htmlContent = GetHtmlFromReportRecord(reportRecord); string salesperson = reportRecord.SalespersonName; List <string> ccList = GetSalespersonEmailCCList(salesperson); //if (ccList.Count == 0) // userMessages += "没有找到有关病人 "+reportRecord.PatientName+" 的销售人员\n"; if (sendEmail) { try { if (!salespersonEmailDict.ContainsKey(salesperson)) { throw new Exception("处理(" + reportRecord.PatientName + ")的报告时,没有找到销售人(" + salesperson + ")的邮箱\n"); } MailMessage mail = new MailMessage(useremail + domain, salespersonEmailDict[salesperson]); mail.From = new MailAddress(useremail + domain); mail.Sender = new MailAddress(useremail + domain); mail.Body = htmlContent; mail.IsBodyHtml = true; foreach (string superiorEmailAddr in ccList) { mail.CC.Add(new MailAddress(superiorEmailAddr)); } mail.Subject = "报告 - " + reportRecord.ReportDate + " - " + reportRecord.PatientName; mail.Attachments.Add(new Attachment(reportRecordPdfPathList[i])); client.Send(mail); sentMailCount++; allFilesInDir.Remove(reportRecordPdfPathList[i]); } catch (Exception e) { userMessages += "发送文件" + reportRecordPdfPathList[i] + "没有成功: " + e.Message + "\n"; } } else { userMessages += "模拟发送邮件给:" + salespersonEmailDict[salesperson] + "\n拷贝给:"; foreach (string addr in ccList) { userMessages += "\n\t" + addr; } userMessages += "\n附件为:" + reportRecordPdfPathList[i] + "\n\n"; sentMailCount++; allFilesInDir.Remove(reportRecordPdfPathList[i]); } } if (sendEmail && userMessages == "") { userMessages = "所有邮件成功发出。\n"; } userMessages += "一共发送了" + sentMailCount + "条邮件。\n"; if (allFilesInDir.Count > 0) { userMessages += "有一部分报告文件没有发出:\n"; foreach (string file in allFilesInDir) { userMessages += file + "\n"; } } return(userMessages); }