C# 調用 Outlook發送郵件執行個體

來源:互聯網
上載者:User

標籤:

添加引用: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 = "[email protected]";            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之類的免費郵箱,則需要設定“用戶端授權密碼”,然後使用這個用戶端授權密碼替換郵箱的登入密碼來進行配置,否則是發不了郵件的。

發送成功之後如所示:

C# 調用 Outlook發送郵件執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.