This is an old revision of the document!
using System; using System.Net; using System.Net.Mail; class Program { static void Main() { // 設定郵件內容 MailMessage mail = new MailMessage(); mail.From = new MailAddress("your_email@example.com"); mail.To.Add("recipient@example.com"); mail.Subject = "這是一封 HTML 郵件"; // 設置郵件內容為 HTML string htmlBody = @" <html> <body> <h1>你好!</h1> <p>這是一封包含 PNG 圖片的 HTML 郵件。</p> <img src='cid:myImage' alt='My Image' /> </body> </html>"; mail.Body = htmlBody; mail.IsBodyHtml = true; // 添加 PNG 附件 Attachment inline = new Attachment("path_to_your_image.png"); inline.ContentId = "myImage"; inline.ContentDisposition.Inline = true; mail.Attachments.Add(inline); // 設定 SMTP 客戶端 SmtpClient smtp = new SmtpClient("smtp.example.com"); smtp.Port = 587; // 或者 465,根據你的 SMTP 伺服器設定 smtp.Credentials = new NetworkCredential("your_email@example.com", "your_password"); smtp.EnableSsl = true; // 發送郵件 try { smtp.Send(mail); Console.WriteLine("郵件發送成功!"); } catch (Exception ex) { Console.WriteLine("發送郵件時出錯: " + ex.Message); } finally { // 清理 mail.Dispose(); smtp.Dispose(); } } }