问题
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.
回答1:
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
来源:https://stackoverflow.com/questions/36988425/saving-an-item-in-the-drafts-folder-also-creates-a-blank-outbox-item