1

I am trying to send an email. The code I am using is found below:

import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart email_user = '[email protected]' email_password = 'password' email_send = '[email protected]' subject = 'subject' msg = MIMEMultipart() msg['From'] = email_user msg['To'] = email_send msg['Subject'] = subject body = 'Hi there, sending this email from Python!' msg.attach(MIMEText(body,'plain')) try: text = msg.as_string() server = smtplib.SMTP('smtp.gmail.com', port=587) server.starttls() server.login(email_user, email_password) server.sendmail(email_user,email_send,text) server.quit() print('successfully sent the mail') except: print("failed to send mail") 

I get an error "failed to send mail" with the error message:

SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials t10sm6451859wra.16 - gsmtp')

The error occurs on the server.login() line, I am not able to login. I checked other post and it says, it has to do with wrong credentials but my credentials are correct. I have check and double checked.

What could be the problem with this and how do I resolve it?

4
  • 5
    The error message contains a link to gmail support that suggests that you may have to allow less secure apps. Did you read and try it? Commented Dec 14, 2017 at 11:00
  • 1
    @SergeBallesta Thanks for the help. Problem resolved! Commented Dec 14, 2017 at 12:36
  • Possible duplicate of Receive and send emails in python Commented Dec 25, 2018 at 8:45
  • This is a duplicate question Commented Dec 25, 2018 at 8:46

1 Answer 1

1

Send an email using python is easy, you may have encountered mistakes in your snippet code above and I am lazy to debug your code. But here might be the code you prefer. The smtp.gmail.com,465 make your code secure. This snippet of code will send email to recipient through spam but lot number of emails you wish to send, will not be blocked by Gmail, you also can use bot to send email through recipient via this snippet of code.

Send Multiple Recipients
In the to line, just separate recipients by comma
to = [ '<recipient_email_address1>', '<recipient_email_address2>', '<recipient_email_address3>']

import smtplib def sendEmailFunc(): gmail_user = '<your_sender_email_address_here>' gmail_password = '<credential_password_of_above_email>' sent_from = gmail_user to = ['<recipient_email_address>'] subject = '<write_your_email_subject_here>' body = "<write_your_email_body_here>" email_text = """ From: %s To: %s Subject: %s %s """ % (sent_from, ", ".join(to), subject, body) try: server = smtplib.SMTP_SSL('smtp.gmail.com', 465) server.ehlo() server.login(gmail_user, gmail_password) server.sendmail(sent_from, to, email_text) server.close() print ('====Email Sent Successfully====') except: print ('Something went wrong...')

NOTE
Please be careful with your indentation. Hope this helpful for you. Thank you.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.