Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

pip - Getting errors while running SMTP python library

Hey I'm working on an Email Bot using the SMTP python library. Can someone explain the error I'm getting and also mention the solution to this problem.

I'm using the Pycharm code editor and this is my code:

# importing mail protocol package
import smtplib
server = smtplib.SMTP('abcdefghijk@gmail.com', '******')
server.starttls()
server.login('abcdefghijk@gmail.com'
'!#$$%^X12345@gmail.com',
'This is just a test to see if Rocland email bot works or not.')

This is the error I'm getting...

/home/rohan/Rocland_Python/Rocland/venv/bin/python 
 "/home/rohan/Rocland_Python/Rocland/Rocland Email Bot.py"
 Traceback (most recent call last):
 File "/home/rohan/Rocland_Python/Rocland/Rocland Email Bot.py", line 3, in <module>
  server = smtplib.SMTP('Windy.eagle2007@gmail.com', 'rocland!2007')
  File "/usr/lib/python3.9/smtplib.py", line 253, in __init__
      (code, msg) = self.connect(host, port)
   File "/usr/lib/python3.9/smtplib.py", line 339, in connect
   self.sock = self._get_socket(host, port, self.timeout)
    File "/usr/lib/python3.9/smtplib.py", line 310, in _get_socket
      return socket.create_connection((host, port), timeout,
           File "/usr/lib/python3.9/socket.py", line 822, in create_connection
     for res in getaddrinfo(host, port, 0, SOCK_STREAM):
       File "/usr/lib/python3.9/socket.py", line 953, in getaddrinfo
      for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
      socket.gaierror: [Errno -8] Servname not supported for ai_socktype

   Process finished with exit code 1

I would really appreciate it if you explained this error and give me the solution to the problem. Thanks in advance!

question from:https://stackoverflow.com/questions/65839529/getting-errors-while-running-smtp-python-library

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should connect to a server (not a email address), and the second parameter is the port (not the password).

The error tell you (in a cryptic way) that the format of server name is not recognized, and so it cannot connect (connections go via sockets).

Check the protocol in the official Python documentation: smtplib module. The constructor signature is:

 class smtplib.SMTP(host='', port=0, local_hostname=None, [timeout, ]source_address=None)

so SMTP(host, port, ...)

PS: It is Python, not PyCharm. You must distinguish editor (PyCharm) from programmin language (Python). This will help you to find quicker answers.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...