using System; using System.Net; using System.Net.Mail;
classProgram { staticvoidMain(string[] args) { // 设置发件人和收件人 stringfrom = "src@example.com"; string to = "target@example.com"; MailAddress sendAddress = new MailAddress(from); MailAddress receiveAddress = new MailAddress(to);
// 创建邮件消息 MailMessage message = new MailMessage(from, to) { Subject = "Test Email", Body = "This is a test email from C#." }; message.Subject = "Test Email"; message.Body = "This is a test email from C#.";
邮件内容支持 HTML 格式,可以通过设置 IsBodyHtml 属性来指定邮件内容是否为 HTML 格式。
1 2 3 4 5 6 7
MailMessage message = new MailMessage(from, to) { Subject = "Test Email", Body = "<h1>This is a test email from C#.</h1>", // 设置邮件内容为 HTML 格式 IsBodyHtml = true };