Create new .msg

  • Thread starter Thread starter Greg Ewing [MVP]
  • Start date Start date
G

Greg Ewing [MVP]

Hi all, I'm building an Outlook application to read blogs (similar to
Newsgator) and am having some trouble getting the new items in to my folders
correctly. With the code below I get what is basically a draft message in
the folder which doesn't have the To: or ReceivedTime set correctly. One
idea I have is to write out .msg files with all of the correct information
set and then import those back in to the folder. Would that work? If it
would, does anyone have any code to write out new .msg files? I can export
an existing MailItem with SaveAs() which doesn't help since I can't set all
of the necessary properties on the MailItem before writing.

I also looked in to ExMAPI with C++ but haven't found a sample yet which
writes new messages to a folder and sets the ReceivedTime and SentTo
properties. Anyone have anything like that?

I'm using C# right now but have no problem writing parts or all in C++ if
that's necessary.

Here's the code I'm using which doesn't work. I've also tried the
Redemption objects but can't set the sender address or ReceivedTime with
those either. (unless I'm missing something)

//blog is the destination folder in Outlook
//si is an RSS item from the RSS feed
private void AddItemToFolder(Outlook.MAPIFolder blog, SyndicationItem si,
string author)
{
MailItem mi = (MailItem)blog.Items.Add(OlItemType.olMailItem);
mi.Subject = si.Title;
mi.Body = si.Description + Environment.NewLine + Environment.NewLine +
si.Comments;
mi.Body = mi.Body + Environment.NewLine + Environment.NewLine + si.Link;
//mi.ReceivedTime = si.PubDate; //read-only property
mi.SentOnBehalfOfName = author; //hack to make the sender's name show up
in the folder list view
mi.DeferredDeliveryTime = DateTime.Now; //si.PubDate; //doesn't set the
received time
mi.Save();
mi.Move(blog);
}

Another less important question at this point is how do I get the body to
display HTML? I tried setting the BodyFormat of the MailItem but the HTML
still just shows up in the message body as HTML, not interpreted.

Thanks a lot for any and all help!

x-posted to microsoft.public.platformsdk.mapi and
microsoft.public.outlook.program_addins
 
Try Application.CreateItemFromTemplate instead. To set HTML, use
MailItem.HTMLBody property.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
Dmitry, those fields won't be read only if I create from a template? So I
can do something like this:

MailItem mi =
(MailItem)CreateItemFromTemplate(@"c:\pathtotemplate\mytemplate.oft");
mi.ReceivedTime = "11/2/2002"; //or some other valid time?
mi.SenderAddress = "WhateverIWant";

Thanks a lot

--
Greg Ewing [MVP]
http://www.citidc.com/
 
I tried the following two sets of code but I still get an error telling me
that SenderName is readonly. Am I using the wrong Redemption object?

Redemption.MessageItem mi =
(MessageItem)app.CreateItemFromTemplate("blogPost.oft", null);
mi.SenderName = author;

and

Redemption.SafeMailItem mi = new SafeMailItemClass();
mi.Item = (MailItem)app.CreateItemFromTemplate("blogPost.oft", null);
mi.SenderName = author;

I get the same error for the Sender, ReceivedTime, and SentOn properties.

--
Greg Ewing [MVP]
http://www.citidc.com
 
1. SenderName is more thanjust one string property, it is half adozen of
PR_SENDER_PROPERTIES
2. You only dim and cast the variable as Redemption.MessageItem, but you
still get the same old Outlook.MailItem from CreateItemFromTemplate
3. Try the following code instead:

MailItem mi = (MailItem)blog.Items.Add(OlItemType.olMailItem);
set SafeItem = CreateObject("Redemption.SafeMailItem")
SafeItem.Item = mi
SafeItem.Import("blogPost.oft", 3)
mi.Save

mi.Save however reset SentOn and Received properties, so an even better
solution would be

MailItem mi = (MailItem)blog.Items.Add(OlItemType.olPostItem); //this way
the message is create in the "sent" state
mi.Save;
strEntryID = mi.EntryID;
mi = NULL;
Utils = CreateObject("Redemption.MAPIUtils");
sItem = Utils.GetItemFromID(strEntryID);
sItem.Import("blogPost.oft", 3);
sItem.Save;

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
OK, how would I use either of those to set the ReceivedTime?
I changed this : MailItem mi =
(MailItem)blog.Items.Add(OlItemType.olPostItem);
to : PostItem pi = (PostItem)blog.Items.Add(OlItemType.olPostItem);
because the former threw an invalid cast exception. Now, PostItem doesn't
include ReceivedTime so I can't set it. If I change the above to reference
to MailItem all the properties are there but ReadOnly. I must be missing
something, think you could give me some sample code that will do this:
MailItem.ReceivedTime = DateTime.Now? I'm at a loss.

Thanks again for all of your help so far.
 
Just to make sure you know what I mean, what if I wanted to do this:

MailItem.ReceivedTime = DateTime.MinValue

How would I do that?
 
Well, I got past this with the MAPIUtils class. Now I just have to figure
out how to make the From address show up correctly. Right now it shows up
as the currently logged in user (me) in the message list pane and as the
actual author when viewing the message.

Here's the code. pi is a PostItem.

Redemption.MAPIUtils utils = new Redemption.MAPIUtils();
int PrReceivedTime = 235274304; //&0E060040;
utils.HrSetOneProp(pi, PrReceivedTime, dt, true);
 
OK, last post for this thread for now. I fixed my last problem with

int PrSentRepresentingName = 4325406;

Thanks for the great objects Dmitry.
 
Back
Top