-1

am receiving mail when I run the code in jupyter, but i tried in my Linux box via python am not receiving any mail.

Here is the below code:

#! /usr/bin/python
import smtplib
import ssl

port = 587
smtp_server = "smtp-mail.outlook.com"
sender = "example@outlook.com"
recipient = "example@outlook.com"
sender_password = "Password"

message = """
Subject: This is a test message
Sent using Python."""

SSL_context = ssl.create_default_context()

with smtplib.SMTP(smtp_server, port) as server:
    server.starttls(context=SSL_context)
    server.login(sender, sender_password)
    server.sendmail(sender, recipient, message)
user4157124
  • 2,809
  • 13
  • 27
  • 42
  • 1
    In your `with smtplib` try to enable debug and see what the messages are: `server.set_debuglevel(1)` – Cow May 25 '22 at 12:08

1 Answers1

0

You are trying to connect Outlook with SMTP. You should require your email account allows smtp, which is not necessarily enabled by default.

Also, you can try other solutions based on libraries;

from O365 import Message

html_template =     """ 
            <html>
            <head>
                <title></title>
            </head>
            <body>
                    {}
            </body>
            </html>
        """

final_html_data = html_template.format(df.to_html(index=False))

o365_auth = ('sender_username@company_email.com','Password')
m = Message(auth=o365_auth)
m.setRecipients('receiver_username@company_email.com')
m.setSubject('Weekly report')
m.setBodyHTML(final)
m.sendMessage()

from https://stackoverflow.com/a/52534847/5942941

or install redmail:

pip install redmail

and try to be use this sample;

from redmail import outlook

outlook.user_name = "example@hotmail.com"
outlook.password = "<MY PASSWORD>"

outlook.send(
    receivers=["you@example.com"],
    subject="An example",
    text="Hi, this is an example."
)

https://stackoverflow.com/a/71056577/5942941

Sezer BOZKIR
  • 534
  • 2
  • 13