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

G

Guy Lateur

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
 
S

Simon Brunning

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')
 
G

Guy Lateur

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" <[email protected]> schreef in bericht

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')
 
T

Tim Williams (gmail)

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]
 
G

Guy Lateur

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)" <[email protected]> schreef in bericht

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]
 
T

Tim Williams (gmail)

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
'\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 :)
 
G

Guy Lateur

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
 
T

Tim Williams (gmail)

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.

:)
 
G

guy lateur

Tim Williams (gmail) said:
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
 
G

guy lateur

"guy lateur" <[email protected]> schreef in bericht
|
| "Tim Williams (gmail)" <[email protected]> schreef in bericht
| | > 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
 
G

Guy Lateur

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
 
G

Guy Lateur

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
 
G

Guy Lateur

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()
 

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

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top