Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Python To Send Emails Via Outlook Express

Reply
Thread Tools

Python To Send Emails Via Outlook Express

 
 
ian@kirbyfooty.com
Guest
Posts: n/a
 
      12-20-2004
Hi,
I'm a newbie (oh no I can here you say.... another one...)

How can I get Python to send emails using the default windows email
client (eg outlook express)?

I thought I could just do the following

import win32com.client

s = win32com.client.Dispatch('CDO.Message')
s.From = "(E-Mail Removed)"
s.To = "(E-Mail Removed)"
s.Subject = "The subject"
s.Send

.... but nothing happens.

What am I doing wrong? Does anyone have some sample code to share with
me please?

Thank you!

Ian Cook

(freeware author of Kirby Alarm And Task Scheduler www.kirbyfooty.com)

 
Reply With Quote
 
 
 
 
Ganesan R
Guest
Posts: n/a
 
      12-20-2004
>>>>> "ian" == ian <(E-Mail Removed)> writes:

> import win32com.client


> s = win32com.client.Dispatch('CDO.Message')
> s.From = "(E-Mail Removed)"
> s.To = "(E-Mail Removed)"
> s.Subject = "The subject"
> s.Send


> ... but nothing happens.


> What am I doing wrong? Does anyone have some sample code to share with
> me please?


Try s.Send().

Ganesan
 
Reply With Quote
 
 
 
 
ian@kirbyfooty.com
Guest
Posts: n/a
 
      12-20-2004
Hi Ganesan
I tried changing s.Send to s.Send(). It now comes up with an exception
error..

The details are below.

Ian

