Unable to connect to gmail/Outlook using IMAP in python - python

I have tried to connect to the Gmail server using IMAP in Spyder(Python 3.6) using the Chilkat package.
I have enabled the IMAP for all Mail in the Settings>Forwarding and POP/IMAP and then I have also enabled the less secure apps tab here https://myaccount.google.com/lesssecureapps?pli=1 after signing in. But in this code
import sys
import chilkat
imap = chilkat.CkImap()
# Anything unlocks the component and begins a fully-functional 30-day trial.
success = imap.UnlockComponent("Anything for 30-day trial")
if (success != True):
print(imap.lastErrorText())
sys.exit()
# Connect to an IMAP server.
# Use TLS
imap.put_Ssl(True)
imap.put_Port(993)
success = imap.Connect("imap.gmail.com")
The success variable which is a boolean remains False. Please help me. My aim is to fetch all attachments from Outlook Server and dump them into a file.But I cannot even connect to the Gmail server. I tried to use "imap.mail.Outlook.com" but that also failed. I do not know the steps to enable IMAP in Outlook. But even if it is enabled in Gmail, why is it not working?

The 1st step is to examine the contents of the imap.LastErrorText property to see what happened. For example:
# Connect to an IMAP server.
# Use TLS
imap.put_Ssl(True)
imap.put_Port(993)
success = imap.Connect("imap.someMailServer.com")
if (success != True):
print(imap.lastErrorText())
sys.exit()
My guess is that a firewall (software or hardware) is blocking the outbound connection.
An alternative solution is to use the GMail REST API as shown in these examples: https://www.example-code.com/python/gmail.asp The HTTP ports (443) are unlikely to be blocked by a firewall. You would download into a Chilkat Email object and then save attachments in exactly the same way had you downloaded via IMAP.

Related

Is POP3 thread safe?

I use poplib to retrieve new messages from my gmail.
I do it in the following way:
server = poplib.POP3_SSL('pop.gmail.com')
# ... login with credentials
#get info about new mails (1)
emails, total_bytes = server.stat()
for i in range(emails):
# get new mail (2)
response = server.retr(i+1)
I was wondering what may happen if between (1) and (2) gmail mailbox receive new emails?
I tried by myself and (2) works only with messages that were actual at the moment of retrieving information about them (1) and no new messages were fetched from the server.
Is this guaranteed by POP3 protocol or not?
Yes, POP3 locks the mailbox.
From RFC 1939:
Once the POP3 server has determined ... that the client should be given access to the
appropriate maildrop, the POP3 server then acquires an exclusive-
access lock on the maildrop, as necessary to prevent messages from
being modified or removed before the session enters the UPDATE state.

Python smtplib hangs when sendmessage() is called

I'm trying to send a message from a gmail account using smtplib. I've been messing with it in the shell, and everything works fine until I call sendmail(), when it just freezes until I close the terminal. No errors, just freezing. My commands look like this:
>>> import smtplib
>>> connection = smtplib.SMTP("smtp.gmail.com", 587)
>>> connection.ehlo()
>>> connection.starttls()
>>> connection.login("gmail_id", "gmail_pwd")
>>> connection.sendmail("addr_from", "addr_to", "message")
Has anybody had this problem? I've tried pinging smtp.gmail.com and using telnet smtp.gmail.com 587, and they both connect.
Use connection.set_debuglevel(1) to enable tracking/debugging SMTP session.
https://docs.python.org/2/library/smtplib.html#smtplib.SMTP.set_debuglevel
I had a similar issue just now, I had tried to login to my gmail account using python3.7's smtplib and was getting:
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')
I went to my gmail inbox using a browser, saw that I received a security alert email with the message
Less secure app blocked: Google blocked the app you were trying to use
because it doesn't meet our security standards.Some apps and devices
use less secure sign-in technology, which makes your account more
vulnerable. You can turn off access for these apps, which we
recommend, or turn on access if you want to use them despite the
risks. Google will automatically turn this setting OFF if it's not
being used.
After I followed the instructions in the aforementioned email and enabled less secure apps access on my gmail account, I tried sendmail still using the initial SMTP connection, but it just hung with no output.
I then proceeded to quit the initial SMTP connection and created a new one (post less secure apps enabled) like below and it worked!
conn = smtplib.SMTP('smtp.gmail.com', 587)

RetriesExhaustedError on connecting to HPE iLO 5 through Python iLO REST client

