#🔒 Outlook & SMTP server (login to my account)

12 messages · Page 1 of 1 (latest)

quiet shuttle
#

For a personal project i need to send an email with python. All is working except the login. Why ?

karmic oliveBOT
#

@quiet shuttle

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

quiet shuttle
#
import smtplib
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email import encoders
import time


def send_mail(textinput) :
    # Paramètres du serveur SMTP et informations d'authentification
    serveur_smtp = 'smtp-mail.outlook.com'
    port_smtp = 587
    adresse_email_expeditrice = 'my outlook sender email adress'
    mot_de_passe = "it's password"

    # Créer un objet MIMEMultipart pour le message
    message = MIMEMultipart()
    message['From'] = adresse_email_expeditrice
    message['To'] = 'the receiver email adress'
    message['Subject'] = 'Test'

    # Corps du message
    corps_message = f"""Message : 
    {textinput}

"""
#
 message.attach(MIMEText(corps_message, 'plain'))

    # Pièce jointe
    nom_fichier_piece_jointes = ['textfile.txt', "textfile2.txt"]

    for nom_fichier_piece_jointe in nom_fichier_piece_jointes :

        piece_jointe = open(nom_fichier_piece_jointe, 'rb')

        part = MIMEBase('application', 'octet-stream')
        part.set_payload(piece_jointe.read())
        encoders.encode_base64(part)
        part.add_header('Content-Disposition', f'attachment; filename={nom_fichier_piece_jointe}')

        message.attach(part)

    time.sleep(2)

    # Établir une connexion avec le serveur SMTP
    connexion_smtp = smtplib.SMTP(serveur_smtp, port_smtp)

    time.sleep(2)

    status_code, response = connexion_smtp.ehlo()
    print(f"[*] Echoing the server:      {status_code}   |   {response}")

    time.sleep(2)

    status_code, response = connexion_smtp.starttls()
    print(f"[*] Starting TLS connection: {status_code}   |   {response}")

    time.sleep(2)
#
# Authentification
    status_code, response = connexion_smtp.login(adresse_email_expeditrice, mot_de_passe)
    print(f"[*] Logging in: {status_code}   |   {response}")
    
    # Vérifier si l'authentification a réussi
    if status_code == 235:
        print("Authentification réussie !")
    else:
        print("Échec de l'authentification.")

    time.sleep(2)


    # Envoyer le message
    returned = connexion_smtp.sendmail(adresse_email_expeditrice, 'the receiver email adress', message.as_string())
    print(returned)


    # Quitter la connexion SMTP
    connexion_smtp.quit()

    with open("textfile.txt", "w", encoding="utf-8") as file :
        file.write("")
    with open("textfile2.txt", "w", encoding="utf-8") as file :
        file.write("")

if __name__ == "__main__" :
        send_mail("Test Test Test")
delicate niche
#

Please include error you're getting

#

but I assume it will be connected to ssl/tls

#

!d smtplib.SMTP.starttls

karmic oliveBOT
#

SMTP.starttls(*, context=None)```
Put the SMTP connection in TLS (Transport Layer Security) mode. All SMTP commands that follow will be encrypted. You should then call [`ehlo()`](https://docs.python.org/3/library/smtplib.html#smtplib.SMTP.ehlo) again.

If *keyfile* and *certfile* are provided, they are used to create an [`ssl.SSLContext`](https://docs.python.org/3/library/ssl.html#ssl.SSLContext).

Optional *context* parameter is an [`ssl.SSLContext`](https://docs.python.org/3/library/ssl.html#ssl.SSLContext) object; This is an alternative to using a keyfile and a certfile and if specified both *keyfile* and *certfile* should be `None`.

If there has been no previous `EHLO` or `HELO` command this session, this method tries ESMTP `EHLO` first.

Changed in version 3.12: The deprecated *keyfile* and *certfile* parameters have been removed.
delicate niche
#

so:

  1. create SMTP object
  2. starttls
  3. login
  4. send
  5. quit

if there's 2fa on the account, you'd need to create a token to use instead
(if it was gmail instead, it would actually require you to first add 2fa, then create a token)

karmic oliveBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.