Sending email

Fencer no.i.dont at
Fri Aug 28 10:37:15 CEST 2009

Hello, I'm using Python version 2.6.2 and I discovered it has built-in 
libraries for sending email (imports smtplib and email). On the web I 
discovered how to send mail through

mail_server = smtplib.SMTP()

mail_server.login('username', 'password')

msg = MIMEText('Some message text')
msg['Subject'] = 'Some subject'
msg['From'] = 'Mr Underhill'
msg['To'] = 'someemailaddress'

me = 'myemailaddress'
mail_server.sendmail(me, ['someemailaddress'], msg.as_string())

That seems to work just fine but for the messages I will be sending I 
don't want to use my private GMail account.
We have an SMTP server at work, and I checked its settings in the 
Thunderbird mail client and it's using SSL over port 465, so a bit 
different from how GMail's SMTP server is configured in Thunderbird.

I altered my code slightly to account for this:
mail_server = smtplib.SMTP_SSL()
mail_server.connect('', 465)

Everything I left untouched. However, it doesn't work:
Traceback (most recent call last):
   File "C:\Users\fencer\workspace\Send Email\src\", line 
16, in <module>
   File "C:\Python26\lib\", line 382, in ehlo
     self.putcmd(self.ehlo_msg, name or self.local_hostname)
   File "C:\Python26\lib\", line 318, in putcmd
   File "C:\Python26\lib\", line 310, in send
     raise SMTPServerDisconnected('please run connect() first')
smtplib.SMTPServerDisconnected: please run connect() first

As you can see, I'm on Windows Vista.

What do I need to change in my code to fix this problem? Thunderbird 
manages to do it! :) Ideally, I would like send an email that the 
recipient can't reply to, but if doesn't work, it's fine too. I want to 
use this to send automated emails to students with account information 
for a course.

- Fencer

More information about the Python-list mailing list