簡體   English   中英

使用Windows窗體創建新的Outlook電子郵件

[英]Create new outlook email using Windows Forms

這可能是一個非常初學者的問題。 我正在嘗試創建我的第一個Windows Forms應用程序,並想通過單擊表單上的按鈕來創建Outlook電子郵件。

問題是有13個錯誤,主要是說:

嚴重性代碼說明項目文件行抑制狀態錯誤CS0246找不到類型或名稱空間名稱“ Outlook”(是否缺少using指令或程序集引用?)報價計算機v.0.0.1 C:\\ Users \\ PC \\ source \\ repos \\優惠機v.0.0.1 \\優惠機v.0.0.1 \\ Form1.cs 29有效

我添加了對我的項目的引用:

在此處輸入圖片說明

這是代碼:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Offer_machine_v._0._0._1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> lstAllRecipients = new List<string>();
                //Below is hardcoded - can be replaced with db data
                lstAllRecipients.Add("sanjeev.kumar@testmail.com");
                lstAllRecipients.Add("chandan.kumarpanda@testmail.com");

                Outlook.Application outlookApp = new Outlook.Application();
                Outlook._MailItem oMailItem = (Outlook._MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
                Outlook.Inspector oInspector = oMailItem.GetInspector;
                // Thread.Sleep(10000);

                // Recipient
                Outlook.Recipients oRecips = (Outlook.Recipients)oMailItem.Recipients;
                foreach (String recipient in lstAllRecipients)
                {
                    Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(recipient);
                    oRecip.Resolve();
                }

                //Add CC
                Outlook.Recipient oCCRecip = oRecips.Add("THIYAGARAJAN.DURAIRAJAN@testmail.com");
                oCCRecip.Type = (int)Outlook.OlMailRecipientType.olCC;
                oCCRecip.Resolve();

                //Add Subject
                oMailItem.Subject = "Test Mail";

                // body, bcc etc...

                //Display the mailbox
                oMailItem.Display(true);
            }
            catch (Exception objEx)
            {
                Response.Write(objEx.ToString());
            }
        }

        private void Label1_Click(object sender, EventArgs e)
        {

        }
    }
}

您沒有在代碼中添加適當的用法。 您需要添加:

using Microsoft.Office.Interop.Outlook;

如果沒有此行,則應在Interop庫中的每個對象之前鍵入完整的名稱空間。 使用該功能后,您可以刪除所有Outlook. 在來自互操作的對象之前。 但是創建主Application對象的對象需要完整的名稱空間,以避免與Winforms中定義的Application類沖突。

Microsoft.Office.Interop.Outlook.Application outlookApp = 
                   new Microsoft.Office.Interop.Outlook.Application();
_MailItem oMailItem = (_MailItem)outlookApp.CreateItem(OlItemType.olMailItem);
Inspector oInspector = oMailItem.GetInspector;

..... and so on ....

看來您已經兩次將Outlook互操作添加到項目“引用”中。

Outlook自動化參考

對於錯誤消息,您只需要向Outlook名稱空間添加別名:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;

另外,您可能會發現C#應用程序自動執行Outlook(CSAutomateOutlook)示例項目很有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM