Reputation: 125
I am trying to connect to Outlook using POP3 in python.
mailbox = poplib.POP3_SSL('outlook.office365.com', 995)
And I am getting the following error:
[WinError 10061] No connection could be made because the target machine actively refused it
I am using a VPN, and can ping 'outlook.office365.com' without issue. I also tried 'pop-mail.outlook.com' and 'pop3.live.com' as I saw them mentioned as hostnames online, and received the same error. Please let me know if there is any more useful information I can provide.
Upvotes: 1
Views: 2253
Reputation: 101
for the office 365 email settings it seems that the port for POP is 903. You can try using the settings in the web under Microsoft365 to also try fetching your email with IMAP.
Just to save you same time I wrote down some working lines using imap for fetching your emails and a simple code for sending using smtp
import smtplib
import imaplib
import ssl
def send_email():
send_port = 587
with smtplib.SMTP('smtp.office365.com', send_port) as smtp:
#Need a ehlo message before starttls
smtp.ehlo()
smtp.starttls()
# Server login
smtp.login(<user-email>, <password>)
# Email data
sender_email = <user-email>
receiver_email = <destination-email>
email_subject = <some-subjetct>
email_body = <some-body>
message = 'Subject: {}\n\n{}'.format(email_subject, email_body)
smtp.sendmail(sender_email, receiver_email, message)
smtp.quit()
def receive_email():
receive_port = 993
with imaplib.IMAP4_SSL('outlook.office365.com', receive_port) as imap:
# Server login
imap.login(<youremail>, <password>)
# Get the list of folders in you email account and some extra info.
retcode, folders = imap.list()
# status of the fetch
print ('Response code:', retcode)
# folders and info of hierarchies
print ('Response code:', folders)
imap.close()
#Test
send_email()
receive_email()
Be aware that this code needs the imap and smtp port open on your firewall. Also use same user-email and password as you were using https://www.office.com/
Upvotes: 2