Python email question

3274
1
08-05-2019 01:58 PM
MarcoPretorius1
New Contributor II

I am trying to send a email using the smtplib package and am getting the following error:

Script:

  1. import smtplib
  2. server_ssl = smtplib.SMTP_SSL('smtp.office365.com', xxx)
  3. server_ssl.ehlo() # optional, called by login()
  4. server_ssl.login('xxxx@xxx.local', 'xx')
  5. # ssl server doesn't support or need tls, so don't call server_ssl.starttls()
  6. server_ssl.sendmail("xxx@xxx.com", "xxx@xxx.com", "hello")
  7. #server_ssl.quit()
  8. server_ssl.close()
  9. print 'successfully sent the mail'

Error: 

Traceback (most recent call last):
File "Script5.py", line 3, in <module>
server_ssl = smtplib.SMTP_SSL('smtp.office365.com', xxx)
File "C:\Python27\ArcGIS10.7\lib\smtplib.py", line 802, in __init__
SMTP.__init__(self, host, port, local_hostname, timeout)
File "C:\Python27\ArcGIS10.7\lib\smtplib.py", line 259, in __init__
raise SMTPConnectError(code, msg)
smtplib.SMTPConnectError: (-1, 'The Microsoft Exchange IMAP4 service is ready. [QgBOADYAUABSADEAMQBDAEEAMAAw]')

Any help is appreciated. Thank you! #

Tags (2)
0 Kudos
1 Reply
by Anonymous User
Not applicable

Hi,

Refer the following sample code which might be helpful for you. 

#system Modules

import httplib

import smtplib

from email.MIMEText import MIMEText

# script

host = "xxxxx-xxxx.xxx"

fromAdd = "fromemailaddress_Admin"

toAdd = "Your email address"

msg = MIMEText(stringInput)

msg['Subject'] = "Subject"

msg['From'] = fromAdd

msg['To'] = toAdd

s = smtplib.SMTP()

s.connect(host)

s.sendmail(fromAdd, msg["To"].split(","), msg.as_string())

s.close()

0 Kudos