通过Python脚本发送简单电子邮件时出错

时间:2016-12-29 14:31:44

标签: python email

我首先想说的是我对Python和编码的新手。在我的业余时间慢慢地教我自己,因为我的无能而无所事事。此脚本正在获取主动连接到我的SDE(SQL数据库)的用户名,然后将它们添加到列表中。在" @ company.com"最后给他们发送一封电子邮件,提醒他们保存数据并在服务器维护之前注销。

此脚本由软件供应商提供:http://desktop.arcgis.com/en/arcmap/10.3/manage-data/geodatabases/using-python-scripting-to-batch-reconcile-and-post-versions.htm

我收到的错误如下。

Traceback (most recent call last):
File "C:\Users\jhead\Desktop\Scripts\ArcPy\SDE_Maintenance.py", line 32, in             <module>
server.sendmail(FROM, TO, MESSAGE)
File "C:\Python27\ArcGIS10.3\lib\smtplib.py", line 723, in sendmail
self.rset()
File "C:\Python27\ArcGIS10.3\lib\smtplib.py", line 462, in rset
return self.docmd("rset")
File "C:\Python27\ArcGIS10.3\lib\smtplib.py", line 387, in docmd
return self.getreply()
File "C:\Python27\ArcGIS10.3\lib\smtplib.py", line 363, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
SMTPServerDisconnected: Connection unexpectedly closed

这是我的代码块,其中包含电子邮件部分:

&#13;
&#13;
import arcpy, time, smtplib

# Set the workspace
arcpy.env.workspace = 'Database Connections/admin.sdeLakeWorth.sde'
# Set a variable for the workspace
workspace = arcpy.env.workspace

# Get a list of connected users.
userList = arcpy.ListUsers("Database Connections/LakeWorth.sde")

# Get a list of user names of users currently connected and make email addresses
emailList = [user.Name + "@mycompnay.org" for user in arcpy.ListUsers("Database Connections/LakeWorth.sde")]

# Take the email list and use it to send an email to connected users.
SERVER = "EXCAS.Mycompany.org"
FROM = "SDE Admin <myname@mycompany.org>"
TO = emailList
SUBJECT = "SDE Maintenance is about to be performed"
MSG = "Auto generated Message.\n\rServer maintenance will be performed in 15 minutes. Please save all open GIS files and exit out."

# Prepare actual message
MESSAGE = """\
From: %s
To: %s
Subject: %s

%s
""" % (FROM, ", ".join(TO), SUBJECT, MSG)

# Send the mail
server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, MESSAGE)
server.quit()
&#13;
&#13;
&#13;

根据要求,我在脚本尝试发送电子邮件之前添加了一些打印语句,请参阅下文:

&#13;
&#13;
print FROM
print TO
print SERVER
print SUBJECT
print MSG

# Send the mail
server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, MESSAGE)
server.quit()
&#13;
&#13;
&#13;

并返回:

&#13;
&#13;
SDE Admin <myemail@mycompany.org>
[u'DBO@lakeworth.org', u'"MYLAKEWORTH\\GRHOADS"@lakeworth.org', u'DBO@lakeworth.org']
EXCAS.MyLakeWorth.org
SDE Maintenance is about to be performed
Auto generated Message.

Server maintenance will be performed in 15 minutes. Please save all open GIS files and exit out.

Traceback (most recent call last):
  File "C:\Users\myname\Desktop\Scripts\ArcPy\SDE_Maintenance.py", line 39, in <module>
    server.sendmail(FROM, TO, MESSAGE)
  File "C:\Python27\ArcGIS10.3\lib\smtplib.py", line 723, in sendmail
    self.rset()
  File "C:\Python27\ArcGIS10.3\lib\smtplib.py", line 462, in rset
    return self.docmd("rset")
  File "C:\Python27\ArcGIS10.3\lib\smtplib.py", line 387, in docmd
    return self.getreply()
  File "C:\Python27\ArcGIS10.3\lib\smtplib.py", line 363, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
SMTPServerDisconnected: Connection unexpectedly closed
&#13;
&#13;
&#13;

所以我编辑了我的名字/电子邮件obv,但似乎电子邮件列表没有抓取名称,只会创建一个无效的电子邮件地址发送给...嗯我想这是一个问题,但错误状态我正在断开连接或服务器无法连接我在smtp代码中遗漏了哪些东西会解决这个问题?

0 个答案:

没有答案
相关问题