我编写了一个脚本,通过" 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
中提及的各种内容示例: