private void sendEmail(String emailId) { String client = Settings.Default.TripolisClient; String userName = Settings.Default.TripoisUserName; String password = Settings.Default.TripolisPassword; ChinaUnionAdapter mailAdapter = new ChinaUnionAdapter(client, userName, password, null); MailData mailData = new MailData(); mailData.fromAddress = Settings.Default.MailFromAddress; mailData.replyAddress = Settings.Default.MailReplyAddress; mailData.sender = Settings.Default.MailSender; mailData.subject = "扫一扫,加入上海联通合作伙伴微信平台"; String databaseId = Settings.Default.TripolisDBId; String workspaceId = Settings.Default.TripolisWorkspaceId; String emailTypeId = Settings.Default.TripolisEmailTypeId; String FilePath = Application.StartupPath + @"\BarcodeNotification.html"; String content = System.IO.File.ReadAllText(FilePath, Encoding.UTF8); String message = mailAdapter.sendSingleEmail(databaseId, workspaceId, emailTypeId, Settings.Default.TripolisEmailId, mailData.sender, mailData.fromAddress, emailId, "Test", mailData.subject, content); }
private void btnQuery_Click(object sender, EventArgs e) { Cursor.Current = Cursors.WaitCursor; try { String client = Settings.Default.TripolisClient; String userName = Settings.Default.TripoisUserName; String password = Settings.Default.TripolisPassword; ChinaUnionAdapter mailAdapter = new ChinaUnionAdapter(client, userName, password, null); String feeMonth = cboFeeBatch.Text.Substring(0, 7) + "-01"; DateTime startTime = DateTime.Parse(feeMonth).AddMonths(1); DateTime endTime = startTime.AddMonths(2); ReportData reportData = mailAdapter.getRerportByJobId(this.cboFeeBatch.SelectedValue.ToString(), startTime, endTime); this.dgvSent.Rows.Clear(); dgvSent.Columns.Clear(); dgvSent.Columns.Add("邮件地址", "邮件地址"); dgvSent.Columns.Add("代理商编号", "代理商编号"); dgvSent.Columns.Add("代理商名称", "代理商名称"); dgvSent.Columns.Add("联系人", "联系人"); //已发送 if (reportData != null && reportData.sent != null && reportData.sent.Length > 0) { for (int i = 0; i < reportData.sent.Length; i++) { dgvSent.Rows.Add(); DataGridViewRow row = dgvSent.Rows[i]; for (int j = 0; j < dgvSent.ColumnCount && j<reportData.sent[i].contactFields.Length; j++) { if (reportData.sent[i].contactFields != null ) { row.Cells[j].Value = reportData.sent[i].contactFields[j] == null ? "" : reportData.sent[i].contactFields[j].value; } } } } this.tabSent.Text = "已发送(Sent)【" + dgvSent.RowCount + "】"; this.dgvBounced.Rows.Clear(); dgvBounced.Columns.Clear(); dgvBounced.Columns.Add("邮件地址", "邮件地址"); dgvBounced.Columns.Add("代理商编号", "代理商编号"); dgvBounced.Columns.Add("代理商名称", "代理商名称"); dgvBounced.Columns.Add("联系人", "联系人"); dgvBounced.Columns.Add("退回时间", "退回时间"); dgvBounced.Columns.Add("退回原因", "退回原因"); //已退回 if (reportData != null && reportData.bounced != null && reportData.bounced.Length > 0) { ArrayList emailList = new ArrayList(); for (int i = 0; i < reportData.bounced.Length; i++) { if (reportData.bounced[i].contactFields != null && emailList.Contains(reportData.bounced[i].contactFields[0].value)) { continue; } dgvBounced.Rows.Add(); DataGridViewRow row = dgvBounced.Rows[dgvBounced.RowCount - 1]; emailList.Add(reportData.bounced[i].contactFields[0].value); for (int j = 0; j < dgvBounced.ColumnCount - 2 && j < reportData.bounced[i].contactFields.Length ; j++) { if (reportData.bounced[i].contactFields != null) { row.Cells[j].Value = reportData.bounced[i].contactFields[j] == null ? "" : reportData.bounced[i].contactFields[j].value; } } row.Cells[4].Value = reportData.bounced[i].bouncedAt.ToString("yyyy-MM-dd hh:mm:ss"); row.Cells[5].Value = reportData.bounced[i].bounceCode + "-" + reportData.bounced[i].bounceCategoryDescription + "-" + reportData.bounced[i].bounceReason; } if (reportData != null && reportData.skipped != null && reportData.skipped.Length > 0) { for (int i = 0; i < reportData.skipped.Length; i++) { dgvBounced.Rows.Add(); DataGridViewRow row = dgvBounced.Rows[dgvBounced.RowCount - 1]; for (int j = 0; j < dgvBounced.ColumnCount - 1 && j < reportData.skipped[i].contactFields.Length; j++) { if (reportData.skipped[i].contactFields != null) { row.Cells[j].Value = reportData.skipped[i].contactFields[j].value == null ? "" : reportData.skipped[i].contactFields[j].value; } } row.Cells[4].Value = reportData.skipped[i].skippedAt.ToString("yyyy-MM-dd hh:mm:ss"); } } } this.tabBounced.Text = "已退回(Bounced)【" + dgvBounced.RowCount + "】"; ////已跳过 //if (reportData != null && reportData.skipped != null && reportData.skipped.Length > 0) //{ // this.dgvSkipped.Rows.Clear(); // dgvSkipped.Columns.Clear(); // dgvSkipped.Columns.Add("邮件地址", "邮件地址"); // dgvSkipped.Columns.Add("代理商编号", "代理商编号"); // dgvSkipped.Columns.Add("代理商名称", "代理商名称"); // dgvSkipped.Columns.Add("联系人", "联系人"); // dgvSkipped.Columns.Add("跳过时间", "跳过时间"); // for (int i = 0; i < reportData.skipped.Length; i++) // { // dgvSkipped.Rows.Add(); // DataGridViewRow row = dgvSkipped.Rows[i]; // for (int j = 0; j < dgvSkipped.ColumnCount - 1; j++) // { // if (reportData.skipped[i].contactFields != null) // { // row.Cells[j].Value = reportData.skipped[i].contactFields[j].value; // } // } // row.Cells[4].Value = reportData.skipped[i].skippedAt.ToString("yyyy-MM-dd hh:mm:ss"); // } //} //this.tabSkipped.Text = "已跳过(Skipped)【" + dgvSkipped.RowCount + "】"; this.dgvOpened.Rows.Clear(); dgvOpened.Columns.Clear(); dgvOpened.Columns.Add("邮件地址", "邮件地址"); dgvOpened.Columns.Add("代理商编号", "代理商编号"); dgvOpened.Columns.Add("代理商名称", "代理商名称"); dgvOpened.Columns.Add("联系人", "联系人"); dgvOpened.Columns.Add("打开时间", "打开时间"); dgvOpened.Columns.Add("打开IP地址", "打开IP地址"); //已opened if (reportData != null && reportData.opened != null && reportData.opened.Length > 0) { ArrayList emailList = new ArrayList(); for (int i = 0; i < reportData.opened.Length ; i++) { if (reportData.opened[i].contact.contactFields != null && emailList.Contains(reportData.opened[i].contact.contactFields[0].value)) { continue; } dgvOpened.Rows.Add(); DataGridViewRow row = dgvOpened.Rows[dgvOpened.RowCount - 1]; emailList.Add(reportData.opened[i].contact.contactFields[0].value); for (int j = 0; j < dgvOpened.ColumnCount - 2 && j < reportData.opened[i].contact.contactFields.Length; j++) { if (reportData.opened[i].contact.contactFields != null) { row.Cells[j].Value = reportData.opened[i].contact.contactFields[j] == null ? "" : reportData.opened[i].contact.contactFields[j].value; } } row.Cells[4].Value = reportData.opened[i].openedAt.ToString("yyyy-MM-dd hh:mm:ss"); row.Cells[5].Value = reportData.opened[i].ip; } } this.tabOpened.Text = "已打开(Opened)【" + dgvOpened.RowCount + "】"; int RecipientsCnt = reportData.emailSummary.job.requestedNumberOfSend; int NotSentCnt = reportData.emailSummary.job.numberOfSkipped; int BounceCnt = reportData.emailSummary.softBounces + reportData.emailSummary.hardBounces; int SentCnt = RecipientsCnt - NotSentCnt - BounceCnt; int OpenCnt = reportData.emailSummary.uniqueOpens; int NotOpenCnt = SentCnt - OpenCnt; this.lblRecipients.Text = RecipientsCnt + "/100%"; this.lblDelivered.Text = SentCnt + "/" + (((Double)SentCnt) / ((Double)RecipientsCnt)).ToString("p2"); this.lblBounced.Text = BounceCnt + "/" + (((Double)BounceCnt) / ((Double)RecipientsCnt)).ToString("p2"); this.lblNotSent.Text = NotSentCnt + "/" + (((Double)NotSentCnt) / ((Double)RecipientsCnt)).ToString("p2"); if (dgvBounced.RowCount > 0) { this.btnReSend.Enabled = true; } else { this.btnReSend.Enabled = false; } this.dgvSent.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; this.dgvOpened.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; // this.dgvSkipped.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; this.dgvBounced.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; Cursor.Current = Cursors.Default; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// 异步 开始事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void worker_DoWork(object sender, DoWorkEventArgs e) { //需要执行的代码 Cursor.Current = Cursors.WaitCursor; if (dgAgentFee.RowCount <= 0) { return; } try{ MailData mailData = new MailData(); StringBuilder sbAgent = new StringBuilder(); worker.ReportProgress(1, "准备数据...\r\n"); WechatAction wechatAction = new WechatAction(); for (int i = 0; i < dgAgentFee.RowCount; i++) { if (dgAgentFee[4, i].Value == null) { continue; } if (String.IsNullOrEmpty(dgAgentFee[4, i].Value.ToString())) { continue; } StringBuilder sb = new StringBuilder(); String url = String.Format(Settings.Default.Wechat_Message, dgAgentFee[0, i].Value.ToString(), feeMonth); wechatAction.sendTextMessageToWechat(dgAgentFee[0, i].Value.ToString(), feeMonth + url, Settings.Default.Wechat_Secret, Settings.Default.Wechat_Agent_AppId); sb.Append("agent_no#").Append(dgAgentFee[0, i].Value == null ? "" : dgAgentFee[0, i].Value.ToString()).Append(","); sb.Append("agent_name#").Append(dgAgentFee[1, i].Value == null ? "" : dgAgentFee[1, i].Value.ToString().Replace(",", "、").Replace("#", "%")).Append(","); sb.Append("agent_type#").Append(dgAgentFee[2, i].Value == null ? "" : dgAgentFee[2, i].Value.ToString().Replace(",", "、").Replace("#", "%")).Append(","); sb.Append("agent_type_comment#").Append(dgAgentFee[3, i].Value == null ? "" : dgAgentFee[3, i].Value.ToString().Replace(",", "、").Replace("#", "%")).Append(","); sb.Append("email#").Append(dgAgentFee[4, i].Value == null ? "" : dgAgentFee[4, i].Value.ToString()); mailData.ContactJsonList.Add(sb.ToString()); } String client = Settings.Default.TripolisClient; String userName = Settings.Default.TripoisUserName; String password = Settings.Default.TripolisPassword; mailData.fromAddress = Settings.Default.MailFromAddress; mailData.replyAddress = Settings.Default.MailReplyAddress; mailData.sender = Settings.Default.MailSender; mailData.subject = this.subject; ChinaUnionAdapter mailAdapter = new ChinaUnionAdapter(client, userName, password, null); String databaseId = Settings.Default.TripolisDBId; String workspaceId = Settings.Default.TripolisWorkspaceId; String emailId = Settings.Default.TripolisDirectEmailId; worker.ReportProgress(1, "发送邮件...\r\n"); String message = mailAdapter.sendBatchMail(databaseId, workspaceId, emailId, mailData, this.dtFeeMonth.Value); if (message.Contains("OK:")) { String mailJobId = message.Substring(3); MailJob mailJob = new MailJob(); mailJob.feeMonth = feeMonth; mailJob.mailJobId = mailJobId; mailJob.subject = mailData.subject; mailJob.sendTime = this.dtFeeMonth.Value.ToString("yyyy-MM-dd hh:mm:ss"); MailJobDao mailJobDao = new MailJobDao(); mailJobDao.Delete(mailJob); mailJobDao.Add(mailJob); // WechatAction wechatAction = new WechatAction(); // wechatAction.sendMessageToWechat("@all",this.dtFeeMonth.Value.ToString("yyyy-MM") + Settings.Default.Wechat_Message); MessageBox.Show("邮件重新发送成功"); } else { MessageBox.Show(message); } } catch (Exception ex) { MessageBox.Show(ex.Message); } Cursor.Current = Cursors.Default; }
/// <summary> /// 异步 开始事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void worker_DoWork(object sender, DoWorkEventArgs e) { //需要执行的代码 Cursor.Current = Cursors.WaitCursor; try { String client = Settings.Default.TripolisClient; String userName = Settings.Default.TripoisUserName; String password = Settings.Default.TripolisPassword; ChinaUnionAdapter mailAdapter = new ChinaUnionAdapter(client, userName, password, null); MailData mailData = new MailData(); mailData.fromAddress = Settings.Default.MailFromAddress; mailData.replyAddress = Settings.Default.MailReplyAddress; mailData.sender = Settings.Default.MailSender; mailData.subject = this.subject; String databaseId = Settings.Default.TripolisDBId; String workspaceId = Settings.Default.TripolisWorkspaceId; String emailId = Settings.Default.TripolisDirectEmailId; worker.ReportProgress(1, "准备数据...\r\n"); WechatAction wechatAction = new WechatAction(); for (int i = 0; i < dgAgentFee.RowCount; i++) { if (dgAgentFee[4, i].Value == null) { continue; } if (String.IsNullOrEmpty(dgAgentFee[4, i].Value.ToString())) { continue; } StringBuilder sb = new StringBuilder(); String url = String.Format(Settings.Default.Wechat_Message, dgAgentFee[0, i].Value.ToString(), this.dateTimePicker1.Value.ToString("yyyy-MM")); wechatAction.sendTextMessageToWechat(dgAgentFee[0, i].Value.ToString(), this.feeMonth + url, Settings.Default.Wechat_Secret, Settings.Default.Wechat_Agent_AppId); sb.Append("agent_no#").Append(dgAgentFee[0, i].Value == null ? "" : dgAgentFee[0, i].Value.ToString()).Append(","); sb.Append("agent_name#").Append(dgAgentFee[1, i].Value == null ? "" : dgAgentFee[1, i].Value.ToString().Replace(",", "、").Replace("#", "%")).Append(","); sb.Append("agent_type#").Append(dgAgentFee[2, i].Value == null ? "" : dgAgentFee[2, i].Value.ToString().Replace(",", "、").Replace("#", "%")).Append(","); sb.Append("agent_type_comment#").Append(dgAgentFee[3, i].Value == null ? "" : dgAgentFee[3, i].Value.ToString().Replace(",","、").Replace("#","%")).Append(","); sb.Append("email#").Append(dgAgentFee[4, i].Value == null ? "" : dgAgentFee[4, i].Value.ToString()).Append(","); sb.Append("contact_name#").Append(dgAgentFee[5, i].Value == null ? "" : dgAgentFee[5, i].Value.ToString().Replace(",", "、").Replace("#", "%")).Append(","); sb.Append("agent_fee_seq#").Append(dgAgentFee[6, i].Value == null ? "" : dgAgentFee[6, i].Value.ToString()).Append(","); sb.Append("feemonth#").Append(this.feeMonth).Append(","); /// Dictionary<String, Dictionary<String, String>> CategoryMap = new Dictionary<string, Dictionary<String, String>>(); //按结账科目分类 int rowIndex = i; for (int j = 7; j < dgAgentFee.ColumnCount - 3; j++) { String headText = dgAgentFee.Columns[j].HeaderText; int locationIndex = headText.IndexOf("-"); int endIndex = headText.IndexOf("("); if (locationIndex == -1) { locationIndex = headText.IndexOf("-"); } if (endIndex == -1) { endIndex = headText.IndexOf("("); } String key = dgAgentFee.Columns[j].HeaderText.Substring(locationIndex + 1); if (endIndex != -1) { key = dgAgentFee.Columns[j].HeaderText.Substring(locationIndex + 1, endIndex - locationIndex - 1); } String value = dgAgentFee[j, rowIndex].Value == null ? "" : dgAgentFee[j, rowIndex].Value.ToString(); if (!CategoryMap.ContainsKey(key)) { Dictionary<String, String> valueMap = new Dictionary<string, string>(); valueMap.Add(headText, value); CategoryMap.Add(key, valueMap); } else { Dictionary<String, String> valueMap = CategoryMap[key]; valueMap.Add(headText, value); } } //int index = 1; int feeSeq = 1; int seq = 1; foreach (String itemKey in CategoryMap.Keys) { Dictionary<String, String> valueMap = CategoryMap[itemKey]; float subTotal = 0; foreach (String value in valueMap.Values) { if (!String.IsNullOrEmpty(value)) { subTotal = subTotal + float.Parse(value); } } sb.Append("fee_seq").Append(feeSeq.ToString()).Append("#").Append("").Append(","); sb.Append("fee_name").Append(feeSeq.ToString()).Append("#<b>").Append(itemKey.Replace(",", "、").Replace("#", "%")).Append("</b>,"); if (subTotal<=0) { sb.Append("fee").Append(feeSeq.ToString()).Append("#").Append("").Append(","); } else { sb.Append("fee").Append(feeSeq.ToString()).Append("#<b>").Append(subTotal).Append("</b>,"); } feeSeq++; foreach (String subKey in valueMap.Keys) { sb.Append("fee_name").Append(feeSeq.ToString()).Append("#").Append(subKey.Replace(",", "、").Replace("#", "%")).Append(","); if (String.IsNullOrEmpty(valueMap[subKey]) ||(!String.IsNullOrEmpty(valueMap[subKey]) && valueMap[subKey].Equals("0"))) { sb.Append("fee_seq").Append(feeSeq.ToString()).Append("#").Append("").Append(","); sb.Append("fee").Append(feeSeq.ToString()).Append("#").Append("").Append(","); } else { sb.Append("fee_seq").Append(feeSeq.ToString()).Append("#").Append(seq.ToString()).Append(","); sb.Append("fee").Append(feeSeq.ToString()).Append("#").Append(valueMap[subKey]).Append(","); seq++; } feeSeq++; } } for (int j = feeSeq; j <= 101; j++) { sb.Append("fee_seq").Append(feeSeq.ToString()).Append("#").Append("").Append(","); sb.Append("fee_name").Append(feeSeq.ToString()).Append("#").Append("").Append(","); sb.Append("fee").Append(feeSeq.ToString()).Append("#").Append("").Append(","); feeSeq++; } sb.Append("fee_total_seq").Append("#").Append(seq.ToString()).Append(","); sb.Append("fee_total#").Append(dgAgentFee[dgAgentFee.Columns.Count - 3, i].Value.ToString()).Append(","); seq++; sb.Append("invoice_fee_seq").Append("#").Append(seq.ToString()).Append(","); sb.Append("invoice_fee#").Append(dgAgentFee[dgAgentFee.Columns.Count - 2, i].Value.ToString()).Append(","); seq++; sb.Append("pre_invoice_fee_seq").Append("#").Append(seq.ToString()).Append(","); sb.Append("pre_invoice_fee#").Append(dgAgentFee[dgAgentFee.Columns.Count - 1, i].Value.ToString()); mailData.ContactJsonList.Add(sb.ToString()); Cursor.Current = Cursors.Default; } worker.ReportProgress(2, "发送邮件...\r\n"); String message = mailAdapter.sendBatchMail(databaseId, workspaceId, emailId, mailData, dateTimePicker1.Value); if (message.Contains("OK:")) { String mailJobId = message.Substring(3); MailJob mailJob = new MailJob(); mailJob.feeMonth = this.feeMonth; mailJob.mailJobId = mailJobId; mailJob.subject = mailData.subject; if (dateTimePicker1.Value != null) { mailJob.sendTime = dateTimePicker1.Value.ToString("yyyy-MM-dd hh:mm:ss"); } MailJobDao mailJobDao = new MailJobDao(); mailJobDao.Delete(mailJob); mailJobDao.Add(mailJob); MessageBox.Show("邮件发送成功"); } else { MessageBox.Show(message); } } catch (Exception ex) { MessageBox.Show(ex.Message); } //this.Close(); }
private void btnSendTestMail_Click(object sender, EventArgs e) { try { frmMailAddress frmMailAddress = new frmMailAddress(); frmMailAddress.subject = "测试:" + Settings.Default.MailSubject + "(" + this.feeMonth + ")"; DialogResult dialogResult = frmMailAddress.ShowDialog(); if (dialogResult == DialogResult.OK) { String email = frmMailAddress.email; String subject = frmMailAddress.subject; if (!String.IsNullOrEmpty(email) && !String.IsNullOrWhiteSpace(email)) { Cursor.Current = Cursors.WaitCursor; String client = Settings.Default.TripolisClient; String userName = Settings.Default.TripoisUserName; String password = Settings.Default.TripolisPassword; ChinaUnionAdapter mailAdapter = new ChinaUnionAdapter(client, userName, password, null); MailData mailData = new MailData(); mailData.fromAddress = Settings.Default.MailFromAddress; mailData.replyAddress = Settings.Default.MailReplyAddress; mailData.sender = Settings.Default.MailSender; mailData.subject = subject; String databaseId = Settings.Default.TripolisDBId; String workspaceId = Settings.Default.TripolisWorkspaceId; String emailTypeId = Settings.Default.TripolisEmailTypeId; String message = mailAdapter.sendSingleEmail(databaseId, workspaceId, emailTypeId,null,mailData.sender, mailData.fromAddress, email,"Test", mailData.subject, this.webBrowser1.DocumentText); if (message.Contains("OK:")) { MessageBox.Show("邮件发送完成"); } else { MessageBox.Show(message); } Cursor.Current = Cursors.Default; } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }