Python 2.7:无法在虚拟机上使用相同的脚本发送电子邮件(在桌面上使用相同的脚本)

时间:2018-04-13 10:44:09

标签: python email smtplib

我编写了一个脚本,通过" smtp.office365.com"使用python发送电子邮件。并且它在我的桌面计算机上工作正常,但是当我在VM上运行相同的脚本时,它给出了以下错误:

<class 'socket.error'>
[Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions
[('C:\\Users\\Administrator\\.jenkins\\workspace\\xxxxx\\Libraries\\NewEmailUtility.py', 190, 'sendEmail', 'server.connect(SMTPEMAILHOST,SMTPEMAILPORT)'), ('C:\\Python27\\lib\\smtplib.py', 317, 'connect', 'self.sock = self._get_socket(host, port, self.timeout)'), ('C:\\Python27\\lib\\smtplib.py', 292, '_get_socket', 'return socket.create_connection((host, port), timeout)'), ('C:\\Python27\\lib\\socket.py', 575, 'create_connection', 'raise err')]
Traceback (most recent call last):
  File "C:\Users\Administrator\.jenkins\workspace\xxxxx\Libraries\SendEmail.py", line 25, in <module>
    emailUtil.sendEmail(mailSubject,mailbody)
  File "C:\Users\Administrator\.jenkins\workspace\xxxxx\Libraries\NewEmailUtility.py", line 198, in sendEmail
    raise Exception("Unable to connect to EmailHost: ", SMTPEMAILHOST)
Exception: ('Unable to connect to EmailHost: ', 'smtp.office365.com')

我使用以下代码发送电子邮件:

SMTPEMAILHOST = "smtp.office365.com"
SMTPEMAILPORT =  587

server = smtplib.SMTP()
server.connect(SMTPEMAILHOST,SMTPEMAILPORT)
server.ehlo()
server.starttls()
server.login(SMTPUSERNAME,SMTPPASSWORD)
server.sendmail(self.From, msg["To"].split(", "), msg.as_string())

请注意我已尝试过link

中提及的各种内容

示例:

  1. 更改命令提示符runas管理员,然后尝试
    执行脚本。
  2. 将端口更改为25或995或465(尝试使用SSL 另外)在不同的论坛上提到。
  3. 检查他们的防火墙设置是否存在,并且防病毒软件也未在VM上运行。

0 个答案:

没有答案