I have tried scripting this with rules and VBA, now trying with C# but still failing. (code is exploratory, so please excuse the terribleness)
maybe someone here will show me the error of my code...
output (slightly clipped)
Sent Mail - Local
step1
step2
step2: checking Deleted Items
step2: checking Inbox
...
step2: checking Sent Mail - Local
step3:Sent Mail - Local
step4 setting outbox
step4 releasing com
step4 set to \\Personal Folders\Sent Mail - Local
step5
step6
maybe someone here will show me the error of my code...
- sending an email by hand - everything works as expected (rules, VBA, etc are all applied).
- sending an email from code (C#, external VBA, etc) has the messages saving in Sent Items regardless of any other settings or configurations (up to and including the scenario where the "save sent items" option is disabled in outlook preferences)
- releasing the COM object before setting a new value doesn't help.
- as you can see from the printouts below (last step4), the property is set correctly, but all emails sent by this code are actually saved in "\\Mailbox - Username\Sent Items" (default), not "\\Personal Folders\Sent Mail - Local" (where they SHOULD be getting saved)
- one caveat - the dev machine only has Outlook 2003 (office 11) interop files, and the machine with outlook has Outlook 2007 (office 12) - this setup unfortunately cannot be easily modified, and alternatives to develop/test on are unavailable, but I don't *think* the mismatch should be causing this particular problem...
Code:
using System;
using System.Collections.Generic;
using System.Text;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace Emailer
{
class Emailer
{
static void Main(string[] args)
{
// folderName is stored in the app.config, value is set to "Sent Mail - Local"
string folderName = System.Configuration.ConfigurationSettings.AppSettings["folderName"].ToString();
Console.WriteLine(folderName);
Outlook.Application oApp = null;
Outlook.NameSpace oNameSpace = null;
Outlook.MAPIFolder oOutboxFolder = null;
Console.WriteLine("step1");
oApp = new Outlook.Application();
oNameSpace= oApp.GetNamespace("MAPI");
oNameSpace.Logon(null,null,true,true);
Console.WriteLine("step2");
foreach (Outlook.MAPIFolder parent in oNameSpace.Folders)
{ // go through each of the parent folders - Mailbox, Personal Folders, etc
foreach (Outlook.MAPIFolder f in parent.Folders)
{ // go through the folders which contain
Console.WriteLine("step2: checking " + f.Name);
if (f.Name.Contains(folderName))
oOutboxFolder = f;
}
}
if (oOutboxFolder == null)
{
Console.WriteLine("error: no folder found");
return;
}
Console.WriteLine("step3:" + oOutboxFolder.Name);
//oOutboxFolder = oNameSpace.Folders[folderName]; //oNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox);
Object o = oApp.CreateItem(Outlook.OlItemType.olMailItem);
Outlook.MailItem mi = (Outlook.MailItem)o;
Console.WriteLine("step4 setting outbox");
Outlook.MAPIFolder tempFolder = mi.SaveSentMessageFolder;
System.Runtime.InteropServices.Marshal.ReleaseComObject(tempFolder);
Console.WriteLine("step4 releasing com");
mi.SaveSentMessageFolder = oOutboxFolder;
Console.WriteLine("step4 set to " + mi.SaveSentMessageFolder.FullFolderPath); // prints correct values!
mi.To = "validemail@validdomain.com";
mi.Body = "Some Message";
mi.Subject = "Some Title";
Console.WriteLine("step5");
mi.Save();
Console.WriteLine("step6");
mi.Send();
}
}
}
output (slightly clipped)
Sent Mail - Local
step1
step2
step2: checking Deleted Items
step2: checking Inbox
...
step2: checking Sent Mail - Local
step3:Sent Mail - Local
step4 setting outbox
step4 releasing com
step4 set to \\Personal Folders\Sent Mail - Local
step5
step6
Comment