Related to smtplib exercise on Network Porgramming with Python

I am working through FCC video with NeuralNine and I keep getting a Traceback:

Traceback (most recent call last):
  File "c:\Users\Admin\Desktop\PY4E\ex_02_05\mail.py", line 8, in <module>
    server = smtplib.SMTP('smtp.gmail.com', 465)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\smtplib.py", line 255, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\smtplib.py", line 343, in connect
    (code, msg) = self.getreply()
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\smtplib.py", line 405, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed

I’ve had trouble with the port number. 22 was used in the video, but that didn’t work. >I searched for the Gmail port number and used both the SSL and TLS port numbers…neither of which worked. Now, I´m not sure. Where am I screwing up?

My code is below:

import smtplib

from email import encoders

from email.mime.text import MIMEText

from email.mime.base import MIMEBase

from email.mime.multipart import MIMEMultipart

server = smtplib.SMTP('smtp.gmail.com', 465)

server.ehlo()

with open('password.txt', 'r') as f:

password = f.read()

server.login('roydev2215@gmail.com', password)

msg = MIMEMultipart()

msg['From'] = 'Gmail'

msg['To'] = 'bbburnside@outlook.com'

msg['Subject'] = 'Just a test'

with open('message.txt, 'r'') as f:

message = f.read()

msg.attach(MIMEText(message, 'plain'))

filename = 'baixa.jpg'

attachment = open(filename, 'rb')

p = MIMEBase('application', 'octat-stream')

p.set_payload(attachment.read())

encoders.encode_base64(p)

p.add_header('Content-Disposition', f'attachment, filename={filename}')

msg.attach(p)

text = msg.as_string()

server.sendmail('roydev2215@gmail.com', 'bburnside"outlook.com', text)

Did you try this? https://stackoverflow.com/questions/59179964/python3-smtp-connection-unexpectedly-closed

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.