Outlook 插件 VSTO:更新和发送电子邮件、标记为未读并将其放在收件箱顶部

发布于 2025-01-17 09:24:46 字数 1240 浏览 4 评论 0原文

我正在尝试更新我的收件箱中的Outlook.mailitem。

首先,我在收件箱中搜索它。找到它后,我将其复制到另一个Outlook.mailitem中,然后使用该副本:我将其更新并将其移至收件箱。

最后,如果成功的话,我将删除旧的Outlook.mailitem。

代码

try
{
    Outlook.MailItem omi = this.SearchForMailItem(id); // this searches for an Outlook.MailItem in the inbox based on and Id.
    
    Outlook.MailItem omiCopy = omi.Copy();
    
    // Here I update the copy with some user properties
    omiCopy.SetUserProperty(myProperty, true);
    
    // I set some flags
    omiCopy.SetMessageFlag(MapiPropertyValue.EnumPidTagMessageFlags.mfUnsent, false);
    
    // Move to the inbox and save the Outlook.MailItem updated
    Outlook.MailItem omiFinal = omiCopy.Move(GetFolder(Outlook.OlDefaultFolders.olFolderInbox));
    omiFinal.Save();
    
    omiFinal = null;
    
    // Delete the old one
    omi.Delete();
}
Catch (Exception ex)
{
    // Print error
}

它不起作用,旧的未删除。因此,我在收件箱中收到两封电子邮件。也下面抛出以下例外:

无法移动项目。

我在做什么错?

我想做的是

更新Outlook.mailitem(电子邮件),将其标记为未读取并将其移动在收件箱的顶部,就像您收到新电子邮件时一样。我不知道该怎么做,所以我做了我发布的内容,制作副本,更新然后删除旧的。

有什么想法吗?

更新: 在进行MOVE()之前,我已经意识到了Save(),并且得到了同样的例外。

I am trying to update an Outlook.MailItem that is in my inbox.

First I search for it in the inbox. Once I have found it I copy it into another Outlook.MailItem and then I work with the copy: I update it and move it to the inbox.

Finally if it is successfull I delete the old Outlook.MailItem.

Code:

try
{
    Outlook.MailItem omi = this.SearchForMailItem(id); // this searches for an Outlook.MailItem in the inbox based on and Id.
    
    Outlook.MailItem omiCopy = omi.Copy();
    
    // Here I update the copy with some user properties
    omiCopy.SetUserProperty(myProperty, true);
    
    // I set some flags
    omiCopy.SetMessageFlag(MapiPropertyValue.EnumPidTagMessageFlags.mfUnsent, false);
    
    // Move to the inbox and save the Outlook.MailItem updated
    Outlook.MailItem omiFinal = omiCopy.Move(GetFolder(Outlook.OlDefaultFolders.olFolderInbox));
    omiFinal.Save();
    
    omiFinal = null;
    
    // Delete the old one
    omi.Delete();
}
Catch (Exception ex)
{
    // Print error
}

It is not working, the old one is not deleted. So I get two emails in the inbox. Also below exception is thrown:

Cannot move the items.

What am I doing wrong?

What I am trying to do is:

Update an Outlook.MailItem (email), mark it as unread and move it at the top of the inbox, like when you receive a new email. I do not know how to do this, so I did what I have posted, make a copy, update and then delete the old one.

Any ideas?

UPDATE:
I have realized the Save() before doing the Move() and I get the same exception.

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

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

发布评论

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

评论(1

甜柠檬 2025-01-24 09:24:46

首先,尝试在调用 Move 方法之前调用 Save 方法。

Outlook.MailItem omi = this.SearchForMailItem(id); // this searches for an Outlook.MailItem in the inbox based on and Id.

Outlook.MailItem omiCopy = omi.Copy();

// Here I update the copy with some user properties
omiCopy.SetUserProperty(myProperty, true);

// I set some flags
omiCopy.SetMessageFlag(MapiPropertyValue.EnumPidTagMessageFlags.mfUnsent, false);

omiCopy.Save();

// Move to the inbox and save the Outlook.MailItem updated
Outlook.MailItem omiFinal = omiCopy.Move(GetFolder(Outlook.OlDefaultFolders.olFolderInbox));

omiFinal = null;

// Delete the old one
omi.Delete();

另外我建议使用 Marshal.ReleaseComObject 方法,该方法减少与指定 COM 对象关联的运行时可调用包装器 (RCW) 的引用计数。请参阅系统地释放对象了解更多信息。将对象设置为 null 不足以释放托管编程语言中的底层 COM 对象:

omiFinal = null;

最后,我建议使用 try/catch 来了解是否有任何错误扔在代码中。因此,请使用 try/catch 块包装您的代码。

First of all, try to call the Save method before calling Move one.

Outlook.MailItem omi = this.SearchForMailItem(id); // this searches for an Outlook.MailItem in the inbox based on and Id.

Outlook.MailItem omiCopy = omi.Copy();

// Here I update the copy with some user properties
omiCopy.SetUserProperty(myProperty, true);

// I set some flags
omiCopy.SetMessageFlag(MapiPropertyValue.EnumPidTagMessageFlags.mfUnsent, false);

omiCopy.Save();

// Move to the inbox and save the Outlook.MailItem updated
Outlook.MailItem omiFinal = omiCopy.Move(GetFolder(Outlook.OlDefaultFolders.olFolderInbox));

omiFinal = null;

// Delete the old one
omi.Delete();

Also I'd recommend using the Marshal.ReleaseComObject method which decrements the reference count of the Runtime Callable Wrapper (RCW) associated with the specified COM object. See Systematically releasing objects for more information. Setting the object to null is not enough for releasing th eunderlying COM objects in managed programming languages:

omiFinal = null;

Finally, I'd recommend using the try/catch to know if any errors are thrown in the code. So, wrap your code with a try/catch block.

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