1

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

import smtplib

sender = "noreply@gmail.com"
receiver = ["ram@gmail.com"]
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
Community
  • 1
  • 1
ramkrishna
  • 632
  • 1
  • 7
  • 24

3 Answers3

1

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

try switching "except SMTPException:" with:

"except smtplib.SMTPException:"

Pownyan
  • 475
  • 1
  • 6
  • 23
1

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

smptlib != smtplib

import smtplib

sender = "noreply@gmail.com"
receiver = ["ram@gmail.com"]
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')
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • @ramkrishna, the code runs, it send s mail successfully and catches the error when I put in invalid details – Padraic Cunningham Jul 29 '14 at 10:53
  • Ok. Then may be there is some problem with some library or something else in my pc. – ramkrishna Jul 29 '14 at 11:11
  • do you have any other lines in your script? – Padraic Cunningham Jul 29 '14 at 11:12
  • no. even I tried just copy and paste your written code but it did not work. – ramkrishna Jul 29 '14 at 11:14
  • 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 ](https://www.google.com/settings/security/lesssecureapps) – Anirudh Dec 23 '14 at 04:38
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.

Raghav
  • 145
  • 1
  • 5