How can I create a new MailItem inside a user folder?

  • Thread starter cbukeo via OfficeKB.com
  • Start date
C

cbukeo via OfficeKB.com

Hello All,

I am developing an Outlook 2007 addin using VisualStudio 2008 and C#. My goal
is to create an email inside a user created folder.

I successfully create the folder and a message. However, I cannot get the
message placed in the user created folder. Instead it always ends up in the
default "Drafts" folder. Is there an outlook object model call that can solve
this problem?

My code looks like this (a bit simplified):

using Outlook = Microsoft.Office.Interop.Outlook;

Outlook.Folder olFolder = olParentFolder.Folders.Add("AUserFolder",
OlDefaultFolders.OlDefaultFolders.olFolderInbox);

Outlook.MailItem olMail = (Outlook.MailItem)olFolder.Items.Add(OlItemType.
olMailItem);

olMail.Subject = "Message Subject";
olMail.Body = "Message Unformatted Body";

foreach (string fromAddress in fromAddresses)
{
Outlook.Recipient olRecipient = olMail.Recipients.Add(fromAddress);
olRecipient.Type = OlMailRecipientType.olOriginator;
olRecipient.Resolve();
}

foreach (string toAddress in toAddresses)
{
Outlook.Recipient olRecipient = olMail.Recipients.Add(fromAddress);
olRecipient.Type = OlMailRecipientType.olTo;
olRecipient.Resolve();
}

olMail.Close(OlInspectorClose.olSave);
 
K

Ken Slovak - [MVP - Outlook]

New messages are always created in Drafts. If you want to move it to the
desired folder after the message is created in Drafts you can do so.
 
C

cbukeo via OfficeKB.com

Thanks! Very simple solution. Can't help but smile and wonder how come I
could not think about this?
 
C

cbukeo via OfficeKB.com

Ken,
Now I am able to move messages around within folders. However, the messages
stay as drafts. How can I set their sent status to true? This is also causing
problems if I try to set them as tasks. OOM throws an exception complaining
that a draft message cannot be set as a task.
Many thanks again
Buke
New messages are always created in Drafts. If you want to move it to the
desired folder after the message is created in Drafts you can do so.
Hello All,
[quoted text clipped - 37 lines]
olMail.Close(OlInspectorClose.olSave);
 
K

Ken Slovak - [MVP - Outlook]

What do you mean "set a message as a task"? They are 2 different objects and
MessageClass's. If you want to create a task item create one and copy any
property values from your email message item to the task that you want (such
as Subject or body text, etc.).

You cannot use the Outlook object model to create an item that looks sent.
You can use Extended MAPI or a 3rd party library, Redemption
(www.dimastr.com/redemption) to do that. You'd have to set various
properties such as sender addresses and so on to the values you want as well
as the Sent property before the new item you create is saved the first time.
There's a code sample showing that at the Redemption Web site.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top