C# 使用 Outlook 发送电子邮件 - 发件箱中没有附件

发布于 2024-12-27 09:16:40 字数 1189 浏览 3 评论 0原文

我有一个程序使用 Outlook 发送带有附件的邮件。它工作正常,发送带有附件的电子邮件,但在发件箱中消息中没有附件。当有人收到邮件时,附件可见,但在发件箱中不可见。 下面是一些代码:

        Outlook.MailItem mail = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
        mail.BodyFormat = Outlook.OlBodyFormat.olFormatPlain;
        int iAttachType = (int)Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue;  
        mail.Attachments.Add(Application.StartupPath+"/"+attachment, iAttachType, null, attachment);
        mail.To = email;
        mail.Subject = "Something";
        mail.Body = "Some body";
        mail.Send();

在此之前我使用:

    private Outlook.Application outlookApp;
    private Outlook._NameSpace outlookNameSpace;
    private Outlook.MAPIFolder outbox;

并且

            outlookApp = new Outlook.Application();
            outlookNameSpace = outlookApp.GetNamespace("MAPI");
            outlookNameSpace.Logon(null, null, false, false);
            outbox = outlookNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox);

我的 Outlook 程序与 Microsoft Exchange Serwer 连接。当我使用用 C++ 编写的应用程序时,它将附件保存在发件箱的消息中。

谢谢帮助!

I have a program that is using Outlook to send messages with attachments. It is working ok, sending emails with attachments but in outbox there is no attachment in the message. When somebody receive the message the attachment is visible but in outbox not.
Here is some code:

        Outlook.MailItem mail = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
        mail.BodyFormat = Outlook.OlBodyFormat.olFormatPlain;
        int iAttachType = (int)Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue;  
        mail.Attachments.Add(Application.StartupPath+"/"+attachment, iAttachType, null, attachment);
        mail.To = email;
        mail.Subject = "Something";
        mail.Body = "Some body";
        mail.Send();

Before this I use:

    private Outlook.Application outlookApp;
    private Outlook._NameSpace outlookNameSpace;
    private Outlook.MAPIFolder outbox;

and

            outlookApp = new Outlook.Application();
            outlookNameSpace = outlookApp.GetNamespace("MAPI");
            outlookNameSpace.Logon(null, null, false, false);
            outbox = outlookNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox);

My outlook program is connected with Microsoft Exchange Serwer. When I was using an application written in C++ it saved attachment in messages in outbox.

Thx for help!

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

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

发布评论

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

评论(1

云巢 2025-01-03 09:16:40

您可能正在使用旧版本的 Outlook 项目。

如果您保留对邮件、rec-pattern、检查员和其他一些类型(我现在忘记了)的引用时间超过了您需要的时间,就会发生这种情况。

您的引用通常会指向该项目的旧版本,保留它还可以防止您获得对更新版本(带有附件的版本)的引用,即使触发事件 (Folder.BeforeItemMove) 也是如此。

另外,您是否尝试过 mail.Save() 是否能为您做任何事情?

这就是我在完成一个项目后立即使用的。

public static void NullAndRelease(object o)
{
    if (o == null) {
        return;
    }

    try {
        int releaseResult = 0;
        do {
            releaseResult = System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
        } while (releaseResult >= 0);
    } catch {
    } finally {
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }
}

该捕获没有任何消息,对我来说并不重要。如果有人传入一个指向 com 对象以外的对象的引用,它就会出现。您还可以尝试 FinalReleaseComObject(o)。

You could be working with an old version of the outlook item.

This can happen if you keep references to your mail items, rec-patterns, inspectors and some other types [that I now forgot] longer than you need them.

Your reference will often point to the old version of the item and keeping it can also prevent you from getting a reference to the updated one (the one with the attachment), even when events (Folder.BeforeItemMove) are triggered.

Also, have you tried if mail.Save() would do anything for you?

This is what I use as soon as I am done with an item.

public static void NullAndRelease(object o)
{
    if (o == null) {
        return;
    }

    try {
        int releaseResult = 0;
        do {
            releaseResult = System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
        } while (releaseResult >= 0);
    } catch {
    } finally {
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }
}

The catch has no message and is not important in my case. It is there if someone would pass in a reference that leads to something other than a com object. You can also try FinalReleaseComObject(o).

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