程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#郵件發送 批量發送郵件

C#郵件發送 批量發送郵件

編輯:C#入門知識

 public void Send()
        {
        	 MailMessage mail = new MailMessage();
        	  try
            {
            	 //設置郵件的標題
            	 mail.Subject ="悠哉WebService報告";
            	 mail.SubjectEncoding = System.Text.Encoding.UTF8;
            	   //設置郵件的發件人
            	    string SendEmail = ConfigurationManager.AppSettings["SendEmail"].ToString();
            	     MailAddress Addressfrom = new MailAddress(SendEmail, "uzai", System.Text.Encoding.UTF8); //郵件的發件人
            	    mail.From = Addressfrom;
            	      /*  這裡這樣寫是因為可能發給多個聯系人,每個地址用 , 號隔開
                     */
                     List mailAddress = new List();
                foreach (string address in mailAddress)
                {
                    if (address != string.Empty)
                    {
                        mail.To.Add(new MailAddress(address, address, System.Text.Encoding.UTF8));
                    }
                }
                  //設置郵件的內容
                mail.Body = mailContent;
                 //設置郵件的格式
                mail.BodyEncoding = System.Text.Encoding.UTF8;
                 //mail.IsBodyHtml = true;
                   //設置郵件的發送級別
                mail.Priority = MailPriority.Normal;
                //發送通知
                   mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
                     SmtpClient client = new SmtpClient();
                        //設置用於 SMTP 事務的主機的名稱,填IP地址也可以了
                         client.Host = "smtp.gmail.com";
                          client.Port = 25;
                           client.Timeout = 9999;
                             client.UseDefaultCredentials = true;
                client.EnableSsl = true;
                 //郵箱登陸名和密碼
                string EmailLogName = ConfigurationManager.AppSettings["EmailLogName"].ToString();
                string EmailLogPwd = ConfigurationManager.AppSettings["EmailLogPwd"].ToString();
                client.Credentials = new System.Net.NetworkCredential(EmailLogName, EmailLogPwd);
                  client.DeliveryMethod = SmtpDeliveryMethod.Network;
                    //發送email
                client.Send(mail);
                 }
            catch (Exception)
            {
            	  //當郵件發送失敗,發送異常時 使用備用方法調用備用郵箱發送
                SendBackUp();
                 }
            finally
            {
                mail.Dispose();
            }
        }
        public void SendBackUp()
        {
        	 MailMessage mail = new MailMessage();
        	  try
            {
            	 //設置郵件的標題
            	 mail.Subject ="悠哉WebService報告";
            	 mail.SubjectEncoding = System.Text.Encoding.UTF8;
            	   //設置郵件的發件人
            	    string SendEmail = ConfigurationManager.AppSettings["SendEmail"].ToString();
            	     MailAddress Addressfrom = new MailAddress(SendEmail, "uzai", System.Text.Encoding.UTF8); //郵件的發件人
            	    mail.From = Addressfrom;
            	      /*  這裡這樣寫是因為可能發給多個聯系人,每個地址用 , 號隔開
                     */
                     List mailAddress = new List();
                foreach (string address in mailAddress)
                {
                    if (address != string.Empty)
                    {
                        mail.To.Add(new MailAddress(address, address, System.Text.Encoding.UTF8));
                    }
                }
                  //設置郵件的內容
                mail.Body = mailContent;
                 //設置郵件的格式
                mail.BodyEncoding = System.Text.Encoding.UTF8;
                 //mail.IsBodyHtml = true;
                   //設置郵件的發送級別
                mail.Priority = MailPriority.Normal;
                //發送通知
                   mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
                     SmtpClient client = new SmtpClient();
                        //設置用於 SMTP 事務的主機的名稱,填IP地址也可以了
                         client.Host = "smtp.gmail.com";
                          client.Port = 25;
                           client.Timeout = 9999;
                             client.UseDefaultCredentials = true;
                client.EnableSsl = true;
                 //郵箱登陸名和密碼
                string EmailLogName = ConfigurationManager.AppSettings["EmailLogName"].ToString();
                string EmailLogPwd = ConfigurationManager.AppSettings["EmailLogPwd"].ToString();
                client.Credentials = new System.Net.NetworkCredential(EmailLogName, EmailLogPwd);
                  client.DeliveryMethod = SmtpDeliveryMethod.Network;
                    //發送email
                client.Send(mail);
                 }
            catch (Exception)
            {
            	  //當郵件發送失敗,發送異常時 使用備用方法調用備用郵箱發送
                Send();
                 }
            finally
            {
                mail.Dispose();
            }
        }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved