【问题标题】:How to open Outlook new mail window c#如何打开Outlook新邮件窗口c#
【发布时间】:2020-10-08 03:43:56
【问题描述】:

我正在寻找一种方法来在 Outlook 窗口中打开新邮件。

我需要以编程方式填写:发件人、收件人、主题、正文信息,但让这个新邮件窗口保持打开状态,以便用户可以验证内容/添加内容,然后像正常的 Outlook 消息一样发送。

发现:

Process.Start(String.Format(
 "mailto:{0}?subject={1}&cc={2}&bcc={3}&body={4}", 
  address, subject, cc, bcc, body))

但是没有“发件人”选项(我的用户有多个邮箱...)

有什么建议吗?

【问题讨论】:

  • 您是否有理由不将 vbscript 用于 Outlook 本身的应用程序?最终,如果您愿意,您可以轻松地从 c# 启动 Outlook 应用程序,在 Outlook 中有一个在启动时运行的规则,该规则会从 vbscript 宏中为您填充它。正如您期望在 gui 中编辑它一样,我的问题仍然存在:您是否需要特定于 c# 的解决方案,或者您只是在您认为必须使用 c# 时才询问?

标签: c# email .net-4.0 office-interop outlook-2003


【解决方案1】:

我终于解决了这个问题。 这是解决我的问题的一段代码(使用 Outlook 互操作)

Outlook.Application oApp    = new Outlook.Application ();
Outlook._MailItem oMailItem = (Outlook._MailItem)oApp.CreateItem ( Outlook.OlItemType.olMailItem );
oMailItem.To    = address;
// body, bcc etc...
oMailItem.Display ( true );

【讨论】:

  • 当我在 Outlook 未打开的情况下运行类似的操作时,Outlook 会打开并且我会看到模式电子邮件对话框,但是一旦用户点击发送并且电子邮件卡在发件箱中,Outlook 就会立即关闭。有人遇到过这个问题吗?
  • 这需要哪些程序集/引用?谢谢。
  • 只是 Outlook 互操作
  • @Taersious 在我的机器上,互操作文件在这里:C:\Program Files (x86)\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office12\Microsoft.Office.Interop.Outlook .dll 我通过在 VS 的参考管理器中搜索 Outlook 的框架程序集作为参考添加。 X
【解决方案2】:

这是我尝试过的。它按预期工作。

此应用程序添加收件人,添加抄送并添加主题并打开一个新的邮件窗口。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using Outlook = Microsoft.Office.Interop.Outlook;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ButtonSendMail_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());
        }
    }
}

【讨论】:

  • 如果用户使用 Outlook 取消按钮取消发送电子邮件,您如何取消发送电子邮件?
  • 代码只是生成一个新的电子邮件窗口,其中填充了收件人:和主题字段并显示它。发送(或不发送)电子邮件由用户决定,因此如果电子邮件窗口关闭(未发送),则不会发送电子邮件。
【解决方案3】:

你不能用 mailto 做到这一点。您的客户必须选择他们发送邮件的帐户,默认为他们的默认帐户,或者您必须在发送电子邮件时提供邮件表单并设置标题。

【讨论】:

  • 我知道我不能这样做 - 这就是为什么我正在寻找不同的选项 - 看到一些应用程序管理它,所以必须以某种方式......
猜你喜欢
  • 2011-05-31
  • 2016-05-30
  • 1970-01-01
  • 2023-03-13
  • 2013-10-08
  • 1970-01-01
  • 2012-07-29
  • 1970-01-01
  • 2017-12-09
相关资源
最近更新 更多