c#發郵件小程序

1:發郵件功能如下:

  a:批量添加郵箱

  b:添加多個附件、並支持查看附件信息和移除附件。

2:因爲免費,所以126,136服務器人家對咱 有控制:(我測試了126,136郵件)

  1:郵箱雖已分組發送,但一次只能發送35個之內。

  2:附件大小控件在50M。

3:功能採用c#的SmtpClient和MailMessage實現,核心源代碼如下:


批量導入郵箱:
   
		void SendsClick(object sender, EventArgs e)
		{
			portCount = 0;
			textBox_addressee.Text = "";
			this.openFile.Filter = "txt文件(*.txt)|*.txt"; 
			if (this.openFile.ShowDialog() == DialogResult.OK)
		   	{
		       string picFileName = this.openFile.FileName;
		       if(string.IsNullOrEmpty(picFileName))
		       		return;
		       StreamReader sr = new StreamReader(picFileName,Encoding.Default);
		       
		       string mail = sr.ReadToEnd();
		       string inpa = @"\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+";
		       MatchCollection collection = Regex.Matches(mail,inpa,RegexOptions.ExplicitCapture|RegexOptions.IgnoreCase);
		       foreach (Match m in collection) {
			       	//if(!textBox_addressee.Text.Contains(m.Value)){
			       		textBox_addressee.AppendText(m.Value+",");
			       		portCount++;
			       	//}
		       }
		       MessageBox.Show("共找到:"+collection.Count+" 可用郵件,已導入:"+portCount+" 位聯繫人");
			}
		}
添加附件:
		void Button_choiceClick(object sender, EventArgs e)
		{
			this.openFile.Filter = "*.*|*.*"; 
			if (this.openFile.ShowDialog() == DialogResult.OK)
		   	{
		       string PicFileName = this.openFile.FileName; 
		       if(!string.IsNullOrEmpty(PicFileName) && !user.Files.ContainsValue(PicFileName)){
		       		fileLength += new FileInfo(PicFileName).Length;		       	
		       		if(fileLength>=(1024*1024*50))
					{
						MessageBox.Show("本次傳輸不能超過50M");
						fileLength -= new FileInfo(PicFileName).Length;//還原
						return;
					}
		       		comboBox_list.Items.Add(Path.GetFileName(PicFileName));
		           	user.Files.Add(Path.GetFileName(PicFileName),PicFileName);
		           	fileSize.Text = string.Format("已用{0}M,還剩下{1}M",fileLength/1024/1024,(49-(fileLength/1024/1024)));
	           	}
		    }
		}
分組郵件
		public void runTask()
		{
			MailService mailService = new MailService();
			String[] addresss = Regex.Split(user.Address,",");
			
			//求出分組次數		
			IList array = new ArrayList();
			StringBuilder sbTemp = null;	
			for (int y = 0; y < addresss.Length; y++) {
					if(sbTemp==null)
					{
						sbTemp = new StringBuilder();
					}
					sbTemp.Append(addresss[y]).Append(",");
					if(y!=0 && (y+1)%35==0){
						array.Add(sbTemp);
						sbTemp = null;
					}
			}	
			//剩下的
			if(sbTemp!=null && sbTemp.ToString().Contains("@")){
				array.Add(sbTemp);
			}
			MessageBox.Show("分"+array.Count+"組進行發送");
			foreach (StringBuilder sb in array) {
				user.Address = sb.ToString();
				String tag = mailService.sendMail(user);
				if(tag==massText.success)
				{
					isSendTrue = true; 
					errorMass.Text = massText.success;
					errorMass.ForeColor = Color.LimeGreen;
				}
				else
				{
					errorMass.Text = tag;
					errorMass.ForeColor = Color.Red;
				}
			}
		}
發郵件:	
		public String sendMail(User user){
			String tag = massText.success;
			user.Username = user.Username.Replace(user.Server,"");
			
			//服務器
			SmtpClient sc = new SmtpClient();
			sc.Host="smtp."+user.Server.Replace("@","");
			sc.DeliveryMethod = SmtpDeliveryMethod.Network;
			sc.UseDefaultCredentials = true;			
			sc.Credentials = new NetworkCredential(user.Username,user.Password);
			
			//郵件內容
			MailMessage message = new MailMessage();
			message.From = new MailAddress(string.Format("{0}{1}",user.Username,user.Server));
			
			try
			{
				string[] address = Regex.Split(user.Address+",",",");
				for (int i = 0; i < address.Length; i++) {
					if(!string.IsNullOrEmpty(address[i]))
					{
						if(!b){
							message.To.Add(address[i]);
						}else{
							if(i%4==0)
							message.CC.Add(address[i]);
							else
							message.Bcc.Add(address[i]);
						}
					}
						
				}
			}
			catch(Exception)
			{
				return massText.mailTitle;
			}
			
			//附件
			message.Subject = user.Title;
			message.Body = user.Context;
			Dictionary<String, String>.KeyCollection keyColl = user.Files.Keys;
			
			foreach (String fileName in keyColl) {
				string fileNamePath = user.Files[fileName];
				message.Attachments.Add(new Attachment(fileNamePath));
			}
			
			//設置編碼、發送級別
			message.BodyEncoding = Encoding.GetEncoding("utf-8");
			message.SubjectEncoding = Encoding.GetEncoding("utf-8");
			message.IsBodyHtml = true;
			message.Priority = MailPriority.Normal;
			
			/*委託任務,出現等待頁面
			Wait wait = null;
			((Action)delegate(){
			 	wait = new Wait();
			 	Thread.Sleep(1000);
			 	this.Invoke((Action)delegate(){
			 	    try {
			 	        wait.ShowDialog();
			 	        Thread.Sleep(2000);
						sc.Send(message);	
					} catch (Exception) {
						tag = massText.failure;
					}
			 	});
			 	
			 }).BeginInvoke(new AsyncCallback(theout),wait);*/
			sc.Send(message);	
			Thread.Sleep(1000);
			message.Dispose();
			sc.Dispose();
			b = true;
			return tag;
		}

程序執行代碼如下:





源代碼下載地址:http://download.csdn.net/detail/u012892431/7120379 點擊打開鏈接

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章