添加引用:Microsoft.Office.Interop.Outlook
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Mail; using System.Net; using Outlook = Microsoft.Office.Interop.Outlook; namespace Common { public class MailHelper { public static void SendMail() { Outlook.Application olApp = new Outlook.Application(); Outlook.MailItem mailItem = (Outlook.MailItem)olApp.CreateItem(Outlook.OlItemType.olMailItem); mailItem.To = "abc@163.com"; mailItem.Subject = DateTime.Now.ToString("yyyyMMdd") + "_報表"; mailItem.BodyFormat = Outlook.OlBodyFormat.olFormatHTML; string content = "附件為" + DateTime.Now.ToString("yyyyMMdd") + " 數據,請查閱,謝謝!"; content = "各收件人,<br/> <br/>請重點關注以下內容:<br/> <br/>" + content + "<br/> <br/><br/><br/>此郵件為系統自動郵件通知,請不要直接進行回復!謝謝。"; content = content + "<br/>\r\n <br/>Best Regards!\r\n <br/>\r\n <br/> \r\n <br/>==============================================\r\n \r\n <br/>\r\n <br/>\r\n \r\n ==============================================="; mailItem.HTMLBody = content; mailItem.Attachments.Add(@"c:\test.rar"); ((Outlook._MailItem)mailItem).Send(); mailItem = null; olApp = null; } } }
在Microsoft Office Outlook中需要配置一個發件人的郵件賬戶,如果是163之類的免費郵箱,則需要設置“客戶端授權密碼”,然后使用這個客戶端授權密碼替換郵箱的登錄密碼來進行配置,否則是發不了郵件的。
發送成功之后如下圖所示:

