By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,400 Members | 903 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,400 IT Pros & Developers. It's quick & easy.

Outlook COM: how to create a MailItem from a .msg file

P: n/a
Hi all,

I've been writing some code to move some data into and out of Outlook (2003
+ Exchange 2003). I have some email .msg files on our file server, and I
can't seem to get them back into the Outlook object I need, ie a MailItem.
I've tried to use App.CopyFile() to (temporarily) put the file in an OL
folder. Problem is, however, this returns a DocumentItem and not a MailItem.

Is there any way I could 'cast' this DItem into a MItem? Apparently, OL
treats it as any general document - which, btw, shows in the view, too; it
has another icon and you have to open it to view it). Or maybe there's
another way to open it; I really only need the object in memory. Any ideas?

TIA,
g
Jul 21 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
On 7/4/05, Guy Lateur <gu********@b-b.be> wrote:
I've been writing some code to move some data into and out of Outlook (2003
+ Exchange 2003). I have some email .msg files on our file server, and I
can't seem to get them back into the Outlook object I need, ie a MailItem..
I've tried to use App.CopyFile() to (temporarily) put the file in an OL
folder. Problem is, however, this returns a DocumentItem and not a MailItem.

Is there any way I could 'cast' this DItem into a MItem? Apparently, OL
treats it as any general document - which, btw, shows in the view, too; it
has another icon and you have to open it to view it). Or maybe there's
another way to open it; I really only need the object in memory. Any ideas?


Well, I don't know anything about Outlook's COM interface, so I don't
know if this will work, but you might try win32com.client.CastTo().
Something like:

my_MItem = win32com.client.CastTo(my_DItem, 'MItem')

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
Jul 21 '05 #2

P: n/a
Thanks for the tip, Simon, but unfortunately it doesn't work; it says "The
interface name 'MailItem' does not appear in the same library as object
'<win32com.gen_py.Microsoft Outlook 11.0 Object Library._DocumentItem
instance at 0x29912600>"

Anything else I could try?

Cheers,
g


"Simon Brunning" <si************@gmail.com> schreef in bericht
news:ma***************************************@pyt hon.org...

Well, I don't know anything about Outlook's COM interface, so I don't
know if this will work, but you might try win32com.client.CastTo().
Something like:

my_MItem = win32com.client.CastTo(my_DItem, 'MItem')

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
Jul 21 '05 #3

P: n/a
On 7/4/05, Guy Lateur <gu********@b-b.be> wrote:

Anything else I could try?


Lateral thinking ?

=== untested ===

import imaplib, time, sys

f = open(msg_file)
r = f.readlines()
f.close()
msg1 = ''.join(r)
server = 'my.exchangeserver.com' # or IP address

user = 'user'
pw = 'pw'

M = imaplib.IMAP4(server)
M.sock.settimeout(120)
M.login(user,pw)[1][0]
M.select()
M.append('INBOX',None ,time.time() , msg1) # Inbox or other folder name
print "MESSAGE successfully saved"
#M.close() Don't use close, deletes/purges items
M.logout()[1][0]
Jul 21 '05 #4

P: n/a
Thanks for the suggestion, Tim. Unfortunately, I get a 'connection refused'
error on the line 'M = imaplib.IMAP4(server)'. It says "socket.error:
(10061, 'Connection refused')". I've tried both the external IP adress and
the internal one (10.0.0.2). I'm sure there's a way to get over this, isn't
there?

One more question: can I avoid having (plain text) passwords in my code? I
guess I could make a special account for this with a password known by
everybody.

Cheers,
g


"Tim Williams (gmail)" <td*******@gmail.com> schreef in bericht
news:ma***************************************@pyt hon.org...

Lateral thinking ?

=== untested ===

import imaplib, time, sys

f = open(msg_file)
r = f.readlines()
f.close()
msg1 = ''.join(r)
server = 'my.exchangeserver.com' # or IP address

user = 'user'
pw = 'pw'

M = imaplib.IMAP4(server)
M.sock.settimeout(120)
M.login(user,pw)[1][0]
M.select()
M.append('INBOX',None ,time.time() , msg1) # Inbox or other folder name
print "MESSAGE successfully saved"
#M.close() Don't use close, deletes/purges items
M.logout()[1][0]
Jul 21 '05 #5

P: n/a
On 7/5/05, Guy Lateur <gu********@b-b.be> wrote:
Thanks for the suggestion, Tim. Unfortunately, I get a 'connection refused'
error on the line 'M = imaplib.IMAP4(server)'. It says "socket.error:
(10061, 'Connection refused')". I've tried both the external IP adress and
the internal one (10.0.0.2). I'm sure there's a way to get over this, isn't
there?
I just tried this and it failed with IP addresses but not
hostnames/machine names, try it again with the server name. :)
One more question: can I avoid having (plain text) passwords in my code? I
guess I could make a special account for this with a password known by
everybody.


Depends how secure you need it to be. For my simple stuff I just do
something like this

In idle/pythonwin/interactive
p = 'password_string'
p = p.encode("utf16")
p

'\xff\xfep\x00a\x00s\x00s\x00w\x00o\x00r\x00d\x00_ \x00s\x00t\x00r\x00i\x00n\x00g\x00'

Then in the script
user = mylogin
pw = '\xff\xfep\x00a\x00s\x00s\x00w\x00o\x00r\x00d\x00_ \x00s\x00t\x00r\x00i\x00n\x00g\x00'
M = imaplib.IMAP4(server)
M.login(mylogin,pw.decode("utf16")
('OK', ['LOGIN completed.'])

HTH :)
Jul 21 '05 #6

