I want to send email with smtplib and no app password. But i get this output:
with smtplib.SMTP("blabla@outlook.com",587) as prt: socket.gaierror: [Errno 11003] getaddrinfo failed
My wrong code:
import smtplib
with smtplib.SMTP("blabla@outlook.com",587) as prt:
prt.starttls()
prt.login(user="blabla@outlook.com",password="password")
prt.sendmail(from_addr="blabla@outlook.com",to_addrs="albalb@outlook.com",msg="Subject:Tester\n\nHello")
The mentioned error occurs because of incorrect information in the code causes failure while trying to connect to the SMTP server. Also you have to provide the domain service. Here in your code it is outlook.com, so 'smtp-mail.outlook.com'.
I have imported the EmailMessage for better formatting of the code, and that is way better way to understand. Also we are using port 465 for SSL connection. You can also add multiple recipients inside the contacts list separated by a comma.
import os
import smtplib
from email.message import EmailMessage
#credentials
email_user = YOUR EMAIL ID HERE
email_pass = YOUR PASSWORD HERE
contacts = ['ENTER RECIPIENTS HERE']
sender = email_user
to = contacts
msg = EmailMessage()
msg['Subject'] = 'YOUR EMAIL SUBJECT HERE'
msg['From'] = sender
msg['To'] = ', '.join(contacts)
msg.set_content('YOUR EMAIL MESSAGE HERE')
with smtplib.SMTP_SSL('smtp-mail.outlook.com', 465) as smtp:
smtp.login(email_user, email_pass)
smtp.send_message(msg)