C#.NET -自動讀取文本文件中Email地址,指定時間後自動發送郵件

-------------------
郵件處理髮送代碼段   
-------------------
//讀取文本文件中的郵件(文本文件中郵件地址按照一行一個存放)   
        private string EmailListRead()   
        {   
            StreamReader SR = new StreamReader("EmailList.txt");   
            string EmailListStr = SR.ReadToEnd();   
            return EmailListStr;   
        }   
  
        //郵件地址處理髮送   
        private void SendMailLocalhost()   
        {   
            MailMessage msg = new System.Net.Mail.MailMessage();   
  
            //逐行讀取郵件地址   
            string email = EmailListRead();   
            string[] emailall = email.Split('/n');   
            for (int i = 0; i < emailall.Length; i++)   
            {   
                if (emailall[i].Contains("@"))   
                {   
                    msg.To.Add(emailall[i].Trim());   
                }   
            }   
               
            //msg.To.Add("[email protected]");可以發送給多人    
            //msg.CC.Add("[email protected]");可以抄送給多人    
  
            msg.From = new MailAddress("[email protected]", "客流量自動導入", System.Text.Encoding.UTF8);   
            /* 上面3個參數分別是發件人地址(可以隨便寫),發件人姓名,編碼*/  
            msg.Subject = "客流量自動導入出現錯誤";//郵件標題                
            msg.SubjectEncoding = System.Text.Encoding.UTF8;//郵件標題編碼    
            msg.Body = this.ACTR_log;//郵件內容    
            msg.BodyEncoding = System.Text.Encoding.UTF8;//郵件內容編碼    
            msg.IsBodyHtml = false;//是否是HTML郵件    
            msg.Priority = MailPriority.High;//郵件優先級    
            //附件   
            //Attachment data = new Attachment("filename");   
            //msg.Attachments.Add(data);   
  
  
            SmtpClient client = new SmtpClient();   
            client.Credentials = new System.Net.NetworkCredential("uID", "uPWD");   
            client.Host = "/*輸入郵件服務器SMTP地址*/";   
            object userState = msg;   
            string mailEx = "";   
            try  
            {   
                client.SendAsync(msg, userState);   
                //MessageBox.Show("發送成功");   
            }   
            catch (System.Net.Mail.SmtpException ex)   
            {   
                mailEx = ex.ToString();   
                //MessageBox.Show(ex.Message, "發送郵件出錯");   
            }   
        }  
 1.建立Timer
private System.Windows.Forms.Timer timer1;
this.timer1 = new System.Windows.Forms.Timer(this.components);
//timer1的間隔時間爲1分鐘(從timer.start()開始計時)
this.timer1.Interval = 60000;
//timer1到時間後啓動發生的時間
this.timer1.Tick += new
System.EventHandler(this.timer1_Tick);
 
2.在需要觸發的事件裏開始計時
timer1.Enabled = true;
timer1.Start();
 
3.Timer1的啓動事件
private void timer1_Tick(object sender, EventArgs e)
{  
   //開始發送郵件
   SendMailLocalhost();      
}
 
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章