using System; using System.Net; using System.Net.Mail; class Program { static void Main() { // 設定 SMTP 伺服器 SmtpClient client = new SmtpClient("smtp.example.com") { Port = 587, // 或其他端口 Credentials = new NetworkCredential("your_email@example.com", "your_password"), EnableSsl = true, }; // 建立郵件 MailMessage mail = new MailMessage { From = new MailAddress("your_email@example.com"), Subject = "帶有 CSS 的電子郵件", Body = @" <html> <head> <style> body { font-family: Arial, sans-serif; } h1 { color: blue; } p { font-size: 14px; } </style> </head> <body> <h1>這是一封測試郵件</h1> <p>這封郵件包含內聯 CSS。</p> </body> </html>", IsBodyHtml = true, }; // 添加收件人 mail.To.Add("recipient@example.com"); try { // 發送郵件 client.Send(mail); Console.WriteLine("郵件已發送成功!"); } catch (Exception ex) { Console.WriteLine($"發送郵件時出錯: {ex.Message}"); } } }