Error in email-sending program in Python with smtplib - python-3.x

Here is my code:
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login("MyEmail", "pass")
msg = "YOUR MESSAGE!"
server.sendmail("MyEmail", "MyEmail", msg)
server.quit()
The error message is the following:
Please log in via your web browser and then try again.\n5.7.14 Learn more at\n5.7.14
What is the solution to my problem?

You need permissions, go to this link https://www.google.com/settings/security/lesssecureapps and allow the access

Related

authentication error trying to send Outlook email from Python

I'm testing out a simple script to send an Outlook email from Python 3 (using Spyder).
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
username = 'my_username#my_company.com'
password = 'my_password'
mail_from = username
mail_to = username
mail_subject = "Test Subject"
mail_body = "This is a test message"
mimemsg = MIMEMultipart()
mimemsg['From']=mail_from
mimemsg['To']=mail_to
mimemsg['Subject']=mail_subject
mimemsg.attach(MIMEText(mail_body, 'plain'))
try:
connection = smtplib.SMTP(host='smtp.office365.com', port=587)
connection.starttls()
connection.login(username,password)
except Exception as e:
print('Got error here')
print(e)
And the output is:
Got error here
(535, b'Authentication unsuccessful, the user credentials were incorrect. [SOME_VALUE_HERE.hostname.prod.outlook.com]')
I know for sure my own username and email are correct - I verified by checking my username's properties > SMTP value. And anyway it's the username I use to login to Windows.
I'm also using the same password for logging into Windows.
Is it possible my company uses different values for host or port? Or on the backend it sends a different user name to the SMTP server?
The error indicates that SMTP authentication is disabled. Read more about that on the page at https://aka.ms/smtp_auth_disabled. The link explains how to enable SMTP AUTH for the whole organization or only for some mailboxes.
Also take a look at the following settings that would block Legacy Authentication:

I am trying to send a gmail text to someone

so I tried to send it gave me this error
here is the error
Traceback (most recent call last):
File "C:\Users\23470\AppData\Local\Programs\Python\Python39\mess.py", line 6, in <module>
server = smtplib.SMTP(smtpServer,25)
File "C:\Users\23470\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 258, in __init__
raise SMTPConnectError(code, msg)
smtplib.SMTPConnectError: (451, b'Request action aborted on MFE proxy, SMTP server is not
available.')
Here is my code
import smtplib
smtpServer='smtp.yourdomain.com'
fromAddr='imranalubankudi#gmail.com'
toAddr='gmmeremnwanne#gmail.com'
text= "This is a test of sending email from within Python."
server = smtplib.SMTP(smtpServer,25)
server = smtplib.SMTP(smtpServer,25)
server.ehlo()
server.starttls()
server.sendmail(fromAddr, toAddr, text)
server.quit()
Try changing port to 587. Also your smtpServer don't looks right for gmail.
server = smtplib.SMTP('smtp.gmail.com', 587)
You should also need to login to session.
Here's a simple function that sends a simple text mail through gmail.
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def send_mail(send_from, send_to, subject, body):
#The mail addresses and password
sender_pass = 'your_gmail_password'
#Setup the MIME
message = MIMEMultipart()
message['From'] = send_from
message['To'] = send_to
message['Subject'] = subject
message.attach(MIMEText(body, 'plain'))
session = smtplib.SMTP('smtp.gmail.com', 587)
session.starttls()
session.login(send_from, sender_pass)
text = message.as_string()
session.sendmail(send_from, send_to, text)
session.quit()

How to send an email without login to server in Python

I want to send an email without login to server in Python. I am using Python 3.6.
I tried some code but received an error. Here is my Code :
import smtplib
smtpServer='smtp.yourdomain.com'
fromAddr='from#Address.com'
toAddr='to#Address.com'
text= "This is a test of sending email from within Python."
server = smtplib.SMTP(smtpServer)
server.set_debuglevel(1)
server.sendmail(fromAddr, toAddr, text)
server.quit()
I expect the mail should be sent without asking user id and password but getting an error :
"smtplib.SMTPSenderRefused: (530, b'5.7.1 Client was not authenticated', 'from#Address.com')"
I am using like this. It's work to me in my private SMTP server.
import smtplib
host = "server.smtp.com"
server = smtplib.SMTP(host)
FROM = "testpython#test.com"
TO = "bla#test.com"
MSG = "Subject: Test email python\n\nBody of your message!"
server.sendmail(FROM, TO, MSG)
server.quit()
print ("Email Send")
import win32com.client as win32
outlook=win32.Dispatch('outlook.application')
mail=outlook.CreateItem(0)
mail.To='To address'
mail.Subject='Message subject'
mail.Body='Message body'
mail.HTMLBody='<h2>HTML Message body</h2>' #this field is optional
# To attach a file to the email (optional):
attachment="Path to the attachment"
mail.Attachments.Add(attachment)
mail.Send()
The code below worked for me.
First, I opened/enabled Port 25 through Network Team and used it in the program.
import smtplib
smtpServer='smtp.yourdomain.com'
fromAddr='from#Address.com'
toAddr='to#Address.com'
text= "This is a test of sending email from within Python."
server = smtplib.SMTP(smtpServer,25)
server.ehlo()
server.starttls()
server.sendmail(fromAddr, toAddr, text)
server.quit()
First, you have to have a SMTP server to send an email. When you don't have one, usually outlook's server is used. But outlook only accepts authenticated users, so if you don't want to login into the server, you have to pick a server that doesn't need authentication.
A second approach is to setup an internal SMTP server. After you setup the internal SMTP server, you can use the "localhost" as the server to send the email. Like this:
import smtplib
receiver = 'someonesEmail#hisDomain.com'
sender = 'yourEmail#yourDomain.com'
smtp = smtplib.SMTP('localhost')
subject = 'test'
body = 'testing plain text message'
msg = 'subject: ' + subject + ' \n\n' + body
smtp.sendmail('sender', receiver, msg)

sending mail from python in 2018

I know there are a lots of how to send mail questions before, but does sending mail from python scripts not work anymore? I've tried to use the SMTP lib with the following script:
import smtplib
fromaddr = 'mymail#gmail.com'
toaddr = 'someones#gmail.com'
msg = "\r\n".join([
"From: mymail#gmail.com",
"To: someonesmail#gmail.com",
"Subject: Just a message",
"",
'Why, oh why?'
])
username = 'mymail#gmail.com'
pwd = 'mypassword'
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(username, pwd)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
But it says to login via a web browser, and this post says that google now uses an api to send mails. So, having confusion about the possibility to send mails via scripts without using any of google's api, Is it possible?

Not able to send the mail without password authentication in python

I am not able to send the mail without password authentication in python.Its printing the last message but mail is not sent.Below is the code that i am using.Anyone please help i am stuck because of this. Thanks in advance.
import smtplib
fromaddr = 'xyz#gmail.com'
toaddrs = ['abc#gmail.com']
msg = '''
From: {fromaddr}
To: {toaddr}
Subject: 'testing'
This is a test
.
'''
msg = msg.format(fromaddr =fromaddr, toaddr = toaddrs[0])
server = smtplib.SMTP("gmail-smtp-in.l.google.com:25")
server.starttls()
server.ehlo("example.com")
server.mail(fromaddr)
server.rcpt(toaddrs[0])
server.data(msg)
server.quit/close()
print("mail sent")

Resources