I am trying to send an email with python, but it keeps saying ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1056)
. Here is my code:
JavaScript
x
8
1
server = smtplib.SMTP_SSL('smtp.mail.com', 587)
2
server.login("something0@mail.com", "password")
3
server.sendmail(
4
"something0@mail.com",
5
"something@mail.com",
6
"email text")
7
server.quit()
8
Do you know what is wrong?
Advertisement
Answer
The port for SSL
is 465 and not 587, however when I used SSL
the mail arrived to the junk mail.
For me the thing that worked was to use TLS
over regular SMTP
instead of SMTP_SSL
.
Note that this is a secure method as TLS
is also a cryptographic protocol (like SSL).
JavaScript
1
20
20
1
import smtplib, ssl
2
3
port = 587 # For starttls
4
smtp_server = "smtp.gmail.com"
5
sender_email = "my@gmail.com"
6
receiver_email = "your@gmail.com"
7
password = input("Type your password and press enter:")
8
message = """
9
Subject: Hi there
10
11
This message is sent from Python."""
12
13
context = ssl.create_default_context()
14
with smtplib.SMTP(smtp_server, port) as server:
15
server.ehlo() # Can be omitted
16
server.starttls(context=context)
17
server.ehlo() # Can be omitted
18
server.login(sender_email, password)
19
server.sendmail(sender_email, receiver_email, message)
20
provided thanks to the real python tutorial.