测试之家淘宝店:主营软件测试定制服务 http://shop71136398.taobao.com/ 软件评测试考试论坛:http://www.testdao.com/forum-113-1.html 2012软件评测师考试群:28388329

批量发送邮件工具

上一篇 / 下一篇  2010-09-04 14:37:45 / 个人分类:自动化测试

最近一直在测试邮件系统,需要批量发送邮件,自己编写了一个批量发送邮件工具,具体参考了网上的代码,不过还是有待改善,因为感觉性能上比较慢,一些异常未处理,不过还是拿出来共享下,记录我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"x OR
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. Form51Testing软件测试网_L6O'NYt$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软件测试网;{-tY H'O0X
   InitializeComponent();51Testing软件测试网M6xR5NQ 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/S b0  {
$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;  
WE5D Bj:Pg0   myMail.Priority = MailPriority.High;  51Testing软件测试网 ?l0` jW7Z v7JK
          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[#fg 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#w j5N^2U1_+w? ]0            else51Testing软件测试网z ^/X2\l
            {
#uaBY8| A0             tbAttachment.Text = null;
&CfW(`'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#l DE@;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:kCH
    int counter = Convert.ToInt32(tbcounter.Text);
QI(s6ck%HmR0    int showcount =0;51Testing软件测试网/E/nYN/]1xwS
    for(int i =0; i<counter ; i++)
7r I(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软件测试网e1M Pj@%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软件测试网8g Bz$T|syTo&i&I
 }51Testing软件测试网m'b*i6\*s{\NN
}51Testing软件测试网a1T#@5Ecd(}jao
 
 

TAG:

 

评分:0

我来说两句

Open Toolbar