How to connect to my Microsoft mail using python to retrieve mail? - python

I'm unable to connect to my email.
Some information about Microsoft IMAP sever name, host, encryption I got from this link https://support.microsoft.com/en-us/office/pop-imap-and-smtp-settings-for-outlook-com-d088b986-291d-42b8-9564-9c414e2aa040
Is there something wrong with my code?
This is my code
import imaplib
import pprint
imap_host = 'outlook.office365.com:993'
imap_user = 'myemail#hotmail.com'
imap_pass = 'password'
imap = imaplib.IMAP4(imap_host) # code dosen't execute past this line. I guess it has something to do with SSL or TSL
## login to server
imap.login(imap_user, imap_pass)
imap.select('Inbox')
tmp, data = imap.search(None, 'ALL')
for num in data[0].split():
tmp, data = imap.fetch(num, '(RFC822)')
print('Message: {0}\n'.format(num))
pprint.pprint(data[0][1])
break
imap.close()
This is the error message I'm getting
File "C:\Users\User\Documents\Python project\email-server\getemail.py", line 9, in <module>
imap = imaplib.IMAP4(imap_host)
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\imaplib.py", line 202, in __init__
self.open(host, port, timeout)
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\imaplib.py", line 312, in open
self.sock = self._create_socket(timeout)
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\imaplib.py", line 302, in _create_socket
return socket.create_connection(address)
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\socket.py", line 824, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\socket.py", line 955, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

Related

I keep recieving an error when I try to send from 1 email address to another in python

