smtplib.SMTPAuthenticationError: (535, b'5.7.3 Authentication unsuccessful - python

\I'm getting an authentication error. I'm 100% certain my email and password are correct (types them in). I've attached a picture of my office.outlook.com settings for IMAP and POPenter image description here
import smtplib
# initialize connection to our email server, we will use Outlook here
smtp = smtplib.SMTP('smtp.office365.com', port='587')
smtp.ehlo() # send the extended hello to our server
smtp.starttls() # tell server we want to communicate with TLS encryption
smtp.login('support#name.com', 'emailpassword') # login to our email server
# send our email message 'msg' to our boss
smtp.sendmail('support#name.com',
'support#name.com',
msg.as_string())
smtp.quit() # finally, don't forget to close the connection

Related

Python - Connect to Gmail and send mail - Error 534 InvalidSecondFactor

i want to include sending notifications mails through gmail in my python code.
I followed all the steps to do it:
import smtplib
Enable less secure app
Wait for 1 day
Set 2-verification access
Create 16 digits app password
Change my gmail password with the 16 digits app password
Log into my gmail account
Unlock Display Captcha
Run the code within 10 minutes
I still got the same error:
(534, b'5.7.9 Application-specific password required. Learn more at\n5.7.9 https://support.google.com/mail/?p=InvalidSecondFactor h25sm8001063qkg.87 - gsmtp')
This is my code:
import smtplib, ssl
port = 465 # For SSL or 465
smtp_server = "smtp.gmail.com"
sender_email = "my_mail#gmail.com" # Enter your address
receiver_email = "my_mail#gmail.com" # Enter receiver address
password = '16digtisapppass' # i've checked I can log in to my gmail account with it
message = """\
Subject: Hi there
This message is sent from Python."""
try:
server = smtplib.SMTP_SSL('smtp.gmail.com', port)
server.ehlo()
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message)
except Exception as e:
print(e)
# or
context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message)
I've read these other posts:
https://stackabuse.com/how-to-send-emails-with-gmail-using-python/
http://stackoverflow.com/a/27515833/2684304
I'm not a professional coder, so maybe there is something simple that i'm missing.
I don't know what else could I do. Could anyone help me?
gmail port is 587
you are using AT&T port 465 just replace with 587port .
The problem is that the "2-Step Verification" is on your account.
Solutions:
Turn off 2-Step Verification and use your normal password account in your app (see this link for more details)
Sign in with App Passwords, An App Password is a 16-digit passcode that gives a less secure app or device permission to access your Google Account. App Passwords can only be used with accounts that have 2-Step Verification turned on (see this link or link2).

Send email ( python smtp ) to a specific domain (not .Gmail)

import smtplib
from smtplib import SMTP
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
msg = MIMEMultipart()
msg['From'] = 'example1#gmail.com'
#Sender
msg['To'] = 'example2#uc.cl'
#Receiver
msg['Subject'] = 'python'
message = ' Wena mimo, como estai mimo'
msg.attach(MIMEText(message))
mailserver = smtplib.SMTP('smtp.gmail.com',587)
# identify ourselves to smtp gmail client
mailserver.ehlo()
# secure our email with tls encryption
mailserver.starttls()
# re-identify ourselves as an encrypted connection
mailserver.ehlo()
mailserver.login('example1#gmail.com', 'password')
#login to the account
mailserver.sendmail('example1#gmail.com','example2#uc.cl',msg.as_string())
mailserver.quit()
#I make a code that sends emails to differents mail(example #gmail and
#corporative ) but i try to send mail to XXXXXXXX#uc.cl (https://www.uc.cl) but this mail dont receive the message
This is a code for Python3 in Ubuntu 18.04 I dont know if the problem is the port:587 or the domain uc.cl or the code, maybe the domain uc.cl takes a high level on this security
THis domain its from a University
I assume the school mail server might try to block the mail coming from your python server.
A few things you could try...
Sending the email while connected to the school wifi. Maybe they don't block internal traffic
Try sending to a email server you set up just to check your code is working right
Make a new email account yahoo, etc and login with the code above and try sending to the other accounts you can test with. This should give you a good idea what is occuring.

Relaying denied. IP name possibly forged [216.136.38.14]')} Python error

I am trying to send mail using SMTP server but I am getting error but the same code is working using gmail.so can you help me to fingd the solution.
from smtplib import SMTP
import datetime
debuglevel = 0
smtp = SMTP()
smtp.set_debuglevel(debuglevel)
smtp.connect('e3-smtp-gtm.zxp.com', 25)
from_addr = "from mail id"
to_addr = "to mail id"
subj = "hello"
date = datetime.datetime.now().strftime( "%d/%m/%Y %H:%M" )
message_text = "Hello\nThis is a mail from your server\n\nBye\n"
msg = "From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%s" % ( from_addr, to_addr, subj, date, message_text )
smtp.sendmail(from_addr, to_addr, msg)
smtp.quit()
When I run above code I am getting error as below
{'to mail id': (550, b'5.7.1 ...Relaying denied. IP name possibly forged [216.136.38.14]')}
Can anyone help me what is main reason for getting this error
An SMTP server is a protected resource without public access. Without the restrictions spammers would misuse it a lot.
While everybody may submit messages that will be delivered locally (incoming mail), only authorized users may submit messages to be forwarded to other servers on Internet (outgoing mail). Usually the authorization is based on IP address or user/password login.
The meaning of "Relaying denied" is that the SMTP server does not recognize you as an authorized user who can send email wherever he wants. You should provide login/password given to you by the server's admin. In most cases that requires STARTTLS prior to login.
Also, connecting to port 25 is discouraged. This port is intended for server to server mail transfer. The initial message submition should go to the TCP port 587.

