Python Forum
Sending Out Email via Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sending Out Email via Python
#1
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.
Reply
#2
Looking here: https://support.microsoft.com/en-us/offi...414e2aa040

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sending a text from Python sawtooth500 2 175 Apr-14-2024, 01:56 PM
Last Post: sawtooth500
  Sending string commands from Python to a bluetooth device Rovelin 13 9,482 Aug-31-2021, 06:40 PM
Last Post: deanhystad
  How can I get Python Bulk Email Verification Script With API? zainalee 1 2,492 Jun-06-2021, 09:19 AM
Last Post: snippsat
  Need Outlook send email code using python srikanthpython 3 8,246 Feb-28-2021, 01:53 PM
Last Post: asyswow64
Video Python Bulk Email Verification Script With API Aj1128 0 2,623 Nov-28-2020, 11:38 AM
Last Post: Aj1128
  Including a Variable In the HTML Tags When Sending An Email JoeDainton123 0 1,885 Aug-08-2020, 03:11 AM
Last Post: JoeDainton123
  How to send email using python? Gigux 2 2,857 Jul-04-2020, 07:53 AM
Last Post: Gigux
  I am trying to send an email with python nick235 9 5,310 Jun-29-2020, 06:40 PM
Last Post: nick235
  Python sockets : Client sending to the server nico31780 0 2,340 May-17-2020, 07:56 PM
Last Post: nico31780
  Sending an email with attachment without using SMTP? PythonNPC 5 3,182 May-05-2020, 07:58 AM
Last Post: PythonNPC

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020