**This Is My Code:
import os
from email.message import EmailMessage
import ssl
import smtplib
request = input('Enter A Song Request, Including Author, And Title: ')
email_sender = 'linuschatbot#gmail.com'
email_password = '****************'
email_reciever = 'judahstarkenburg#gmail.com'
subject = 'New Song Request!'
body = ("Someone Has Requested For The Song: " + request)
em = EmailMessage()
em['From'] = email_sender
em['To'] = email_reciever
em['Subject'] = subject
em.set_content(body)
thecontext = ssl.create_default_context()
with smtplib.SMTP_SSL('stmp.gmail.com', 465, context=thecontext) as smtp:
smtp.login(email_sender, email_password)
smtp.sendmail(email_sender, email_reciever, em.as_string())
print("Song Request Made!")
I keep recieving this error!:
File "c:\Users\judah\OneDrive\Desktop\Python\Python\Linus\test.py", line 25, in
with smtplib.SMTP_SSL('stmp.gmail.com', 465, context=thecontext) as smtp:
File "C:\Users\judah\AppData\Local\Programs\Python\Python310\lib\smtplib.py", line 1050, in init
SMTP.init(self, host, port, local_hostname, timeout,
File "C:\Users\judah\AppData\Local\Programs\Python\Python310\lib\smtplib.py", line 255, in init
(code, msg) = self.connect(host, port)
File "C:\Users\judah\AppData\Local\Programs\Python\Python310\lib\smtplib.py", line 341, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Users\judah\AppData\Local\Programs\Python\Python310\lib\smtplib.py", line 1056, in _get_socket
new_socket = super()._get_socket(host, port, timeout)
File "C:\Users\judah\AppData\Local\Programs\Python\Python310\lib\smtplib.py", line 312, in _get_socket
return socket.create_connection((host, port), timeout,
File "C:\Users\judah\AppData\Local\Programs\Python\Python310\lib\socket.py", line 824, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Users\judah\AppData\Local\Programs\Python\Python310\lib\socket.py", line 955, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
Can someone please help? I know I got the information right! I just don't know what I did wrong! I have tried multiple ways to send an email from one of my accounts to another, if someone can make this for me than that would be awesome. I just want code that will send a song request from my email_sender to email_reciever, ill put the password in. (I used 2 Step verification method).
I am so desperate for this to work, I even asked chat GPT and it would not give me an answer that works.**

How to get data from The Things Network through python paho mqtt library

I am getting an error when i try to use the attached python code to connect and send messages to the Things Network application. When i try using the mqtt mosquitto broker in the terminal with
Traceback (most recent call last):
File "/Users/ayushsharma/Desktop/ttn.py", line 28, in <module>
client.connect(broker,port = 8883)
File "/Users/ayushsharma/Desktop/ven/lib/python3.10/site-packages/paho/mqtt/client.py", line 914, in connect
return self.reconnect()
File "/Users/ayushsharma/Desktop/ven/lib/python3.10/site-packages/paho/mqtt/client.py", line 1044, in reconnect
sock = self._create_socket_connection()
File "/Users/ayushsharma/Desktop/ven/lib/python3.10/site-packages/paho/mqtt/client.py", line 3685, in _create_socket_connection
return socket.create_connection(addr, timeout=self._connect_timeout, source_address=source)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/socket.py", line 824, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/socket.py", line 955, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
it works in connecting but not through the python script. I'm really new to all this so i appreciate any feedback or guidance on how to approach this problem of retrieving date from TheThingsNetwork.
#!/usr/bin/env python3
import paho.mqtt.client as paho
#settings
app_id = "appID"
access_key = "access_key"
broker="nam1.cloud.thethings.network "
port=1883
def on_publish(client,userdata,result):
print("data published \n")
print(client,userdata,result)
pass
def on_log(mqttc, obj, level, string):
print(string)
#setup
client = paho.Client()
client.username_pw_set(app_id,access_key)
#callbacks
client.on_publish = on_publish
client.on_log = on_log
#establish connection
client.connect(broker,port = 8883)
client.loop_start()
#publish
ret= client.publish("appID","on",qos=1)
ret.wait_for_publish()
[1]: https://i.stack.imgur.com/2oejU.png

python unable to connect to mqtt broker

When I was trying out a tutorial in the internet, I failed to connect to the mqtt broker - anyone can help me on that?
import paho.mqtt.client as mqtt
broker_url = "mqtt.eclipse.org"
broker_port = 1883
client = mqtt.Client()
client.connect(broker_url, broker_port)
print(client)
Traceback (most recent call last):
File "C:\Workspace\FI Systemintegration\Python\MitarbeiterVerwaltung\rpi\connect.py", line 6, in <module>
client.connect(broker_url, broker_port)
File "C:\Users\TorbenIT\AppData\Local\Programs\Python\Python310\lib\site-packages \paho\mqtt\client.py", line 914, in connect
return self.reconnect()
File "C:\Users\TorbenIT\AppData\Local\Programs\Python\Python310\lib\site-packages \paho\mqtt\client.py", line 1044, in reconnect
sock = self._create_socket_connection()
File "C:\Users\TorbenIT\AppData\Local\Programs\Python\Python310\lib\site-packages \paho\mqtt\client.py", line 3685, in _create_socket_connection
return socket.create_connection(addr, timeout=self._connect_timeout, source_address=source)
File "C:\Users\TorbenIT\AppData\Local\Programs\Python\Python310\lib\socket.py", line 824, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Users\TorbenIT\AppData\Local\Programs\Python\Python310\lib\socket.py", line 955, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
The service does not exist at mqtt.eclipse.org. It was either used only as an example or the service is no longer available.
You should use another MQTT instance, maybe run a local instance instead.

Send email using python

I want to send emails from my Gmail account using python. I followed steps given in this stackoverflow post: How to send an email with Python?
But, my the mails that I sent do not reach the addresses.
This is the error that I get:
Traceback (most recent call last):
File "something.py", line 24, in <module>
server = smtplib.SMTP('myserver')
File "/anaconda2/lib/python2.7/smtplib.py", line 256, in __init__
(code, msg) = self.connect(host, port)
File "/anaconda2/lib/python2.7/smtplib.py", line 317, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/anaconda2/lib/python2.7/smtplib.py", line 292, in _get_socket
return socket.create_connection((host, port), timeout)
File "/anaconda2/lib/python2.7/socket.py", line 557, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
What should I be doing here?
What you've get is a DNS query error indicating that domain myserver does not exist.
You have to replace the argument myserver in server = smtplib.SMTP('myserver') with the actual address of SMTP server, such as smtp.mail.yahoo.com.
This is how I do it.
import smtplib
server=smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login('your_email#gmail.com','your_password')
server.sendmail('your_email#gmail.com','your_email#gmail.com','test email')

error sending mail in python

pwd = "mypassword"
import smtplib
server = smtplib.SMTP('smtp.gmail.com')
server.ehlo()
server.starttls()
server.login("gvpcse113#gmail.com",pwd)
msg = "YOUR MESSAGE!"
server.sendmail("gvpcse113#gmail.com", "sender#xyz.com", msg)
server.quit()
I have tried sending a mail through python...
Error :
Traceback (most recent call last):
File "H:/my projects/PythonCourse/test_cont/mail_test4.py", line 4, in <module>
server = smtplib.SMTP('smtp.gmail.com')
File "C:\Python27\lib\smtplib.py", line 256, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python27\lib\smtplib.py", line 316, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Python27\lib\smtplib.py", line 291, in _get_socket
return socket.create_connection((host, port), timeout)
File "C:\Python27\lib\socket.py", line 557, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 11001] getaddrinfo failed
I am connecting through a proxy connection
I set the proxy through cmd in windows.
Please help me with this.
Update :
I am sure with the internet connection :
import urllib2
def internet_on():
try:
response=urllib2.urlopen('https://www.google.co.in',timeout=1)
return True
except urllib2.URLError as err: pass
return False
print internet_on()
Output is True
Your code works fine for me, so it's probably the connection settings.
Try changing server to:
server = smtplib.SMTP('64.233.184.108')
(that's the IP address of smtp.gmail.com, to bypass DNS resolution)
Try updating the server = smtplib.SMTP('smtp.gmail.com') to include a port. The port for gmail is 587, so add that as a second parameter: server = smtplib.SMTP('smtp.gmail.com', 587)
If this does not work, make sure that you have used this site to make sure that the account will let you in:
https://www.google.com/settings/security/lesssecureapps

Categories

Resources