Eric
Eric

Reputation: 53

I would like to define a server using smtplib on Python but an error occurred

A program I wanted to create requires to login to a mail server. I was able to login using smtplib on Gmail, but it didn't work on Yahoo mail.

SMTP Port 465 Server mail: smtp.mail.yahoo.co.jp

python code on IDLE

import smtplib
smtp_obg = smtplib.SMTP("smtp.mail.yahoo.co.jp", 465)

Error message

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    smtp_obg = smtplib.SMTP("smtp.mail.yahoo.co.jp", 465)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 253, in __init__
    (code, msg) = self.connect(host, port)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 341, in connect
    (code, msg) = self.getreply()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 398, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed

I would appreciate if someone could help. Thank you in advance.

Upvotes: 0

Views: 164

Answers (2)

ParisNakitaKejser
ParisNakitaKejser

Reputation: 14979

When you create a connection to your smtp server you need to know more about the server about what way you want to connect to it.

There are normal with and without SSL connection based on port number to your SMTP server.

I use this site https://settings.email/yahoo-co-jp-email-settings/ to get more info about your SMTP server you want to use from yahoo and it says the port 465 is an SSL connection so you need to connect with SSL connection else you can't sending out this way.

in smtplib in python you have smtplib.SMTP and smtplib.SMTP_SSL functions to work with, the first it's used when it's not SSL requirement, and the second its used when you need SSL like yahoo smtp.

Can you try out to use smtplib.SMTP_SSL function and see about its working for you?

import smtplib
smtp_obg = smtplib.SMTP_SSL("smtp.mail.yahoo.co.jp", 465)

Mabey its required a username and password to sending out, I did not have so much info about yahoo SMTP to can explain that part for you.

Upvotes: 0

rzlvmp
rzlvmp

Reputation: 9482

  1. You must run login after creating smtp_obj (smtp_obg.login('username', 'password')). If you trying to login, please mention it.

  2. You must activate SMTP feature at Yahoo's web page (Same as Gmail). Sorry for Japanese, but not sure if yahoo.co.jp is using somewhere outside Japan. :)

    In English, this parameter should be called like: IMAP/POP/SMTP access and mail transfer

I checked my account. STMP access is disabled by default.

enter image description here

Upvotes: 1

Related Questions