[Tutor] How to connect to an email server?

Tim Golden mail at timgolden.me.uk
Mon Mar 16 21:09:41 CET 2009


Eduardo Vieira wrote:
> Hello, I'm fairly new to programming and Python and am trying to
> explore Python's email libraries. But I am having trouble making a
> connection with an email server, I guess.
> I'm running Windows XP, in a server environment. I have administrator
> privileges to my machine (not the server machine) to install, delete
> programs, for example. My email program (Outlook 2007) says that
> Exchange Server is albertapdc.express.local
> I would like to send emails using smtp in a script, but when I do
> these lines, I get an error:
> 
> import smtplib
> server = smtplib.SMTP('localhost') # if I replace 'localhost' with
> 'albertapdc.express.local' I get the same.
> 
> Traceback (most recent call last):
>   File "<input>", line 1, in <module>
>   File "C:\Python25\lib\smtplib.py", line 244, in __init__
>     (code, msg) = self.connect(host, port)
>   File "C:\Python25\lib\smtplib.py", line 310, in connect
>     raise socket.error, msg
> error: (10061, 'Connection refused')
> 
> The same error 'Connection refused' I get trying IMAP:
> import imaplib
> se = imaplib.IMAP4('albertapdc.express.local')
> Traceback (most recent call last):
>   File "<input>", line 1, in <module>
>   File "C:\Python25\lib\imaplib.py", line 163, in __init__
>     self.open(host, port)
>   File "C:\Python25\lib\imaplib.py", line 230, in open
>     self.sock.connect((host, port))
>   File "<string>", line 1, in connect
> error: (10061, 'Connection refused')
> 
> What should I do? And how can I know I can use smtp or IMAP?


SMTP has to be enabled specifically on the Exchange Server:
it usually uses its own built-in protocols. It looks
as though your Exchange isn't running it. Either switch it
on (or ask your admins to do so) or look at using MAPI
or CDO instead.

TJG


More information about the Tutor mailing list