我有一个C#控制台应用程序,可以动态创建一堆电子邮件并将它们保存到Outlook 2013中的“草稿”文件夹中。但是,我遇到了一个问题,我创建的项目不仅在草稿中创建文件夹,但也会在“发件箱”文件夹中创建一个空白项目(为每个已填写的“草稿”项目创建一个空白的“发件箱”项目。)
为了进行比较,当我尝试使用UI创建草稿电子邮件时,它按预期工作。我创建了一封新电子邮件,单击“保存”,该项目仅显示在“草稿”文件夹中。当我尝试以编程方式执行此操作时,将创建发件箱项目。
这是我用来创建电子邮件的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 = "Test";
email.HTMLBody = "Test draft email";
email.Recipients.Add(smtpAddress.SmtpAddress);
email.Recipients.ResolveAll();
email.UnRead = True;
email.Save();
我也尝试过这种变化:
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();
要进行故障排除,我在Outlook模块中创建了以下VBA代码,以测试与上述类似的代码:
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
VBA代码在运行时与C#代码完全相同。它会在“草稿”文件夹中创建一个已完成的电子邮件,但也会在“发件箱”中创建一个空白项目。
作为参考,我有两个Outlook配置文件,并让应用程序提示我打开Outlook时使用的那个。但是,如果我将设置更改为始终使用我的默认配置文件(" Outlook"),我已经测试了运行控制台应用程序和VBA代码,并且每次它仍然会创建一个空白的发件箱项目创建一个完整的草稿项目。
有没有人遇到过这个问题,如果有的话,你是怎么解决的?
感谢。
答案 0 :(得分:1)
在标记save
.UnRead = True
VBA代码
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