473,503 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
13 6414
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
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
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
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
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
> 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
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

"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

"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
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
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
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
sKIPper29M
1 New Member
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
4282
by: Sven Templin | last post by:
Hello all, our configuration is as following described: - OS: Windows 2000 - Apache server 1.3 - Php 3.8 - MS outlook client 2000 _and_ no SMTP server available in the whole intranet.
9
7348
by: John | last post by:
Hi I am using the following code to search for an email message with id myID; Dim SentFld As Outlook.MAPIFolder Dim Email As Outlook.MailItem Dim I As Integer OutlookApp = New...
10
2816
by: John | last post by:
Hi When I open a new outlook email from vb.net, sometimes outlook is very slow to appear or occasionally outlook freezes completely. I am targeting mixed office2000/xp environments so I am...
2
2220
by: John | last post by:
Hi I am trying to find the way to create an email in outlook. I am using the code given at the end. It works fine the first time but calling the code second time gives the following error; ...
8
2673
by: Li Pang | last post by:
Hi, I used following codes to pass a message item from CDO to Outlook. They worked fine when I used outlook 2000, but get an error of "Specified cast is not valid." when I used Outlook 2003....
5
3906
by: Siv | last post by:
Hi, A little while ago I asked if anyone could help me with how to create an email using MS Outlook that contained an embedded picture file. Thanks to Jay Harlow I was able to get this working...
2
2438
by: Pieter | last post by:
Hi, I'm using a thight integration with Outlook 2003 (with an Exchange server) in my VB.NET (2005) application. Until now I'm using the Outlook Object Model, but it appears to be very slow, and...
7
6780
by: Dean Spencer | last post by:
Can anyone help? I am importing Emails from Outlook using the following code: Public Function ScanInbox(SubjectLine As String) Dim TempRst As Recordset Dim OlApp As Outlook.Application Dim...
1
4679
by: arnott | last post by:
hi all, I am writing a add-in for Outlook 2007 using C# and VSTO ( Visual Studio 2008). When the user clicks on each mailitem in the inbox (or any folder) in the active explorer window, the...
0
7201
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7083
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6988
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7456
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5011
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3166
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1510
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.