473,698 Members | 2,751 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python To Send Emails Via Outlook Express

ian
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 = "ia*@kirbyfooty .com"
s.To = "so*****@yahoo. com"
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)

Jul 18 '05 #1
40 11861
>>>>> "ian" == ian <ia*@kirbyfooty .com> writes:
import win32com.client s = win32com.client .Dispatch('CDO. Message')
s.From = "ia*@kirbyfooty .com"
s.To = "so*****@yahoo. com"
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
Jul 18 '05 #2
ian
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\Li b\site-packages\python win\pywin\frame work\scriptutil s.py",
line 307, in RunScript
debugger.run(co deObject, __main__.__dict __, start_stepping= 0)
File
"C:\Python23\Li b\site-packages\python win\pywin\debug ger\__init__.py ",
line 60, in run
_GetCurrentDebu gger().run(cmd, globals,locals, start_stepping)
File
"C:\Python23\Li b\site-packages\python win\pywin\debug ger\debugger.py ",
line 595, in run
exec cmd in globals, locals
File "D:\MyPython\em ailtest.py", line 7, in ?
s.Send()
File
"C:\Python23\li b\site-packages\win32c om\gen_py\CD000 000-8B95-11D1-82DB-00C04FB1625Dx0x 1x0.py",
line 686, in Send
return self._oleobj_.I nvokeTypes(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
<iw****@yahoo.c om>: Relay access denied\r\n', None, 0, -2147220977),
None)

Jul 18 '05 #3
<ia*@kirbyfooty .com> 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
<iw****@yahoo.c om>: 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>

Jul 18 '05 #4
ian
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

Jul 18 '05 #5
ia*@kirbyfooty. com 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 <kd***@kdart.co m>
public key: ID: F3D288E4
=============== =============== =============== =============== =========
Jul 18 '05 #6
ia*@kirbyfooty. com 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
Jul 18 '05 #7
<ia*@kirbyfooty .com> 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>

Jul 18 '05 #8
ian
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.MIMEMulti part import MIMEMultipart
from email.MIMEText import MIMEText
FROM = 'i...@kirbyfoot y.com'
TO = 'i...@cgbs.com. au;i...@yahoo.c om'
SUBJECT = 'This is the subject'
MSGBODY = 'This the body of the message '
ATTACHSTR =
'c:/ian.txt;c:/c55/footytip/2003finalresult s.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.stri p()
username = username.strip( )
password = password.strip( )

# function to attach files
def getAttachment(p ath, 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.rea d(),_subtype=su btype)
elif maintype == 'message':
attach = email.message_f rom_file(fp)
elif maintype == 'image':
attach = MIMEImage(fp.re ad(),_subtype=s ubtype)
elif maintype == 'audio':
attach = MIMEAudio(fp.re ad(),_subtype=s ubtype)
else:
print maintype, subtype
attach = MIMEBase(mainty pe, subtype)
attach.set_payl oad(fp.read())
encode_base64(a ttach)
fp.close
attach.add_head er('Content-Disposition', 'attachment',
filename=filena me)
return attach

#Connect to server
print 'Connecting to mail server ', MAILSERVER
try:
s = smtplib.SMTP(MA ILSERVER,port)
#s.set_debuglev el(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(usernam e,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(MIME Text(MSGBODY))
# get list of file attachments
AttachList = ATTACHSTR.split (';')
for file in AttachList:
try:
attach = getAttachment(f ile,os.path.bas ename(file))
msg.attach(atta ch)
except:
print 'Error attaching ',file
pass
# send email
s.sendmail(FROM , ToList, msg.as_string() )
s.quit()
s.close()

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

Jul 18 '05 #9
ian
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 = "ia*@cgbs.com.a u" (was
"ia*@kirbyfooty .com")
s.To = "ia*@kirbyfooty .com" (was
"so*****@yahoo. com")
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

Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5681
by: Philipp Ott | last post by:
Hello! I m looking for a solution to generate a digitally signed mime-email with linux/perl and to feed/pipe this then to sendmail. I found RFCs related to mime-signed etc. but somehow fail to implement them or rather make it work. Is there a fininshed cpan module that can do this? I didnt find one though. Second, what if i want to use verisign/thawte digital certs for email
2
9601
by: Ronny Sigo | last post by:
Hello all, I a struggling for a few days now to find a way to send automated mail. I succeeded except that MS Outlook always gives me those 2 stupid warnings that 1) a "program tries to get access to the emailadresses in the contactlist - and wether I want to allow this" and 2) That a program is trying to send emails on my behalf - and wether I want to allow this. ... I have tried to use the redemption package but obviously I do not...
6
1901
by: Bob Alston | last post by:
I am using the Outlook library to send emails from Access. I have Outlook open and run a program to respond yes to the security question (Express click Yes). It seems to take about 5 seconds for each email to get sent. Does this seem like the normal amount of time for others who do this? Any tricks to speed it up?
3
3117
by: Carlos Lozano | last post by:
Hi, I know I can create e-mails and send them using System.Web.Mail, but I was wondering how can I do it using the POP3 settings on Office Outlook or Outlook Express. The problem I have is some users are being denied access to relying directly to the smtp server, but they can send using outlook's POP3 settings. Thanks,
4
7288
by: roni | last post by:
i dont like to use ocx controlx. is there new dll for vb.net that do the job ? or newer code, to send email throught outlook express.
14
9895
by: sridhar | last post by:
iam having user account on an exchangeserver. with that can i send an email using python? if iam using the following code iam getting error fromAddress = 'sridhar_kasturi@satyam.com' toAddress = 'sridhar_kasturi@satyam.com' msg = "Subject: Hello\n\nThis is the body of the message." import smtplib
4
2296
by: =?Utf-8?B?Um9nZXIgR2FycmV0dA==?= | last post by:
I'm having a problem using the System.Net.Mail.SmtpClient. After I set everything up and issue the Send method it throws an exception indicating "Sorry, that domain isn't in my list of allowed rcpthosts", which is apparently coming from the mail server, smtpout.secureserver.net. However, I do indeed have the email address set up properly (with GoDaddy) and I have successully set up Outlook Express with the appropriate paremeters so that...
2
1454
by: =?Utf-8?B?U3VzYW4=?= | last post by:
I can't send emails on my Microsoft outlook express 6. HELP?
0
8674
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7723
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5860
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4366
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3045
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 we have to send another system
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.