最近一直在
测试邮件系统,需要批量发送邮件,自己编写了一个批量发送邮件工具,具体参考了网上的代码,不过还是有待改善,因为感觉性能上比较慢,一些异常未处理,不过还是拿出来共享下,记录我C#学习进度。
using System;
W6B$[ N~u8~e
U
h9vq0using System.Collections;
.nJ;Gw/vkl%L7Ib0using System.Drawing;51Testing软件测试网 XdnBW
using System.Windows.Forms;51Testing软件测试网9RZmO+[s4W{
using System.Net;51Testing软件测试网*Ce[BM)H"xOR
using System.Net.Mail;
9C-bz2vN-ZE|/BF0using System.Text;51Testing软件测试网2u"c0|z#w3IO3K
using System.IO;
namespace FromMailSend
,oGC-Ue3u0{
51Testing软件测试网+w)A4_VSok;H8B /// <summary>
51Testing软件测试网v,`+l'YOJ /// Description of MainForm.
51Testing软件测试网5E K1JUOQ!Q /// </summary>
t._&|#t!oC8b3x0 public partial class MainForm. Form
51Testing软件测试网_L6O'N Yt$Pip {
hs_R@^0 public MainForm()
O,n*z~R\0 {
mp}6z~n0 //
1E7U OHc:R0 // The InitializeComponent() call is required for
Windows Forms designer support.
51Testing软件测试网m8T*l!fX!oHx;[ //
51Testing软件测试网;{-tYH'O0X InitializeComponent();
51Testing软件测试网M6x R5NQ W 51Testing软件测试网,d;q%m RGz`t //
51Testing软件测试网'hE1NU.N8y])@8E // TODO: Add constructor code after the InitializeComponent() call.
51Testing软件测试网2Q"x.t
["FHKo //
'a/Rd/iC0 }
;|N
U^Xw0 void Button1Click(object sender, EventArgs e)
D{6d,UV/Sb0 {
$th,h~7s1J;}UL0 //mail message
/Ybwd\'u6t3q0cO0 MailAddress from = new MailAddress("
test@gmail.com");
~'y-ugF%O6U0 MailMessage myMail = new MailMessage();
%MMi
Y!rp;~}p0 myMail.To.Add(new MailAddress(ToTxt.Text));
DA%hf\;j0 myMail.From = from;
o|Ij/l2U9_h]0~#c3}Y0 myMail.Subject = SubjectTxt.Text;
51Testing软件测试网 x ~"kqW u myMail.SubjectEncoding = Encoding.UTF8;
51Testing软件测试网"O?7XHFM3\ myMail.Body = BodyTxt.Text;
}me*d'co1a0 myMail.BodyEncoding = Encoding.UTF8;
%p+W3qz/a?0 myMail.IsBodyHtml = false;
WE5DBj:Pg0 myMail.Priority = MailPriority.High;
51Testing软件测试网
?l0`
jW7Zv7JK 51Testing软件测试网&zc0f]WR3B(aRU;A-} //Attachment
string attahmentName=this.tbAttachment.Text.Trim().ToString();51Testing软件测试网#[O^
s@mz.`h(?
if (!string.Equals(attahmentName, null) && !string.Equals(attahmentName, ""))51Testing软件测试网4?'sc0Dz%d0{
{51Testing软件测试网$B2o$zVc[#f g Amm
Attachment data = new Attachment(attahmentName, System.Net.Mime.MediaTypeNames.Application.Octet);51Testing软件测试网:gE1?/V,a+`s
Y|&A
myMail.Attachments.Add(data);51Testing软件测试网-B/bpX;F
}
!_s#wj5N^2U1_+w?]0 else51Testing软件测试网z^/X2\l
{
#uaBY8|A0 tbAttachment.Text = null;
&Cf W(`'e-b:P0 }
'T8{pQ4SM,n0
//smtp client
51Testing软件测试网M j/Gd.Oy SmtpClient mailsender = new SmtpClient();
,F?#D#cE!y0 mailsender.Host = "smtp.gmail.com";
Eg`
W{CIL,zB0 mailsender.Port = 587;
51Testing软件测试网j#lDE@;sxe|K mailsender.Credentials =
~ t\1\\-t~1[
{2a0 new NetworkCredential("
test@gmail.com" ,"
test");
9T*IV,v
EJq't0 mailsender.DeliveryMethod = SmtpDeliveryMethod.Network;
f;YoEf/z;a{0 mailsender.EnableSsl = true;
51Testing软件测试网 T
}w5eW,i&f@1z&v0R +_8?w%gySi~0 try
51Testing软件测试网5t2wEZ\hzt~8L~ {
51Testing软件测试网
UQ|*X?R:k CH int counter = Convert.ToInt32(tbcounter.Text);
QI(s6c k%HmR0 int showcount =0;
51Testing软件测试网/E/nYN/]1xwS for(int i =0; i<counter ; i++)
7rI(D*hb4uBN0 {
51Testing软件测试网 @eYS:H!u;m,u8P+j mailsender.Send(myMail);
9qr`~U)Zd6s0 showcount++;
51Testing软件测试网W]$Y8q2Y(D&v lbshowcount.Text = Convert.ToString(showcount);
51Testing软件测试网e1MPj@%QM}4w this.Refresh();4l
S4mDfZu0 }
51Testing软件测试网4Zohn U"A `}q MessageBox.Show("Success");
51Testing软件测试网%p:N#iz v(P1u\ k }
51Testing软件测试网9NM*n)Ol@Z5b catch (Exception ex)
51Testing软件测试网b2gK
HIb'e {
F8k%d3l(Pl$B/Qm_0 MessageBox.Show(ex.ToString());
51Testing软件测试网j4g^b
yyAS }
$|CvBY2RK"g0 }
"s*c+LHv
A*Hv0 HMSx%|0 void BtnBrowseClick(object sender, EventArgs e)
G'].e/H^0 {
.l.cb
ESf.[ k0 OpenFileDialog fda = new OpenFileDialog();
7G'Ay2oC'b0 ofda.Filter = "Text Files(*.TXT)|*.TXT||";
if(ofda.ShowDialog() == DialogResult.OK)
F6?g
rf%Sj:Oj0 {
/`OZ_@x8v*R0
#P"q1lNv^7AM0 this.tbAttachment.Text = ofda.FileName;51Testing软件测试网L(L#JY|7{p\
}
Ep mT%O
|0 }51Testing软件测试网8gBz$T|syTo&i&I
}51Testing软件测试网m'b*i6\*s{\NN
}51Testing软件测试网a1T#@5Ecd(}jao