AOL mail rejected using smtplib

I'm using smtplib to send email via AOL account, but after successful authentication it gets rejected with following error.
reply: '521 5.2.1 : AOL will not accept delivery of this message.\r\n'
reply: retcode (521); Msg: 5.2.1 : AOL will not accept delivery of this message.
data: (521, '5.2.1 : AOL will not accept delivery of this message.')
Here's explanation for this error.
The SMTP reply code 521 indicates an Internet mail host DOES NOT ACCEPT
incoming mail. If you are receiving this error it indicates a configuration
error on the part of the recipient organisation, i.e. inbound e-mail traffic
is being routed through a mail server which has been explicitly configured
(intentionally or not) to NOT ACCEPT incoming e-mail.
Recipient mail (in my script) is valid (gmail) address and after this debug message mail gets rejected.
send: 'Content-Type: text/plain; charset="us-ascii"\r\nMIME-Version: 1.0\r\nContent-Transfer-Encoding: 7bit\r\nSubject: My reports\r\nFrom: myAOLmail#aol.com\r\nTo: reportmail#gmail.com\r\n\r\nDo you have my reports?\r\n.\r\n'
Here's short version of code:
r_mail = MIMEText('Do you have my reports?')
r_mail['Subject'] = 'My reports'
r_mail['From'] = e_mail
r_mail['To'] = 'reportmail#gmail.com'
mail = smtplib.SMTP("smtp.aol.com", 587)
mail.set_debuglevel(True)
mail.ehlo()
mail.starttls()
mail.login(e_mail, password)
mail.sendmail(e_mail, ['reportmail#gmail.com'] , r_mail.as_string())
Is this some kind of permission problem because I'm successfully sending same email with Yahoo account without any problems?
I guess that AOL doesn't allow relay access by default or you have not configured it manually. The error you get says that aol doesn't have recipient you want to send message. In this case if you want to send email to gmail account try to connect to gmail SMPT server instead of AOL.
Change smpt server to gmail-smtp-in.l.google.com for example and turn off authentication.
Was running into 5.2.1 : AOL will not accept delivery of this message. from the AOL SMTP relay myself. What I ended up needing was valid From and To headers in the MIME message body, as opposed to just the SMTP connection.
In your particular case there could be any number of reasons for getting this 5.2.1 bounce. The postmaster.aol.com site has some helpful tools to diagnose, as well as some pretty vague documentation for this specific error message. In my case I ended up packet sniffing the SMTP messages that my Thunderbird email client was sending vs. the Python script, and eventually spotted the difference.
postmaster.aol.com documentation:
https://postmaster.aol.com/error-codes
AOL will not accept delivery of this message
This is a permanent bounce due to:
RFC2822 From domain does not match the rDNS of sending server.
RFC 2822 FROM address does not have an A record and is not a valid domain.
IP has a poor reputation and mail is sent to multiple recipients.
There are multiple From address in the mail headers and the IP reputation is poor.
My Python function for sending mail through smtp.aol.com:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def genEmail(user, passwd, to, subject, message):
smtp=smtplib.SMTP_SSL('smtp.aol.com',465)
smtp.login(user, passwd)
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = user # This has to exist, and can't be forged
msg['To'] = to
msg.attach(MIMEText(message, 'plain'))
smtp.sendmail(user, to, msg.as_string())
smtp.quit()

How to send an email to corporate email account using Python?

I have a corporate email account. Now I need to send out an email to a corporate email account within our company using Python. We use Microsoft Exchange within our company along with Outlook.
I tried using the below code but it doesn't work for me and I always get SOCKET error
#!/usr/bin/python
import smtplib
from smtplib import SMTPException
message = """From: david#company.com
To: ron#company.com
MIME-Version: 1.0
Content-type: text/html
Subject: SMTP HTML e-mail test
This is an e-mail message to be sent in HTML format
<b>This is HTML message.</b>
<h1>This is headline.</h1>
"""
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receivers, message)
print "Successfully sent email"
except SMTPException:
print "Error: unable to send email"
Below is the error I am getting -
socket.error: [Errno 111] Connection refused
Here what should I replace for localhost or should I be using SMTP for this or some other thing?
localhost has to be replaced with smtp mail server address. Every corporate environment should have a smtp host. Any IT admin should be able to point you to it. It looks something similar to 'smtphost.corpabc.com'

Categories

Resources