P: n/a
> I just tried this and it failed with IP addresses but not
hostnames/machine names, try it again with the server name. :)
Nope, same problem. I think TJG might be right, and our server probably
doesn't have IMAP running (yet).
Depends how secure you need it to be. For my simple stuff I just do
something like this
[snip]


That's a nice thought, although the pw is still pretty visible. Maybe
there's a way to encode/decode it using some sort of key (that only I know),
that produces a truly unrecognisable pw?

Thanks for the input,
g
Jul 21 '05 #7

P: n/a
On 7/5/05, Guy Lateur <gu********@b-b.be> wrote:
I just tried this and it failed with IP addresses but not
hostnames/machine names, try it again with the server name. :)


Nope, same problem. I think TJG might be right, and our server probably
doesn't have IMAP running (yet).


Could you SMTP it back in ? It would gain an extra Received: header
but the rest of the email would most likely be unaltered.
Depends how secure you need it to be. For my simple stuff I just do
something like this
[snip]


That's a nice thought, although the pw is still pretty visible. Maybe
there's a way to encode/decode it using some sort of key (that only I know),
that produces a truly unrecognisable pw?


Not my area of expertise I'm afraid. If you manually run the script
then you could use getpass() to prompt you for the password at run
time.

:)
Jul 21 '05 #8

P: n/a

"Tim Williams (gmail)" <td*******@gmail.com> schreef in bericht
news:ma***************************************@pyt hon.org...
Could you SMTP it back in ? It would gain an extra Received: header
but the rest of the email would most likely be unaltered.
I don't understand what you mean. How does this have to do with connecting
to the (probably-not-running) IMAP service?
Not my area of expertise I'm afraid. If you manually run the script
then you could use getpass() to prompt you for the password at run
time.


Not mine either.. ;)
I'd like to avoid having the user type in a pw every time. Outlook doesn't
seem to need that, so, unless that's unsafe, why should I? Or could I get xp
to 'remember' it? Btw, Outlook does ask permission if you try to, say, read
the body of message.

Anyway, the approach I suggested earlier (pw encrypted using key) is
probably unlikely to solve the security issue, either. I mean, if the key
itself (or a reference to it) is in my code, then anyone reading that code
can decrypt it, right?
g

Jul 21 '05 #9

P: n/a

"guy lateur" <gu********************@pandora.be> schreef in bericht
news:KU**********************@phobos.telenet-ops.be...
|
| "Tim Williams (gmail)" <td*******@gmail.com> schreef in bericht
| news:ma***************************************@pyt hon.org...
| > Could you SMTP it back in ? It would gain an extra Received: header
| > but the rest of the email would most likely be unaltered.
|
| I don't understand what you mean. How does this have to do with connecting
| to the (probably-not-running) IMAP service?
|

Hold on, I think I do know what you mean: using SMPT (running) instead of
connecting to the IMAP service (probably-not-running). I'll let you know how
I get along with that. I may still need a way to convert a DocumentItem to a
MailItem, though.

Cheers,
g
Jul 21 '05 #10

P: n/a
Ok, we didn't have the IMAP service running; we do now (no SSL).

Connecting to the server is not a problem anymore, but logging in is. It
works with the administrator account, but not with my personal account. We
have restricted access to all machines in 10.0.0.0/255.255.255.0, which
includes my machine.

My password is empty (yeah, I know..). Could that be the problem? I'm using
this: pw = ''

Thanks,
g
Jul 21 '05 #11

P: n/a
Yes! I finally got it to work. I've written a VBscript which I'll call from
python. It uses Outlook.Redemption's SafeMailItem. No need to use IMAP or
whatever services.

Only weird thing is it doesn't put the msg in the Inbox, as I intended, but
in the Drafts folder. Well, never mind that, it's a temp object anyway.

Here's the code:

Dim sItem, oItem
Dim myDestBox, myNS, myOL

Set myOL = CreateObject("Outlook.Application")
Set myNS = myOL.GetNamespace("MAPI")
Set sItem = CreateObject("Redemption.SafeMailItem")

Set myDestBox = myNS.GetDefaultFolder(6)
Set oItem = myDestBox.Items.Add(0)

sItem.Item = oItem
sItem.Import
"H:\Inhoud-iedereen\Inhoud-Guy\app\BBProject\data\test\Leemarchitect.msg", 3
sItem.Save

Cheers,
g


Jul 21 '05 #12

P: n/a
python version:

import win32com.client

myOL = win32com.client.Dispatch("Outlook.Application")
myNS = myOL.GetNamespace("MAPI")
sItem = win32com.client.Dispatch("Redemption.SafeMailItem" )

myDestBox = myNS.GetDefaultFolder(6)
oItem = myDestBox.Items.Add(0)

sItem.Item = oItem
sItem.Import("H:\\Inhoud-iedereen\\Inhoud-Guy\\app\\BBProject\\data\\test\\Leemarchitect.msg ",
3)
sItem.Save()

Jul 21 '05 #13

P: 1
Here is the VB.NET version of the code:

Dim sItem As New Redemption.SafeMailItem
Dim oItem As New Object
Dim myOL As New Outlook.Application
Dim myNS As Outlook.NameSpace
Dim myDestBox As Outlook.MAPIFolder

myNS = myOL.GetNamespace("MAPI")
myDestBox = myNS.GetDefaultFolder(OlDefaultFolders.olFolderInb ox)
oItem = myDestBox.Items.Add(0)

sItem.Item = oItem
sItem.Import("H:\\Inhoud-iedereen\\Inhoud-Guy\\app\\BBProject\\data\\test\\Leemarchitect.msg ", 3)
sItem.Save()

I'm getting the same problem: the email is appearing in the Drafts folder. Guy, have you figured out a solution to this?
Apr 24 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.