C#調用Outlook來發送郵件

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   for   檔案   

原文:C#調用Outlook來發送郵件

     寫了一個簡單的Windows Form程式,實現利用Outlook來寄送電子郵件的功能。下面逐步講解如何?,再加上具體的代碼。

  1. 開啟VS2010, 建立一個Windows Form程式。
  2. 右擊工程檔案的Reference,選擇Add Reference。
  3. 點擊Com tab, 選擇Microsoft.Office.Interop.Outlook.dll,點擊 OK。
  4. 添加引用: using Outlook = Microsoft.Office.Interop.Outlook;
  5. 其它的就不說了,直接上代碼了。

 

View Code
 1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using Outlook = Microsoft.Office.Interop.Outlook;
10
11 namespace SendMailByOutlook
12 {
13 public partial class Form1 : Form
14 {
15 public Form1()
16 {
17 InitializeComponent();
18 }
19
20 private void button1_Click(object sender, EventArgs e)
21 {
22 Outlook.Application olApp = new Outlook.Application();
23 Outlook.MailItem mailItem = (Outlook.MailItem)olApp.CreateItem(Outlook.OlItemType.olMailItem);
24 mailItem.To = "[email protected]";
25 mailItem.Subject = "A test";
26 mailItem.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
27 mailItem.HTMLBody = "Hello world";
28 ((Outlook._MailItem)mailItem).Send();
29
30 mailItem = null;
31 olApp = null;
32 MessageBox.Show("Mail has been sent successfully!");
33
34
35 }
36 }
37 }


    最後,點擊Send,提示發送郵件成功!

    還有一點需要注意,就是說調用Outlook來發送郵件時,Outlook必須要開啟。否則,該程式會拋出異常。大家有什麼更好的辦法,歡迎交流!

       

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.