I have written a script to send email using python through "smtp.office365.com" and its working fine on my Desktop machine but when I an running same script on VM its giving below error:
<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')
I am using below code to send email:
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())
Please note i have tried various things mentioned on this link
Example:
- Changing the Command prompt runas administrator and then try to
execute the script. - Changing the port to 25 or 995 or 465(tried SSL also) as mentioned on different forums.
- Checked their is not Firewall setting present and antivirus is also not running on VM.