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

批量发送邮件工具

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

最近一直在测试邮件系统,需要批量发送邮件,自己编写了一个批量发送邮件工具,具体参考了网上的代码,不过还是有待改善,因为感觉性能上比较慢,一些异常未处理,不过还是拿出来共享下,记录我C#学习进度。
 
using System;51Testing软件测试网 ~(i*aL7yO$A
using System.Collections;
$JFi;I5TR k0using System.Drawing;51Testing软件测试网V.W _Y,d:L
using System.Windows.Forms;51Testing软件测试网gi4M*J{"L!Y*M
using System.Net;51Testing软件测试网e4jy:{ n5NQ0g
using System.Net.Mail;
,dT"LLi6ry0~0using System.Text;51Testing软件测试网4v9w{"pX)e
using System.IO;
namespace FromMailSend51Testing软件测试网+M4G`)},_
{
I?cggAI0 /// <summary>51Testing软件测试网X3d,M(|}d
 /// Description of MainForm.51Testing软件测试网%m4i~}v]rC+D
 /// </summary>
r/p|Sw5R&T0 public partial class MainForm. Form51Testing软件测试网:x2\)e dw'P
 {
CB0u0_ K0E/w0  public MainForm()
!iU+t!G*p!K,\a'r\B0  {51Testing软件测试网%M#^ V+|I
   //
gD'Ah(p`!t0   // The InitializeComponent() call is required for Windows Forms designer support.51Testing软件测试网.D}7gZHq u
   //51Testing软件测试网@&\t7uF}$t
   InitializeComponent();
?2M0N#A go]#n:{~ep \0   51Testing软件测试网&W)az%ZBLbi2L}
   //51Testing软件测试网.[Z6]4\n6y:rVB
   // TODO: Add constructor code after the InitializeComponent() call.51Testing软件测试网b$[8_U+j6z M(U Y'gE
   //51Testing软件测试网kh(ZH"j4Ne
  }51Testing软件测试网a P }2J'}E{
  void Button1Click(object sender, EventArgs e)51Testing软件测试网@B.zNJ'v
  {51Testing软件测试网NT G|Ws9G!g
   //mail message 
BO"YZNcU:V0   MailAddress from = new MailAddress("test@gmail.com");51Testing软件测试网Y$F7B4m ^'N;QC
   MailMessage myMail = new MailMessage();51Testing软件测试网)Jm ^[*uau%@
   myMail.To.Add(new MailAddress(ToTxt.Text));
%F{@@(V|-ys$i0   myMail.From = from;51Testing软件测试网m{;I6\,m O s
   myMail.Subject = SubjectTxt.Text;
/Se%wz?*N0   myMail.SubjectEncoding = Encoding.UTF8; 51Testing软件测试网BV~'y,tI
   myMail.Body = BodyTxt.Text;
OJ4dE0o#}.[0   myMail.BodyEncoding = Encoding.UTF8; 51Testing软件测试网%hR;]#Gv:V|
   myMail.IsBodyHtml = false;  51Testing软件测试网2]#?FC:W
   myMail.Priority = MailPriority.High;  
J p@W~b0          
'g1LLF#Le0    //Attachment
    string attahmentName=this.tbAttachment.Text.Trim().ToString();
2SM6p\C V0            if (!string.Equals(attahmentName, null) && !string.Equals(attahmentName, ""))
1e|9[#n9i-BW1W1z0            {51Testing软件测试网3M\K5|;X!e:HO C D s
                Attachment data = new Attachment(attahmentName, System.Net.Mime.MediaTypeNames.Application.Octet);
*zX;w2ul1xZ_u0                myMail.Attachments.Add(data);
W!sB"@ogh0            }
Zl#Ao gA_`w$f0            else51Testing软件测试网]dv;zZZ y'pM
            {51Testing软件测试网9k x:B f"M wq o6m/w
             tbAttachment.Text = null;51Testing软件测试网ya&AIR tKQ]
            }51Testing软件测试网3z'}*Y2^_
          
    //smtp client 51Testing软件测试网C)J?fIL
    SmtpClient mailsender = new SmtpClient(); 51Testing软件测试网QvlVJ
    mailsender.Host = "smtp.gmail.com"; 51Testing软件测试网H#OmzGEe
    mailsender.Port = 587;  51Testing软件测试网Ey"k;t4n9d VP!m
    mailsender.Credentials = 
8M BT^*uaY0     new NetworkCredential("test@gmail.com" ,"test");51Testing软件测试网;q-khp l}9S;C
    mailsender.DeliveryMethod = SmtpDeliveryMethod.Network;  51Testing软件测试网.x!yn2@0d,T2e;Q
    mailsender.EnableSsl = true;51Testing软件测试网Y'`)?h^5U`#O
    51Testing软件测试网 bLG%x@+{
   try 
d qC"{9gm;v5J$@%p0   {  
-H7Q*q[E,b0    int counter = Convert.ToInt32(tbcounter.Text);
ty&LMd0    int showcount =0;51Testing软件测试网0r)mhy Li0_c
    for(int i =0; i<counter ; i++)
!Lh`?&g0    {51Testing软件测试网 L)I c^2R~-J;N
     mailsender.Send(myMail);51Testing软件测试网6G#jWFI?P r|
     showcount++;  
o Wkn`u#i0     lbshowcount.Text = Convert.ToString(showcount);
a/~9l+b }R%iV `&c;nd*T0     this.Refresh();
)]1Zl ~g0U.^0    }     
P7EK6HM%g0    MessageBox.Show("Success");  51Testing软件测试网,O Z#zA(M^hO3N;I:n
   } 51Testing软件测试网 M%qn*G(i*l
   catch (Exception ex) 
.}"bC(n%y-f/l0   {  
iyF])J%v'[x0    MessageBox.Show(ex.ToString());51Testing软件测试网,h,v~CS8c
   }
_;p D9kTR0  }
.nE z*d(c5`&QOa K0   
%LQ+Yj,BA| m0  void BtnBrowseClick(object sender, EventArgs e)51Testing软件测试网"dJ8w*q1H*N3S0J})N
  {
!x9a1D&X&c-]0A5PM0   OpenFileDialog fda = new OpenFileDialog();
f-w(`E[0    ofda.Filter = "Text Files(*.TXT)|*.TXT||";
   if(ofda.ShowDialog() == DialogResult.OK)
R2MX lz$N X&S8`0   {
fWeV bP'X0    51Testing软件测试网,c.T:`yrhEk@v
    this.tbAttachment.Text = ofda.FileName;51Testing软件测试网8]V#g,|v9`$LoL9m
   }51Testing软件测试网v0d:n6g_1U9T
  }
~(K:A8i:\Y3|+k0 }
'O"M;d xr$V6[qE0}51Testing软件测试网 P+y${F\0n
 
 

TAG:

 

评分:0

我来说两句

Open Toolbar