0

Hi I am trying to connect to the outlook server to sending the email with smtplib in python.

Trying this code smtplib.SMTP('smtp-mail.outlook.com') does not print out anything and also does not return an error.

Can somebody tell me what might be the problem and how to fix it.

Thank you a lots.

hsm59
  • 1,991
  • 19
  • 25
  • to be clear, the command to the outlook server did not suceeded. The python command prompt just stuck. I cannot type the next command or do anything except for closing the command prompt and open it again – Phuong Duyen Huynh Ngoc Jan 11 '18 at 12:03
  • Since this question pops up at the top of Google when you search for smtplib connecting issues I wanted to share my solution. I had the same issue, however if I set the connection timeout to e.g. 2 seconds it would "time out" but it actually didn't fail and the email was sent successfully. Turns out the VPN I was using was causing the issue and after disconnecting from the VPN it worked without setting a low timeout. – Decko Apr 26 '18 at 21:22
  • Please, select the correct answer or explain the solution if you found it. Thanks – juankysmith Jul 18 '18 at 08:18

3 Answers3

0

Try with this (make sure your outlook host_name is correct)

import smtplib
server = smtplib.SMTP(host_name, port_number)

server.login(username, password)

#Send the mail
email_content = 'Helloooooooooooooo!' 
server.sendmail(sender_address, target_address, email_content)

Where host_name, port_number, username, password, sender_address and target_address are variables you have to fill

See the docs for SMTP

juankysmith
  • 11,839
  • 5
  • 37
  • 62
  • Well the program just stop. it does not prompt me to type the next command. So i cannot do anything with it. – Phuong Duyen Huynh Ngoc Jan 11 '18 at 12:00
  • 1
    Can you show us the code before and after that line? – juankysmith Jan 11 '18 at 12:22
  • >>> import smtplib >>> smtpObj=smtplib.SMTP('smtp-mail.outlook.com') - ( so you can see instead of showing the new >>> for typing the next command, it just so this sign '-' and I cannot type or do anything with the command prompt, it just stuck there as long as you keep the command prompt open – Phuong Duyen Huynh Ngoc Jan 11 '18 at 20:51
0

Maybe the mail server required authentification (username, password).

See login-function: https://docs.python.org/2/library/smtplib.html#smtplib.SMTP.login

Taree
  • 61
  • 5
  • I cannot come to the authentication step because after I enter the command to connect to the smtp, the program just running forever. It does not allow me to write the next command also. – Phuong Duyen Huynh Ngoc Jan 11 '18 at 12:01
0

thank you for all your answer, the problem is actually caused because of some restriction in my work network. I have talked with them and the problem is solved.

  • Little bit late for a question about this but do you know what the network restriction was? I am in the same situation where when I'm using my work WI-FI I can't connect. But with my own mobile data it works perfectly. – Yorbjörn Jun 15 '21 at 08:05