公司受金融危机影响,业务量大减,公司销售人员一天到晚在网上找客户名录发邮件,这天公司领导一拍脑袋“开发一个邮件群发器,销售人员提供客户通迅录,程序自动发送”,再而拍拍我肩膀"开发这个邮件群发器任务就交给你去处理了",虽然有三年多时间没有再做过WinForm,但也没办法只好硬着头皮拍拍胸脯"没问题"。
马上登录园子"WinForm 邮件"搜索,真庆辛结果还不少。照着大牛们的文章,很快就可以接发邮件了,看来园子不愧我们程序员的百科全书。
不过期间还是碰到了一些小问题,比如qq邮箱始终通不过身份验证(到现在也没解决,最后放弃了QQ邮箱),还在多线程进度显示的问题(最后也是在园子里找到解决思路),在此贴部份代码,以备忘。
1#region 发送邮件
2 private void SendEmail()
3 {
4 try
5 {
6 MethodInvoker mi = new MethodInvoker(this.ShowProcessBar);
7 this.BeginInvoke(mi);
8 MailMessage mail = new MailMessage();
9 mail.From = new MailAddress(txtAccount.Text + cmbServer.Text);
10 mail.Subject = txtSubject.Text;
11 mail.SubjectEncoding = System.Text.Encoding.UTF8;
12 mail.IsBodyHtml = isHTML.Checked;
13 mail.BodyEncoding = System.Text.Encoding.UTF8;
14 mail.Priority = MailPriority.High;
15 mail.ReplyTo = mail.From;
16 if (lstAttachFile.Items.Count>0)
17 {
18 Attachment atta = null;
19 foreach (object li in lstAttachFile.Items)
20 {
21 string fn = li.ToString();
22 if (System.IO.File.Exists(fn))
23 {
24 atta = new Attachment(fn);
25 ContentDisposition dispotion = atta.ContentDisposition;
26 dispotion.CreationDate = File.GetCreationTime(fn);
27 dispotion.ModificationDate = File.GetLastWriteTime(fn);
28 dispotion.ReadDate = File.GetLastAccessTime(fn);
29 mail.Attachments.Add(atta);
30 }
31 }
32 }
33 SmtpClient client = GetSmtpClien(cmbServer.Text);
34 int index = 1;
35 foreach (Contract item in lstContract)
36 {
37 mail.To.Clear();
38 mail.To.Add(new MailAddress(item.MailAddress));
39 StringBuilder sbHTML = new StringBuilder();
40 if (isHTML.Checked)
41 {
42
43 sbHTML.Append("<h4>" + item.CompanyName + "</h4>");
44 sbHTML.Append("<h4>" + item.Name + ":</h4>");
45 sbHTML.Append("<p>" + currentBodyHtml + "</p>");
46 sbHTML.Append("<hr/>");
47 sbHTML.Append("<p>" + txtSin.Text + "</p>");
48
49 }
50 else
51 {
52 sbHTML.Append(item.CompanyName + " ");
53 sbHTML.Append(item.Name + ": ");
54 sbHTML.Append(currentBodyHtml + " ");
55 sbHTML.Append(txtSin.Text);
56 }
57 mail.Body = sbHTML.ToString();
58 client.Send(mail);
59 if (IncreaseEvent != null)
60 {
61 IncreaseEvent(index);
62 }
63 index++;
64 }
65 //mail.
66 //object stateMsg = mail;
67 MessageBox.Show(this, "发送完毕!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
68 MethodInvoker mi2 = new MethodInvoker(this.HideProccessBar);
69 this.BeginInvoke(mi2);
70 }
71 catch (Exception ex)
72 {
73 MessageBox.Show(this, ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
74 MethodInvoker mi3 = new MethodInvoker(this.HideProccessBar);
75 this.BeginInvoke(mi3);
76 }
77 }
78 #endregion
79
80 #region 获取SmptClient
81 private SmtpClient GetSmtpClien(string serverType)
82 {
83 SmtpClient client = new SmtpClient("");
84 switch (serverType)
85 {
86 case "@163.com":
87 client.Host = "smtp.163.com";
88 client.UseDefaultCredentials = false;
89 client.EnableSsl = false;
90 client.DeliveryMethod = SmtpDeliveryMethod.Network;
91 client.Credentials = new System.Net.NetworkCredential(txtAccount.Text, txtPassword.Text);
92 break;
93 case "@qq.com":
94 client.Host = "smtp.qq.com";
95 client.UseDefaultCredentials = true;
96 client.EnableSsl = false;
97 client.Port = 25;
98 client.DeliveryMethod = SmtpDeliveryMethod.Network;
99 client.Credentials = new System.Net.NetworkCredential(txtAccount.Text, txtPassword.Text);
100 break;
101 case "@gmail.com":
102 client.Host = "smtp.gmail.com";
103 //client.UseDefaultCredentials = false;
104 client.EnableSsl = true;
105 client.Port = 587;
106 //client.DeliveryMethod = SmtpDeliveryMethod.Network;
107 client.Credentials = new System.Net.NetworkCredential(txtAccount.Text, txtPassword.Text);
108 break;
109 case "@winccess.com":
110 client.Host = "mail.winccess.com";
111 //client.UseDefaultCredentials = false;
112 client.EnableSsl = false;
113 //client.Port = 25;
114 //client.DeliveryMethod = SmtpDeliveryMethod.Network;
115 client.Credentials = new System.Net.NetworkCredential(txtAccount.Text + serverType, txtPassword.Text);
116 break;
117 default:
118 break;
119 }
120 return client;
121 }
122 #endregion