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; /* 這裡這樣寫是因為可能發給多個聯系人,每個地址用 , 號隔開 */ ListmailAddress = 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(); } }