1

For doing this I started following the link New to Python, GMail SMTP error. And my code is

import smtplib

sender = "[email protected]"
receiver = ["[email protected]"]
message = "Hello!"

try:
    session = smptlib.SMTP('smtp.gmail.com',587)
    session.ehlo()
    session.starttls()
    session.ehlo()
    session.login(sender,'mypassword')
    session.sendmail(sender,receiver,message)
    session.quit()

except smtplib.SMTPException:
    print('Error')

But I am getting the following error:

Traceback (most recent call last):
  File "email2.py", line 1, in <module>
    import smtplib
  File "/usr/lib/python2.7/smtplib.py", line 46, in <module>
    import email.utils
  File "/home/ramkrishna/test/email.py", line 17, in <module>
    except SMTPException:
NameError: name 'SMTPException' is not defined
5
  • hard to know without see code Commented Jul 29, 2014 at 10:19
  • Did you add from smtplib import SMTPException Commented Jul 29, 2014 at 10:28
  • @PadraicCunningham: Now I added code in my question post. Commented Jul 29, 2014 at 10:36
  • @Sar009: I tried to add it in place of "except smtplib.SMTPException:" but then I got the following error: File "email2.py", line 16 from smtplib import SMTPException ^ SyntaxError: invalid syntax Commented Jul 29, 2014 at 10:39
  • @ramkrishna I added the answer Commented Jul 29, 2014 at 10:40

3 Answers 3

1

It looks like you have to use the full name (smtplib.SMTPException)

try switching "except SMTPException:" with:

"except smtplib.SMTPException:"

Sign up to request clarification or add additional context in comments.

Comments

1

session = smptlib.SMTP('smtp.gmail.com',587) should be session = smtplib.SMTP('smtp.gmail.com',587)

smptlib != smtplib

import smtplib

sender = "[email protected]"
receiver = ["[email protected]"]
message = "Hello!"

try:    
    session = smtplib.SMTP('smtp.gmail.com',587)
    session.ehlo()
    session.starttls()
    session.ehlo()
    session.login(sender,'mypassword')
    session.sendmail(sender,receiver,message)
    session.quit()

except smtplib.SMTPException:
    print('Error')

The code with corrected spelling runs fine. When I change:

except smtplib.SMTPException: print('Error')

to

except smtplib.SMTPException as e:
    print(e)

I get the followoing output on python2 and python 3:

(535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 http://support.google.com/mail/bin/answer.py?answer=14257 ev18sm10203485wid.1 - gsmtp')

5 Comments

@ramkrishna, the code runs, it send s mail successfully and catches the error when I put in invalid details
Ok. Then may be there is some problem with some library or something else in my pc.
do you have any other lines in your script?
no. even I tried just copy and paste your written code but it did not work.
The error I got while using the above script was that google didn't allow the sign-in.They have a security feature I received a mail about it. On disabling the feature the script worked. Using the following links for the disable the security feature [ link ](google.com/settings/security/lesssecureapps)
0
    File "/home/ramkrishna/test/email.py", line 17, in <module>
    except SMTPException:
NameError: name 'SMTPException' is not defined

from this trace - i presume, you tried to write a python script with file name as email.py

when you do that, this email.py overrides the python libraries email.py (with the same name) - as the current directory has precedence over the python library.

eventually, you might have changed your script name as email2.py, however you had already attempted to run email.py once, so you must be having a email.pyc in the mentioned folder

File "/home/ramkrishna/test/email.py"

if you had tried a different filename in a different folder, it might have worked or you could rename your email.py to sendMyEmail.py and make sure you remove the .pyc file too.

the question is an old one. thought this answer could help some one new.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.