Following is a Python based RESTful library client (recommended by HP https://developer.hpe.com/platform/ilo-restful-api/home) that uses Redfish REST API (https://github.com/HewlettPackard/python-ilorest-library) to connect to the remote HPE iLO5 server of ProLiant DL360 Gen10 based hardware
#! /usr/bin/python
import redfish
iLO_host = "https://xx.xx.xx.xx"
username = "admin"
password = "xxxxxx"
# Create a REST object
REST_OBJ = redfish.redfish_client(base_url=iLO_host,username=username, password=password, default_prefix='/redfish/v1')
# Login into the server and create a session
REST_OBJ.login(auth="session")
# HTTP GET request
response = REST_OBJ.get("/redfish/v1/systems/1", None)
print response
REST_OBJ.logout()
I am getting RetriesExhaustedError when creating REST object. However, I can successfully do SSH to the server from the VM (RHEL7.4) where I am running this script. The authentication details are given correctly. I verified that the Web Server is enabled (both port 443 and 80) in the iLO Security - Access settings. Also, in my VM box the Firewalld service has been stopped and IPTables is flushed. But still connection could not be established. What other possibilities I can try yet?
I found the root cause. The issue is with SSL Certificate verification being done by the Python code.
This can be turned off by setting the environment variable PYTHONHTTPSVERIFY=0 before running the code solved the problem.
This is a very old topic, but perhaps for other people that have a similar issue when accessing the iLO in any way, and not just over Python:
You most likely need to update the firmware in your server, so that the TLS is updated. You will most likely need to use an old browser to do this, as modern versions of Mozilla/Chrome will not work with old TLS. I have had luck with Konqueror.

Send email from python webapp2 Google App Engine

i am trying to send an email from my application. The code run successfully with out error. But it do not send the email. It show this message on Console.
You are not currently sending out real email.
If you have sendmail installed you can use it by using the
server with --enable_sendmail
The sample code provided by the google is:
message = mail.EmailMessage(
sender="shaizi9687#gmail.com",
subject="Your account has been approved")
message.to = "ABC <shahzebakram#hotmail.com>"
message.body = """Dear Albert:
Your example.com account has been approved. You can now visit
http://www.example.com/ and sign in using your Google Account to
access new features.
Please let us know if you have any questions.
The example.com Team
"""
message.send()
That's because the dev_appserver by default doesn't send out real mails.
Email sending will work when you push to a live server, for eg:
appcfg update /path/to/app/
But like the error message points out, you'll have to use the --enable_sendmail flag if you have sendmail installed on your system or use smtp flags, for eg:
dev_appserver /path/to/app/ --enable_sendmail=yes
or another eg using gmail as smtp provider
dev_appserver /path/to/app --smtp_host=smtp.gmail.com --smtp_port=465 \
--smtp_user=user#gmail.com --smtp_password=password
More explanation here: https://cloud.google.com/appengine/docs/python/mail/
Mail and the development server
The development server can be configured to send email messages
directly from your computer when you test a feature of your app that
sends messages. You can configure the development server to use an
SMTP server of your choice. Alternatively, you can tell the
development server to use Sendmail, if Sendmail is installed on your
computer and set up for sending email.
If you do not configure an SMTP server or enable Sendmail, when your
app calls the Mail service, the development server will log the
contents of the message. The message will not actually be sent.

Send mail with Chilkat Mailman using authenticated HTTP proxy

I am trying to send emails using the chilkat mailman api in python, through an authenticated http proxy. I have followed the instructions from the Chilkat docs to the best of my ability but am having issues with the proxy server. I have verified that this proxy works given the specified port and auth using phantomjs scripts.
import chilkat
# The mailman object is used for sending and receiving email.
mailman = chilkat.CkMailMan()
# set the http proxy
mailman.put_HttpProxyAuthMethod("LOGIN")
mailman.put_HttpProxyHostname("xxx.xxx.xxx.xxx")
mailman.put_HttpProxyPort(xxxxx)
mailman.put_HttpProxyUsername("xxxxx")
mailman.put_HttpProxyPassword("xxxxx")
# Set the SMTP server.
mailman.put_SmtpHost("smtp.live.com")
mailman.put_StartTLS(True)
mailman.put_SmtpPort(25)
# Set the SMTP login/password (if required)
mailman.put_SmtpUsername("xxxxxxx")
mailman.put_SmtpPassword("xxxxxxx")
# Create a new email object
email = chilkat.CkEmail()
email.put_Subject("This is a test")
email.put_Body("This is a test")
email.put_From("name#email.com")
email.AddTo("Chris Johnson","name#email.com")
# Call SendEmail to connect to the SMTP server via the HTTP proxy and send.
success = mailman.SendEmail(email)
if (success != True):
print(mailman.lastErrorText())
sys.exit()
If I take out the section that sets the proxy, the mail is successfully sent. Is there some other attribute I am missing?
Though Chillkat's mailman has support for HTTP proxies, it seems that it doesn't support pure HTTP proxies. Mainly, the proxy that you use must also support other protocols because SMTP doesn't broadcast over HTTP. As my proxy only supported HTTP protocol, it wouldn't work for me. I swapped to use a SOCKS proxy and now everything is working fine.

Categories

Resources