Sending email



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 smtp.gmail.com:

mail_server = smtplib.SMTP()

mail_server.connect('smtp.gmail.com')
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('mysmtpserver.at.work', 465)

Everything I left untouched. However, it doesn't work:
Traceback (most recent call last):
File "C:\Users\fencer\workspace\Send Email\src\send_email.py", line 16, in <module>
mail_server.ehlo()
File "C:\Python26\lib\smtplib.py", line 382, in ehlo
self.putcmd(self.ehlo_msg, name or self.local_hostname)
File "C:\Python26\lib\smtplib.py", line 318, in putcmd
self.send(str)
File "C:\Python26\lib\smtplib.py", 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
.



Relevant Pages

  • Re: Sending email
    ... libraries for sending email. ... different from how GMail's SMTP server is configured in Thunderbird. ... I altered my code slightly to account for this: ...
    (comp.lang.python)
  • Re: Must have softwares for Treo 650
    ... >>> What problems are you having sending email? ... >>> be able to at least use Sprint's SMTP server. ... > setup an email account, you usually provide only the login information ... > logged into your home ISP, and sending mail through their SMTP server). ...
    (comp.sys.palmtops.pilot)
  • Re: Must have softwares for Treo 650
    ... >>> What problems are you having sending email? ... >>> be able to at least use Sprint's SMTP server. ... > setup an email account, you usually provide only the login information ... > logged into your home ISP, and sending mail through their SMTP server). ...
    (comp.sys.palmtops.pilot)
  • Re: OT: Question about spam
    ... Once I get this killfile extension done, Thunderbird will be the ROCK for newsgroups. ... Being not only a powerful and free mail client Thunderbird has a reasonably news interface as well. ... On an already running version you choose Account Settings from the Tools menu and click Add Account. ... In Account Wizard check "Newsgroup Account" and proceed. ...
    (rec.gambling.poker)
  • RE: Unable to send SMTP mail with alternate FROM address
    ... I have a computer system with Thunderbird on it. ... I configure a second account in Thunderbird for my gmail account. ... Exchange server with an alternate FROM address. ... The SMTP server is the secure virtual server that I used. ...
    (microsoft.public.exchange.admin)

Loading