Gmail SMTP authentication always fail - python

I created this software in python 3
import smtplib
TO = 'anywhere#mail.com'
SUBJECT = 'Text subject of the mail'
TEXT = 'Text of the mail'
gmail_sender = 'yourMail#gmail.com'
gmail_passwd = 'password'
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(gmail_sender, gmail_passwd)
BODY = '\r\n'.join(['To: %s' % TO,
'From: %s' % gmail_sender,
'Subject: %s' % SUBJECT,
'', TEXT])
try:
server.sendmail(gmail_sender, [TO], BODY)
print ('email sent')
except:
print ('error sending mail')
server.quit()
The first day the software worked well but now I always have an authentification error from gmail :
smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials o81-v6sm9180362wmo.38 - gsmtp')
What I did already is :
I desactivated the unlock captchar from this link :
https://accounts.google.com/DisplayUnlockCaptcha
and I have enabled the less secure apps from this link:
https://www.google.com/settings/security/lesssecureapps
But still the same, I can't send an e-mail anymore. It doesn't seems that there's a Python software problem because I wrote the same software in Go and I still receive the same error.

It’s solved. My login was wrong.

Related

Sending email using smtplib without logging in [duplicate]

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)

How can I send e-mails from python without having to enable less secure apps?

I am having trouble sending an e-mail without having to enable google's less secure apps: https://myaccount.google.com/lesssecureapps . How can I modify the code in Python so that I can send e-mail without having to enable this? I need to disable this option, because I am building an application in which the user can send a feedback e-mail to the developer, so I cannot force the user to enable less secure apps.
My code in Python:
def send_email(self):
print("send e-mail to developer")
sender_email = self.email.text
sender_password = self.password.text
sender_feedback = self.feedback_message.text
print("email is: ", type(sender_email))
print("pass is: ", sender_password)
print("feedback message is: ", sender_feedback)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls() # encrypt connection
server.login(sender_email, sender_password)
subject = 'Feedback'
msg = sender_feedback
msg = f"Subject: {subject} \n\n {msg}"
server.sendmail(
# from,to,message
sender_email,
'my_email#gmail.com',
msg
)
print("E-MAIL HAS BEEN SENT!")
server.quit()
According to POP3/SMTP compatible with 2 factor authentication? - Help - Gmail, you need to set up an "app password" to be used instead of your regular password to be able to use an SMTP client when "2-step verification" is turned on for your Google account.

Python gmail-smtp script works from win\3.6, not linux\3.5.2

The following script works perfectly fine from my Win 10 box using Python 3.6:
import smtplib,subprocess
def send_email(user, pwd, recipient, subject, body):
FROM = user
TO = recipient if isinstance(recipient, list) else [recipient]
SUBJECT = subject
TEXT = body
# Prepare actual message
message = """From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
try:
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(user, pwd)
server.sendmail(FROM, TO, message)
server.close()
print ('successfully sent the mail')
except Exception as e:
print(e)
print ("failed to send mail")
send_email("user#gmail.com", "pass", "dst#email.com", "some subject", "some body")
However, it fails with a
"Please log in via your web browser and\n5.7.14 then try again"
when I try to run it from a Ubuntu 16.04 box and Python 3.5.2.
I should mention that "allow unsecure apps" is enabled on my gmail account (didnt work from the Win box without it).

Can't send mail : smtplib.SMTPAuthenticationError

def send_email(user, pwd, recipient, subject, body):
import smtplib
gmail_user = user
gmail_pwd = pwd
FROM = user
TO = ['dhe*****#***.com']
SUBJECT = subject
TEXT = body
# Prepare actual message
message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
server.sendmail(FROM, TO, message)
server.close()
print 'successfully sent the mail'
if __name__ == '__main__':
send_email('abc#gmail.com', 'password', '', 'Hello', 'hello')
I am using this script to send email from abc#gmail.com. (hardcoded the recipient for now)
But I am getting
smtplib.SMTPAuthenticationError: (534, '5.7.14 https://accounts.google.com/signin/ .....
Please log in via your web browser and\n5.7.14 then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/answer/78754 dl17sm699785obc.9 - gsmtp')
Access for Less secure apps is already turned on for that account (abc#gmail.com).
There is also NO 2-factor authentication for that account.
I can't login through a web-browser on that machine because it is an EC2 Instance. How do I proceed here?

Sending email using smtplib doesn't work anymore

So yesterday I had this bit of code written out and it worked perfectly fine, but today it's not sending e-mails anymore. Can someone explain why?
import smtplib
SERVER = 'owa.server.com'
FROM = 'noreply#server.com'
TO = ['person#gmail.com', '1112223344#vtext.com']
name = 'Mr. Man'
SUBJECT = 'Recent Information for: %s' % (name)
TEXT = "Dear " +name+ ",\n\nHello.\n\nSincerely,\nOur Guys Here"
message = """From: %s\r\nTo: %s\r\nSubject: %s\r\n\
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
server = smtplib.SMTP(SERVER, 587)
server.ehlo()
server.starttls()
server.ehlo
server.login('noreply#server.com', 'password')
server.sendmail(FROM, TO, message)
server.quit()
This code is a working snippet. I wasn't getting the e-mails in my personal gmail account because gmail was sending it to the spam folder. I checked to see if it works at my office account, and it did just fine.
import smtplib
# Specifying the from and to addresses
fromaddr = 'fromuser#gmail.com'
toaddrs = 'to#gmail.com'
# Writing the message (this message will appear in the email)
msg = 'Enter you message here'
# Gmail Login
username = 'username'
password = 'password'
# Sending the mail
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
Above standard smtp send works with gmail,
thus it must be your server(whatever you're using) configuration that is at fault.

Categories