0

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:

  1. Changing the Command prompt runas administrator and then try to
    execute the script.
  2. Changing the port to 25 or 995 or 465(tried SSL also) as mentioned on different forums.
  3. Checked their is not Firewall setting present and antivirus is also not running on VM.
thebadguy
  • 1,872
  • 1
  • 17
  • 27
  • Does your VM use a bridged interface to your physical one? I.e. does the VM actually have access to your internet gateway? Can you ping smtp.office365.com from your VM? – shmee Apr 13 '18 at 10:46
  • Yes VM can connect to internet. I can open smtp.office365.com on chrome and same VM. Also I can ping and it shows reply: Pinging outlook-in.office365.com [40.100.136.2] with 32 bytes of data: Reply from 40.100.136.2: bytes=32 time=39ms TTL=240 Reply from 40.100.136.2: bytes=32 time=39ms TTL=240 Reply from 40.100.136.2: bytes=32 time=39ms TTL=240 Reply from 40.100.136.2: bytes=32 time=39ms TTL=240 – thebadguy Apr 13 '18 at 10:55

0 Answers0