Python Forum

Full Version: Sending Out Email via Python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello all

I was hoping someone could help me with the following issue.

The code below attempts to send an email from my company's email account.

We use outlook, but this code does not work.

The code is as follows:-

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
            
mail=smtplib.SMTP('smtp-mail.outlook.com', 587) 
mail.ehlo() 
mail.starttls()  
mail.login("username","pwd") 
                
From_Address = ["from email"]
To_Address = ["to email"]
CC_Address = ["cc email"]
Subject_Email = "Email Subject"
            

Body = "Email Body"
    
html = """\
            <html>
              <head> 
              </head>
              <body>
                      Hi {0}
                      <br>
                  <br>
                      Message Message
                  <br> 
                  <br>
                      {1}
                  <br>
                      Message Message Message Message <br><br>
              <br>
              <br>
              <br
              ><br>
              <br>
              </body>
              <br>
    
            </html>
""".format(Body.to_html())

msg = MIMEMultipart()
msg['From'] = ', '.join(From_Address)
msg['To'] = ', '.join(To_Address)
msg['Cc'] = ', '.join(CC_Address)
msg['Subject'] = Subject_Email
                
message = MIMEText(html,'html')
msg.attach(message)
mail.sendmail(From_Address, (To_Address + CC_Address), msg.as_string()) 
mail.quit() 
The error messages i get include:-

SMTPAuthenticationError: (550, b'5.2.1 Mailbox cannot be accessed [LO3P265CA0017.GBRP265.PROD.OUTLOOK.COM]')

SMTPAuthenticationError: (535, b'5.7.3 Authentication unsuccessful [LNXP265CA0009.GBRP265.PROD.OUTLOOK.COM]')
When i change the SMTP settings for gmail it works fine - i have no idea how to fix this.

I run this code from my company laptop so there shouldn't be any security issues.

I was wondering if anyone could point me in the right direction.

(BTW i have assumed the port number to be 578, if there is anyway of confirming this then please let me know)

Thank you.
Looking here: https://support.microsoft.com/en-us/offi...414e2aa040

It looks like the domain might be SMTP server name smtp.office365.com.