Traceback (most recent call last):
File
"C:\Python23\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py" ,
line 307, in RunScript
debugger.run(codeObject, __main__.__dict__, start_stepping=0)
File
"C:\Python23\Lib\site-packages\pythonwin\pywin\debugger\__init__.py",
line 60, in run
_GetCurrentDebugger().run(cmd, globals,locals, start_stepping)
File
"C:\Python23\Lib\site-packages\pythonwin\pywin\debugger\debugger.py",
line 595, in run
exec cmd in globals, locals
File "D:\MyPython\emailtest.py", line 7, in ?
s.Send()
File
"C:\Python23\lib\site-packages\win32com\gen_py\CD000000-8B95-11D1-82DB-00C04FB1625Dx0x1x0.py",
line 686, in Send
return self._oleobj_.InvokeTypes(158, LCID, 1, (24, 0), (),)
com_error: (-2147352567, 'Exception occurred.', (0, None, 'The server
rejected one or more recipient addresses. The server response was: 554
<(E-Mail Removed)>: Relay access denied\r\n', None, 0, -2147220977),
None)

 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      12-20-2004
<(E-Mail Removed)> wrote:

> I tried changing s.Send to s.Send(). It now comes up with an exception
> error..


> com_error: (-2147352567, 'Exception occurred.', (0, None, 'The server
> rejected one or more recipient addresses. The server response was: 554
> <(E-Mail Removed)>: Relay access denied\r\n', None, 0, -2147220977),
> None)


sure looks like you managed to talk to the mail program, but your mail
server thinks you're trying to send faked mails. your local mail admins
can probably help you sort this one out.

(googling for "Relay access denied" could also help)

</F>



 
Reply With Quote
 
ian@kirbyfooty.com
Guest
Posts: n/a
 
      12-20-2004
Thanks Fredrik,
That was my first impression too.

But all I want to do is use Python to instruct Outlook Express to send
an email.
That way the user would not have to do any setting up etc of the mail
server properties etc and Outlook Express will magage all the
connection side of things.

I have seen other python scripts that will talk to Excel etc but so far
(despite a lot of searching) I cannot see how Python can talk to
Outlook Express.

I know I can do this in Clarion by accessing MapiSendEmail but because
I'm new to Python I don't know how to do it in Python. I'm really
impressed with the power of Python. It seems just about anything you
can think of is there already so I know th eanswer is out there
somewhere!

Can this be done using Python? Does anyone have a sample script please?
Pretty please?? <grin>

Ian

 
Reply With Quote
 
Keith Dart
Guest
Posts: n/a
 
      12-20-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi Ganesan
> I tried changing s.Send to s.Send(). It now comes up with an exception
> error..
>
> The details are below.


Looks like the COM part works, but sending mail has an error from the
SMTP host. But, slightly off topic, FYI, Python can send email directly
with the email and snmplib modules.


--
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~
Keith Dart <(E-Mail Removed)>
public key: ID: F3D288E4
================================================== ===================
 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      12-20-2004
<(E-Mail Removed)> wrote:

> But all I want to do is use Python to instruct Outlook Express to send
> an email.


and you succeeded -- the error message you saw came from the mail server, not
outlook itself. your problem is that the server didn't like the mail you sent; checking
the server configuration (or just the server logs) can help you figure out why.

have you tried different From/To settings, btw?

</F>



 
Reply With Quote
 
Max M
Guest
Posts: n/a
 
      12-20-2004
(E-Mail Removed) wrote:
> Thanks Fredrik,
> That was my first impression too.
>
> But all I want to do is use Python to instruct Outlook Express to send
> an email.



Which you did! From the look of the traceback.

But your mailserver is configured in such a way that you cannot send
mail from your machine using those email adresse, or you don't log on
with the correct credentials.


--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science
 
Reply With Quote
 
ian@kirbyfooty.com
Guest
Posts: n/a
 
      12-20-2004
Hi Keith
Thanks for your reply.

I am aware of the smtplib module and it works very well! (refer script
below)

The problem is that I have a developed a freeware application called
Kirby Alarm And Task Scheduler (see www.kirbyfooty.com).
The program can pop up a note, run a program, play a sound, or send an
email at whatever intervals the user wants..
When it comes to sending emails the user has the option of sending them
via smtp, or via there email client (eg outlook express). I prefer the
send method as this makes setting up the email parameters a lot easier
for the user.
As the program is used by over 16,000 people around the world I don't
want to complicate things by asking them to enter the mail server
properties.

Because Python is so powerful I want to develop a suite of applications
in Python that Kirby Alarm can run.
Things like FTP, Backup, Speech etc would be excellent


There has to be a way for Python to send emails via Outlook Express....

Kind regards
Ian

PS Here is the working script for sending emails via SMTP..

-----------------------------------------------------------------------------------------------------------------

# Import smtplib for the actual sending function
import os
import sys
import smtplib
import mimetypes
from email.Encoders import encode_base64
from email.MIMEAudio import MIMEAudio
from email.MIMEBase import MIMEBase
from email.MIMEImage import MIMEImage
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText


FROM = '(E-Mail Removed)'
TO = '(E-Mail Removed);(E-Mail Removed)'
SUBJECT = 'This is the subject'
MSGBODY = 'This the body of the message '
ATTACHSTR =
'c:/ian.txt;c:/c55/footytip/2003finalresults.txt;c:/snap.jpg'
MAILSERVER = 'insert mail server'
port = 25
username = 'insert username'
password = 'insert password'

# trim the strings of any leading or trailing spaces
FROM = FROM.strip()
TO = TO.strip()
SUBJECT = SUBJECT.strip()
MSGBODY = MSGBODY.strip()
ATTACHSTR = ATTACHSTR.strip()
MAILSERVER = MAILSERVER.strip()
username = username.strip()
password = password.strip()

# function to attach files
def getAttachment(path, filename):
ctype, encoding = mimetypes.guess_type(path)
if ctype is None or encoding is not None:
ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/', 1)
fp = open(path, 'rb')
if maintype == 'text':
attach = MIMEText(fp.read(),_subtype=subtype)
elif maintype == 'message':
attach = email.message_from_file(fp)
elif maintype == 'image':
attach = MIMEImage(fp.read(),_subtype=subtype)
elif maintype == 'audio':
attach = MIMEAudio(fp.read(),_subtype=subtype)
else:
print maintype, subtype
attach = MIMEBase(maintype, subtype)
attach.set_payload(fp.read())
encode_base64(attach)
fp.close
attach.add_header('Content-Disposition', 'attachment',
filename=filename)
return attach

#Connect to server
print 'Connecting to mail server ', MAILSERVER
try:
s = smtplib.SMTP(MAILSERVER,port)
#s.set_debuglevel(1)
except:
print 'ERROR: Unable to connect to mail server', MAILSERVER
sys.exit(1)

#login to server
if password <> '':
print 'Logging into mail erver'
try:
s.login(username,password)
except:
print 'ERROR: Unable to login to mail server', MAILSERVER
print 'Please recheck your password'
sys.exit(1)

# get list of email addresses to send to
ToList = TO.split(';')
print 'Sending email to ', ToList

# set up email parameters
msg = MIMEMultipart()
msg['From'] = FROM
msg['To'] = TO
msg['Subject'] = SUBJECT
msg.attach(MIMEText(MSGBODY))


# get list of file attachments
AttachList = ATTACHSTR.split(';')
for file in AttachList:
try:
attach = getAttachment(file,os.path.basename(file))
msg.attach(attach)
except:
print 'Error attaching ',file
pass


# send email
s.sendmail(FROM, ToList, msg.as_string())
s.quit()
s.close()

print 'done'
-----------------------------------------------------------------------------------------

 
Reply With Quote
 
ian@kirbyfooty.com
Guest
Posts: n/a
 
      12-20-2004
Hi Fredrik,
Thank you for the suggestion.

I tried different from/to settings and guess what? The mail came thru.

The script is now..
import win32com.client

s = win32com.client.Dispatch('CDO.Message')
s.From = "(E-Mail Removed)" (was
"(E-Mail Removed)")
s.To = "(E-Mail Removed)" (was
"(E-Mail Removed)")
s.Subject = "The subject"
s.Send()


My problem is thought, the message is still not being sent via Outlook
Express.
What am I missing?
Thanks again for your help so far!!

Kind regards
Ian

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Send To > Mail Recipient opens Outlook Express instead of Outlook 2003 rvp@ncf.ca Windows 64bit 6 09-06-2012 07:26 AM
Receiving emails in Outlook and outlook Express reboots computer. J-McC Computer Support 1 07-11-2007 07:46 AM
I can't get Outlook Express and XP Pro to play togehter - Outlook Express just doesn't start. Marc Computer Support 5 08-22-2006 10:46 PM
unable to send emails on outlook express tracey ellis Computer Support 4 02-24-2006 01:49 PM
ctypes NULL pointers; was: Python To Send Emails Via Outlook Express Lenard Lindstrom Python 3 01-04-2005 05:29 AM



Advertisments