<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Outlook email Error in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/outlook-email-error/m-p/1104569#M62581</link>
    <description>&lt;P&gt;Not sure exactly what that error might be referring to. Just for another thing to try, here's the email function I built based on &lt;A href="https://docs.python.org/3/library/email.examples.html#email-examples" target="_self"&gt;examples in the Python docs&lt;/A&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def sendEmail(subject="", body_content="", to=[], cc=[], bcc=[], from_addr="donotreply@mydomain.suffix"):
    """Send a plain text email.

    Required imports:
        from email.message import EmailMessage # for a text/plain email message
        import smtplib # for sending email

    :param str subject: Subject line of email.
    :param str body_content: Main message in the body of the email.
    :param list to: A list of email addresses as strings for sending the email to.
    :param list cc: A list of email addresses as strings for CC'ing the email.
    :param list bcc: A list of email addresses as strings for BCC'ing the email.
    :param str from_addr: The email address from which to send the email.
    :return: All email addresses that received the email (to, cc, bcc).
    :rtype: Dictionary
    """
    from email.message import EmailMessage
    import smtplib
    # Validate email recipient args
    for recipient in [to, cc, bcc]:
        if not isinstance(recipient, list):
            raise TypeError(f"Recipients (to, cc, bcc) must each be a list of strings; not {type(recipient)}")
    # Create email message object and content.
    msg = EmailMessage()
    msg.set_content(body_content)
    # Set recipients
    msg["Subject"] = subject
    msg["From"] = from_addr
    msg["To"] = ", ".join(to)
    msg["Cc"] = ", ".join(cc)
    msg["Bcc"] = ", ".join(bcc)
    # Send the message via our own SMTP server.
    with smtplib.SMTP("mymailserver.mydomain.suffix") as smtp:
        smtp.send_message(msg)
    print("sendEmail() successful")
    return {"to": to, "cc": cc, "bcc": bcc}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Like&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/167692"&gt;@DavidPike&lt;/a&gt;&amp;nbsp;mentioned, there's likely something your email server isn't happy with and the GeoNet community probably can't help much with that.&lt;/P&gt;</description>
    <pubDate>Mon, 04 Oct 2021 20:39:43 GMT</pubDate>
    <dc:creator>BlakeTerhune</dc:creator>
    <dc:date>2021-10-04T20:39:43Z</dc:date>
    <item>
      <title>Outlook email Error</title>
      <link>https://community.esri.com/t5/python-questions/outlook-email-error/m-p/1104542#M62579</link>
      <description>&lt;P&gt;Error:&lt;/P&gt;&lt;P&gt;Traceback (most recent call last): File "C:\Python27\ArcGIS10.6\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript exec codeObject in __main__.__dict__ File "C:\Users\jstout\Desktop\PY\ESRI1.py", line 37, in &amp;lt;module&amp;gt; server.sendmail(FROM, TO, MESSAGE) File "C:\Python27\ArcGIS10.6\lib\smtplib.py", line 742, in sendmail (code, resp) = self.rcpt(each, rcpt_options) File "C:\Python27\ArcGIS10.6\lib\smtplib.py", line 490, in rcpt return self.getreply() File "C:\Python27\ArcGIS10.6\lib\smtplib.py", line 369, in getreply raise SMTPServerDisconnected("Connection unexpectedly closed") SMTPServerDisconnected: Connection unexpectedly closed&lt;/P&gt;&lt;P&gt;My Code&lt;/P&gt;&lt;P&gt;import arcpy, arceditor, time, smtplib&lt;/P&gt;&lt;P&gt;# Set the workspace&lt;BR /&gt;arcpy.env.workspace = r'C:\Users\jstout\AppData\Roaming\ESRI\Desktop10.6\ArcCatalog\EED_FM@SDE.sde'&lt;/P&gt;&lt;P&gt;# Set a variable for the workspace&lt;BR /&gt;adminConn = arcpy.env.workspace&lt;/P&gt;&lt;P&gt;# Get a list of connected users.&lt;BR /&gt;userList = arcpy.ListUsers(adminConn)&lt;BR /&gt;print "User List Finished"&lt;/P&gt;&lt;P&gt;# Get a list of user names of users currently connected and make email addresses&lt;BR /&gt;emailList = [user.Name + "@cityofelizabethton.org" for user in arcpy.ListUsers(adminConn)]&lt;BR /&gt;print "Email List Finished"&lt;/P&gt;&lt;P&gt;# Take the email list and use it to send an email to connected users.&lt;/P&gt;&lt;P&gt;SERVER = "cityofelizabethton-org.mail.protection.outlook.com"&lt;BR /&gt;FROM = "SDE Admin &amp;lt;jstout@cityofelizabethton.org&amp;gt;"&lt;BR /&gt;TO = emailList&lt;BR /&gt;SUBJECT = "Maintenance is about to be performed"&lt;BR /&gt;MSG = "Auto generated Message.\n\rServer maintenance will be performed in 15 minutes. Please log off."&lt;/P&gt;&lt;P&gt;# Prepare actual message&lt;BR /&gt;MESSAGE = """\&lt;BR /&gt;From: %s&lt;BR /&gt;To: %s&lt;BR /&gt;Subject: %s&lt;/P&gt;&lt;P&gt;%s&lt;BR /&gt;""" % (FROM, ", ".join(TO), SUBJECT, MSG)&lt;BR /&gt;print(MESSAGE)&lt;/P&gt;&lt;P&gt;# Send the mail&lt;BR /&gt;server = smtplib.SMTP(SERVER)&lt;BR /&gt;server.sendmail(FROM, TO, MESSAGE)&lt;BR /&gt;server.quit()&lt;BR /&gt;print "Email sent"&lt;/P&gt;&lt;P&gt;# Block new connections to the database.&lt;BR /&gt;# print("The database is no longer accepting connections")&lt;BR /&gt;# arcpy.AcceptConnections(adminConn, False)&lt;/P&gt;&lt;P&gt;# Disconnect all users from the database.&lt;BR /&gt;# print("Disconnecting all users")&lt;BR /&gt;# arcpy.DisconnectUser(adminConn, "ALL")&lt;/P&gt;&lt;P&gt;# This portion is to call a C# executable to call orphaned version cleanup.&lt;BR /&gt;print "Start EED Automated Cleanup"&lt;BR /&gt;import os&lt;BR /&gt;os.startfile(r"C:\Users\jstout\Desktop\EED_Automated_Cleanup\bin\x86\Debug\AutomatedCleanup.exe")&lt;BR /&gt;import sys, string, os, arcgisscripting&lt;BR /&gt;import subprocess&lt;BR /&gt;subprocess.Popen([r"C:\Users\jstout\Desktop\EED_Automated_Cleanup\bin\x86\Debug\AutomatedCleanup.exe"])&lt;BR /&gt;print "Automated Cleanup finished"&lt;/P&gt;&lt;P&gt;# Wait .5 minutes&lt;BR /&gt;time.sleep(30)&lt;/P&gt;&lt;P&gt;database = "Database Connections/toolboxDEFAULTVersion.sde"&lt;BR /&gt;versions = arcpy.ListVersions(adminConn)&lt;/P&gt;&lt;P&gt;# Print the versions available to the user&lt;BR /&gt;for version in versions:&lt;BR /&gt;print(version)&lt;BR /&gt;print "Version list complete."&lt;/P&gt;&lt;P&gt;# Execute the ReconcileVersions tool.&lt;BR /&gt;# Use a list comprehension to get a list of version names where the owner&lt;BR /&gt;# is the current user and make sure sde.default is not selected.&lt;BR /&gt;print 'Reconcile versions started.'&lt;/P&gt;&lt;P&gt;verList = [ver.name for ver in arcpy.da.ListVersions() if ver.isOwner&lt;BR /&gt;== True and ver.name.lower() != 'sde.EFAULT']&lt;/P&gt;&lt;P&gt;arcpy.ReconcileVersions_management(adminConn, "ALL_VERSIONS", "SDE.DEFAULT", "", "LOCK_ACQUIRED", "NO_ABORT", "BY_OBJECT", "FAVOR_TARGET_VERSION", "POST","DELETE_VERSION")&lt;/P&gt;&lt;P&gt;# Write messages to a Text File&lt;BR /&gt;txtFile = open("c:\\temp\\GPMessages.txt","w")&lt;BR /&gt;txtFile.write (arcpy.GetMessages())&lt;BR /&gt;txtFile.close()&lt;/P&gt;&lt;P&gt;print ('see error log at "c:\temp\GPMessages.txt"')&lt;BR /&gt;print'Reconciling Complete'&lt;/P&gt;&lt;P&gt;# Run the compress tool.&lt;BR /&gt;print("Running compress")&lt;BR /&gt;arcpy.Compress_management(adminConn)&lt;/P&gt;&lt;P&gt;# Use a connection file to create the connection&lt;BR /&gt;print "Check state ID"&lt;BR /&gt;egdb = r'C:\Users\jstout\AppData\Roaming\ESRI\Desktop10.6\ArcCatalog\EED_FM@SDE.sde'&lt;BR /&gt;egdb_conn = arcpy.ArcSDESQLExecute(egdb)&lt;/P&gt;&lt;P&gt;table_name = 'EED_FM.sde.sde_States'&lt;BR /&gt;field_name = 'state_id'&lt;/P&gt;&lt;P&gt;sql = '''&lt;BR /&gt;SELECT {0}, COUNT({0}) AS f_count FROM {1}&lt;BR /&gt;GROUP BY {0}&lt;BR /&gt;ORDER BY f_count DESC&lt;BR /&gt;'''.format(field_name, table_name)&lt;/P&gt;&lt;P&gt;egdb_return = egdb_conn.execute(sql)&lt;BR /&gt;for i in egdb_return:&lt;BR /&gt;print('{}: {}'.format(*i))&lt;BR /&gt;print "State count complete"&lt;/P&gt;&lt;P&gt;# Allow the database to begin accepting connections again&lt;BR /&gt;# arcpy.AcceptConnections(adminConn, True)&lt;BR /&gt;# print "allow connections to database"&lt;/P&gt;&lt;P&gt;# Update statistics and indexes for the system tables&lt;BR /&gt;# Note: to use the "SYSTEM" option the user must be an geodatabase or database administrator.&lt;BR /&gt;# Rebuild indexes on the system tables&lt;BR /&gt;print("Rebuilding indexes on the system tables")&lt;BR /&gt;arcpy.RebuildIndexes_management(adminConn, "SYSTEM")&lt;/P&gt;&lt;P&gt;# Update statistics on the system tables&lt;BR /&gt;print("Updating statistics on the system tables")&lt;BR /&gt;arcpy.AnalyzeDatasets_management(adminConn, "SYSTEM")&lt;/P&gt;&lt;P&gt;print("Finished.")&lt;/P&gt;</description>
      <pubDate>Mon, 04 Oct 2021 19:48:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/outlook-email-error/m-p/1104542#M62579</guid>
      <dc:creator>JamesStout101</dc:creator>
      <dc:date>2021-10-04T19:48:38Z</dc:date>
    </item>
    <item>
      <title>Re: Outlook email Error</title>
      <link>https://community.esri.com/t5/python-questions/outlook-email-error/m-p/1104552#M62580</link>
      <description>&lt;P&gt;Looks like your SMTP relay is bouncing you out, has it worked before? have you got the right credentials if it's secured?&lt;/P&gt;&lt;P&gt;I'd advise forgetting about the rest of the code when posting and testing the issue, if the rest of the code works, it's just gets in the way in terms of troubleshooting the emailer.&lt;/P&gt;&lt;P&gt;Also, possibly StackOverflow would be a better place.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Oct 2021 20:03:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/outlook-email-error/m-p/1104552#M62580</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-10-04T20:03:29Z</dc:date>
    </item>
    <item>
      <title>Re: Outlook email Error</title>
      <link>https://community.esri.com/t5/python-questions/outlook-email-error/m-p/1104569#M62581</link>
      <description>&lt;P&gt;Not sure exactly what that error might be referring to. Just for another thing to try, here's the email function I built based on &lt;A href="https://docs.python.org/3/library/email.examples.html#email-examples" target="_self"&gt;examples in the Python docs&lt;/A&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def sendEmail(subject="", body_content="", to=[], cc=[], bcc=[], from_addr="donotreply@mydomain.suffix"):
    """Send a plain text email.

    Required imports:
        from email.message import EmailMessage # for a text/plain email message
        import smtplib # for sending email

    :param str subject: Subject line of email.
    :param str body_content: Main message in the body of the email.
    :param list to: A list of email addresses as strings for sending the email to.
    :param list cc: A list of email addresses as strings for CC'ing the email.
    :param list bcc: A list of email addresses as strings for BCC'ing the email.
    :param str from_addr: The email address from which to send the email.
    :return: All email addresses that received the email (to, cc, bcc).
    :rtype: Dictionary
    """
    from email.message import EmailMessage
    import smtplib
    # Validate email recipient args
    for recipient in [to, cc, bcc]:
        if not isinstance(recipient, list):
            raise TypeError(f"Recipients (to, cc, bcc) must each be a list of strings; not {type(recipient)}")
    # Create email message object and content.
    msg = EmailMessage()
    msg.set_content(body_content)
    # Set recipients
    msg["Subject"] = subject
    msg["From"] = from_addr
    msg["To"] = ", ".join(to)
    msg["Cc"] = ", ".join(cc)
    msg["Bcc"] = ", ".join(bcc)
    # Send the message via our own SMTP server.
    with smtplib.SMTP("mymailserver.mydomain.suffix") as smtp:
        smtp.send_message(msg)
    print("sendEmail() successful")
    return {"to": to, "cc": cc, "bcc": bcc}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Like&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/167692"&gt;@DavidPike&lt;/a&gt;&amp;nbsp;mentioned, there's likely something your email server isn't happy with and the GeoNet community probably can't help much with that.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Oct 2021 20:39:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/outlook-email-error/m-p/1104569#M62581</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-10-04T20:39:43Z</dc:date>
    </item>
    <item>
      <title>Re: Outlook email Error</title>
      <link>https://community.esri.com/t5/python-questions/outlook-email-error/m-p/1106144#M62605</link>
      <description>&lt;P&gt;If i have a server error I don't know how to remedy that.&lt;/P&gt;&lt;P&gt;My user list has admin connections in it as well as user connections don't know how to remove the admin user names from the user list. The admins have no email.&lt;/P&gt;&lt;P&gt;Example, SDE, DBO, ARCFM, RESPONDER, non admin users all have emails admins do not.&lt;/P&gt;</description>
      <pubDate>Fri, 08 Oct 2021 20:42:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/outlook-email-error/m-p/1106144#M62605</guid>
      <dc:creator>JamesStout101</dc:creator>
      <dc:date>2021-10-08T20:42:09Z</dc:date>
    </item>
    <item>
      <title>Re: Outlook email Error</title>
      <link>https://community.esri.com/t5/python-questions/outlook-email-error/m-p/1106147#M62606</link>
      <description>&lt;P&gt;You may need to find an Outlook email administrator that supports your organization to troubleshoot a server error.&lt;/P&gt;&lt;P&gt;Our organization has a separate database that stores the email addresses of our normal users with user name so I just query that database with the connected user names from ArcGIS and send the email to every address that was actually returned. Since there are no org domain users like "SDE" or "DBO", there are no email addresses returned from the query.&lt;/P&gt;&lt;P&gt;How are you getting your email addresses?&lt;/P&gt;</description>
      <pubDate>Fri, 08 Oct 2021 20:51:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/outlook-email-error/m-p/1106147#M62606</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-10-08T20:51:30Z</dc:date>
    </item>
  </channel>
</rss>

