.NET 在 Office 2010 中创建带附件的电子邮件

发布于 2024-10-29 21:00:18 字数 165 浏览 3 评论 0原文

我知道通过 mailto 链接,您可以打开默认邮件客户端并填充主题和标题。我需要做类似的事情,但还要附上一份文件。

我的所有用户都将使用 Outlook 2010,并将其设置为默认邮件客户端。它只需要适用于这种情况。

如何创建打开 Outlook 新邮件窗口并填充附件字段的电子邮件?

I know with an mailto link you can open your defautl mail client and populate a subject and a title. I need to do something similar but also attach a document.

All my users will be using Outlook 2010 and it will be set as there default mail client. It only needs to work for that case.

How can you create an email that opens up the Outlook new message window and populates the attachment field?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往事随风而去 2024-11-05 21:00:18

您需要对 Outlook COM 库的引用,然后类似的内容应该可以工作:

    /// <summary>
    /// Get Application Object
    /// </summary>
    public static OL.Application Application
    {
        get
        {
            try
            {
                return Marshal.GetActiveObject("Outlook.Application") as OL.Application;
            }
            catch (COMException)
            {
                return new OL.Application();
            }
        }
    }

    /// <summary>
    /// Prepare An Email In Outlook
    /// </summary>
    /// <param name="ToAddress"></param>
    /// <param name="Subject"></param>
    /// <param name="Body"></param>
    /// <param name="Attachment"></param>
    public static void CreateEmail(string ToAddress, string Subject, string Body, string AttachmentFileName)
    {
        //Create an instance of Outlook (or use existing instance if it already exists
        var olApp = Application;

        // Create a mail item
        var olMail = olApp.CreateItem(OL.OlItemType.olMailItem) as OL.MailItem;
        olMail.Subject = Subject;
        olMail.To = ToAddress;

        // Set Body
        olMail.Body = Body;

        // Add Attachment
        string name = System.IO.Path.GetFileName(AttachmentFileName);
        olMail.Attachments.Add(AttachmentFileName, OL.OlAttachmentType.olByValue, 1, name);

        // Display Mail Window
        olMail.Display();
    }

为此,您还需要:

using System.Runtime.InteropServices;
using OL = Microsoft.Office.Interop.Outlook;

You'll need a reference to the Outlook COM library, then something like this should work:

    /// <summary>
    /// Get Application Object
    /// </summary>
    public static OL.Application Application
    {
        get
        {
            try
            {
                return Marshal.GetActiveObject("Outlook.Application") as OL.Application;
            }
            catch (COMException)
            {
                return new OL.Application();
            }
        }
    }

    /// <summary>
    /// Prepare An Email In Outlook
    /// </summary>
    /// <param name="ToAddress"></param>
    /// <param name="Subject"></param>
    /// <param name="Body"></param>
    /// <param name="Attachment"></param>
    public static void CreateEmail(string ToAddress, string Subject, string Body, string AttachmentFileName)
    {
        //Create an instance of Outlook (or use existing instance if it already exists
        var olApp = Application;

        // Create a mail item
        var olMail = olApp.CreateItem(OL.OlItemType.olMailItem) as OL.MailItem;
        olMail.Subject = Subject;
        olMail.To = ToAddress;

        // Set Body
        olMail.Body = Body;

        // Add Attachment
        string name = System.IO.Path.GetFileName(AttachmentFileName);
        olMail.Attachments.Add(AttachmentFileName, OL.OlAttachmentType.olByValue, 1, name);

        // Display Mail Window
        olMail.Display();
    }

For this to work you would also need:

using System.Runtime.InteropServices;
using OL = Microsoft.Office.Interop.Outlook;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文