将项目保存在草稿文件夹中还会创建一个空白的发件箱项目 [英] Saving an item in the drafts folder also creates a blank outbox item

查看:218
本文介绍了将项目保存在草稿文件夹中还会创建一个空白的发件箱项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#控制台应用程序,可动态创建一堆电子邮件并将其保存到Outlook 2013中的草稿文件夹中。但是,我遇到了一个问题,即我创建的项目不仅在草稿文件夹中创建,但是在发件箱文件夹中也会创建一个空白项目(为每个草稿项目填充一个空白的发件箱项目)。



为了比较,当我尝试创建一个使用用户界面起草电子邮件,它可以按预期工作。我创建了一封新电子邮件,单击保存,该项目仅出现在草稿文件夹中。当我尝试以编程方式进行操作时,将创建发件箱项目。



这是我用来创建电子邮件的C#代码:

  Outlook.Application app = new Outlook.Application(); 
Outlook._NameSpace ns = null;
Outlook.Account smtpAddress = null;
Outlook.MAPIFolder draftsFolder = null;

ns = app.GetNamespace( MAPI);
draftsFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderDrafts);

Outlook.MailItem email = draftsFolder.Items.Add( IPM.NOTE);
email.Subject =测试;
email.HTMLBody =测试草稿电子邮件;
email.Recipients.Add(smtpAddress.SmtpAddress);
email.Recipients.ResolveAll();
email.UnRead = True;
email.Save();

我也尝试了这种变化:

  Outlook.Application应用=新的Outlook.Application(); 
Outlook._NameSpace ns = null;
Outlook.Account smtpAddress = null;
Outlook.MAPIFolder draftsFolder = null;

ns = app.Session;
ns.Logon(Profile:,密码:Type.Missing,ShowDialog:Type.Missing,NewSession:true);
draftsFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderDrafts)作为Outlook.Folder;

Outlook.MailItem email =(Outlook.MailItem)draftsFolder.Items.Add( IPM.NOTE);
email.Subject =测试;
email.HTMLBody =测试草稿电子邮件;
email.Recipients.Add(smtpAddress.SmtpAddress);
email.Recipients.ResolveAll();
email.UnRead = True;
email.Save();

要解决此问题,我在Outlook模块中创建了以下VBA代码,以测试与上述代码相似的代码:

  Sub SaveEmailToDraftFolder()
Dim应用程序作为Outlook.Application
Dim ns作为Outlook.nameSpace
Dim fd作为Outlook.folder
Dim em作为Outlook.MailItem

设置应用程序=新Outlook.Application
设置ns = app.GetNamespace( MAPI)
设置fd = ns.GetDefaultFolder(olFolderDrafts)

设置em = fd.Items.Add( IPM.NOTE)

和em
。 Subject =测试VBA
.HTMLBody =这是一个测试。
.Recipients.Add(smtpAddress)的smtpAddress只是真实电子邮件地址
的占位符.Recipients.ResolveAll
.UnRead = True
。保存
结尾为
End Sub

VBA代码在运行时的作用与C#代码相同。它将在草稿文件夹中创建完成的电子邮件,但还会在发件箱中创建一个空白项目。



作为参考,我有两个Outlook配置文件,并且应用程序提示我输入打开Outlook时要使用的一种。但是,如果将设置更改为始终使用默认配置文件( Outlook),则我已经测试过同时运行控制台应用程序和VBA代码,并且每次创建完整的草稿项目时,它仍会创建一个空白的发件箱项目。 / p>

有人遇到过这个问题吗?如果是这样,您是如何解决的?



谢谢。

解决方案

在标记它之前,您需要保存 .UnRead = True



VBA代码

 使用em 
.Subject =测试VBA
.HTMLBody =这是一个测试。
.Recipients.Add( smtpAddress)的smtpAddress是真实电子邮件地址
的占位符。Recipients.ResolveAll
。保存
。UnRead= True
结尾为


I have a C# console app that dynamically creates a bunch of emails and saves them to the Drafts folder in Outlook 2013. However, I've run into an issue where the items I create are not only created in the Drafts folder, but a blank item is also created in the Outbox folder (one blank Outbox item is created for every filled in Drafts item).

For comparison, when I try to create a draft email using the UI, it works as intended. I create a new email, click Save, and the item only appears in the Drafts folder. When I try to do it programmatically, the Outbox items are created.

Here's the C# code I'm using to create the emails:

Outlook.Application app = new Outlook.Application();
Outlook._NameSpace ns = null;
Outlook.Account smtpAddress = null;
Outlook.MAPIFolder draftsFolder = null;

ns = app.GetNamespace("MAPI");
draftsFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderDrafts);

Outlook.MailItem email = draftsFolder.Items.Add("IPM.NOTE");
email.Subject = "Test";
email.HTMLBody = "Test draft email";
email.Recipients.Add(smtpAddress.SmtpAddress);
email.Recipients.ResolveAll();
email.UnRead = True;
email.Save();

I also tried this variation:

Outlook.Application app = new Outlook.Application();
Outlook._NameSpace ns = null;
Outlook.Account smtpAddress = null;
Outlook.MAPIFolder draftsFolder = null;

ns = app.Session;
ns.Logon(Profile:"", Password:Type.Missing, ShowDialog:Type.Missing, NewSession:true);
draftsFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderDrafts) As Outlook.Folder;

Outlook.MailItem email = (Outlook.MailItem)draftsFolder.Items.Add("IPM.NOTE");
email.Subject = "Test";
email.HTMLBody = "Test draft email";
email.Recipients.Add(smtpAddress.SmtpAddress);
email.Recipients.ResolveAll();
email.UnRead = True;
email.Save();

To troubleshoot, I created the following VBA code in an Outlook module to test out similar code to the above:

Sub SaveEmailToDraftFolder()
    Dim app As Outlook.Application
    Dim ns As Outlook.nameSpace
    Dim fd As Outlook.folder
    Dim em As Outlook.MailItem

    Set app = New Outlook.Application
    Set ns = app.GetNamespace("MAPI")
    Set fd = ns.GetDefaultFolder(olFolderDrafts)

    Set em = fd.Items.Add("IPM.NOTE")

    With em
        .Subject = "Test VBA"
        .HTMLBody = "This is a test."
        .Recipients.Add (smtpAddress)  'smtpAddress is just a placeholder for a real email address
        .Recipients.ResolveAll
        .UnRead = True
        .Save
    End With
End Sub

The VBA code, when run, does the same thing as the C# code. It creates a completed email in the Drafts folder, but also creates a blank item in the Outbox.

For reference, I have two Outlook profiles and have the application prompt me for the one to use when I open Outlook. However, I've tested running both the console app and the VBA code if I change the setting to always use my default profile ("Outlook"), and it still creates a blank Outbox item every time it creates a completed Drafts item.

Has anyone experienced this issue and, if so, how did you resolve it?

Thanks.

解决方案

You need to save it before you mark it .UnRead = True

VBA Code

With em
    .Subject = "Test VBA"
    .HTMLBody = "This is a test."
    .Recipients.Add ("smtpAddress")  'smtpAddress is placeholder for a real email address
    .Recipients.ResolveAll
    .Save
    .UnRead = True
End With

这篇关于将项目保存在草稿文件夹中还会创建一个空白的发件箱项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