Python: smtplib: error AAttributeError: module 'smtplib' has no attribute 'SMTP' 」

Asked 2 years ago, Updated 2 years ago, 137 views

When I try to send an email via Gmail using smtp.log on Python 3, they say AttributeError: module 'smtplib' has no attribute 'SMTP'.Please tell me what's wrong.

batchMailerOne.py

#!/usr/bin/env python3
#
# # batchMailerOne.py
# -*-coding:utf-8-*-

#### START CUSTOMIZATION ####
smtp_host='smtp.gmail.com'
smtp_port = 587
from_email='foo<[email protected]>'
mail_subject='bar'
user_name='xxxxx no suchmail [email protected]'#Gmail login name
user_password='xxxxxxxx'#Gmail pssswd (2 steps authorization is not supported)

# set mail address surrounded in double quote, ended with comma (you can send up to 100 mails a day)
to_emails=[
        "recipient1<[email protected]>",
        "recipient2<[email protected]>",
        "recipient3<[email protected]>",
]

# write your message between ' ' ' ' and ' ' '
message_text = ''
Dear someone,

Hello
'''

### END CUSTOMIZATION###

from email import message
import smtplib

server=smtplib.SMTP(smtp_host, smtp_port)
server.ehlo()
server.starttls()
server.ehlo()
server.login(user_name, user_password)

for to_email in to_emails:
        msg = message.EmailMessage()
        msg.set_content(message_text)
        msg ['Subject'] = mail_subject
        msg ['From'] = from_email
        msg ['To'] = to_email
        server.send_message(msg)

server.quit()

Runtime Error

Traceback (most recent call last):
  File "C:\Users\cf\batchMailerOne.py", line 31, in <module>
    import smtplib
  File "C:\Users\cf\Anaconda3\lib\smtplib.py", line 49, in<module>
    import email.generator
  File "C:\Users\cf\Anaconda3\lib\email\generator.py", line 14, in<module>
    from copy import deepcopy
  File "C:\Users\cf\Anaconda3\lib\copy.py", line 60, in<module>
    from org.python.core import PyStringMap
  File "C:\Users\cf\batchMailerOne.py", line 39, in <module>
    server=smtplib.SMTP(smtp_host, smtp_port)
AttributeError: module 'smtplib' has no attribute 'SMTP'

Python version

 C:\Users\cf>\Users\cf\Anaconda3\python.exe-V
Python 3.6.3:: Anaconda, Inc.

python python3 gmail

2022-09-30 19:23

2 Answers

Do you happen to have a file called org.pyc compiled by bytes in the batchMailer\Resources folder?

-- This is to post comment from metropolis as a community wiki response


2022-09-30 19:23

In my (non-anaconda, original) environment, an error occurred much earlier.
Imagine that there is a problem with the environment created in Anaconda.

 smtplib.SMTPAUTHENTICATIONError: (535, b'5.7.8 Username and Password not accepted.Learn more at
5.7.8 https://support.google.com/mail/?p= BadCredentials t4-v6sm41336798 pfh.45-gsmtp')


2022-09-30 19:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.