Sending email

7stud bbxx789_05ss at
Fri Aug 28 14:40:46 CEST 2009

On Aug 28, 2:37 am, Fencer <no.i.d... at>
> 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.connect('')
> mail_server.ehlo()
> mail_server.starttls()
> mail_server.ehlo()
> 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>
>      mail_server.ehlo()
>    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
>      self.send(str)
>    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

The docs say:

class smtplib.SMTP_SSL([host[, port[, local_hostname[, keyfile[,
certfile[, timeout]]]]]])

A SMTP_SSL instance behaves exactly the same as instances of SMTP.
SMTP_SSL should be used for situations where SSL is required from the
beginning of the connection and using starttls() is not appropriate.

So try getting ride of these two lines:

> mail_server.starttls()
> mail_server.ehlo()

There's also some info on google about a bug in this regard, so check
to see when you installed python 2.6.2.

More information about the Python-list mailing list