user1494941
user1494941

Reputation: 175

Socket Error - Sending email using gmail

I am using the code from this article to send an email using gmail.

The code is as follows

import smtplib

def sendemail(from_addr, to_addr_list, cc_addr_list,
          subject, message,
          login, password,
          smtpserver='smtp.gmail.com:587'):
    header  = 'From: %s\n' % from_addr
    header += 'To: %s\n' % ','.join(to_addr_list)
    header += 'Cc: %s\n' % ','.join(cc_addr_list)
    header += 'Subject: %s\n\n' % subject
    message = header + message

    server = smtplib.SMTP(smtpserver)
    server.starttls()
    server.login(login,password)
    problems = server.sendmail(from_addr, to_addr_list, message)
    server.quit()
    return problems

When i run it, I however get the following socket error.

socket.error: [Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions

EDIT: Traceback

O:\Send Email With Attachment>SendEmail_Ver2.py
Traceback (most recent call last):
File "O:\Send Email With Attachment\SendEmail_Ver2.py", line 26, in <module>
password     = 'XXXXXXXXXXX')
File "O:\SendEmail_Ver2.py", line 13, in sendemail
server = smtplib.SMTP(smtpserver)
File "C:\Program Files (x86)\Python26\lib\smtplib.py", line 239, in __init__
(code, msg) = self.connect(host, port)
File "C:\Program Files (x86)\Python26\lib\smtplib.py", line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Program Files (x86)\Python26\lib\smtplib.py", line 273, in _get_socket
return socket.create_connection((port, host), timeout)
File "C:\Program Files (x86)\Python26\lib\socket.py", line 561, in create_connection
raise error, msg socket.error: [Errno 10013] An attempt was made to access a socket in   
a way forbidden by its access permissions`

How do I get privileges for the script to use port 587? Or is there a higher port number that gmail can use?

Thanks

Upvotes: 4

Views: 10894

Answers (2)

J Graham
J Graham

Reputation: 203

The problem for me was also McAfee. But instead of disabling McAfee completely as user1494941 did, I would recommend adding python.exe to the list of excepted programs:

In the VirusScan Console,

  1. Right-click on 'Access Protection', select 'Properties'
  2. In Categories, Select 'Anti-virus Standard Protection'
  3. In the Rules panel, select 'Prevent mass mailing worms from sending email'
  4. Select 'Edit', to edit the rules for this protection.
  5. Add python.exe to the list of processes to exclude and then OK.

Upvotes: 8

user1494941
user1494941

Reputation: 175

The problem was McAfee Antivirus. It was blocking the socket connection.

From McAfee Access Protection Log

23/08/2013 10:23:54 a.m. Blocked by port blocking rule C:\Program Files (x86)\Python26\python.exe Anti-virus Standard Protection:Prevent mass mailing worms from sending mail 74.125.25.108:25

I disabled McAfee completely and it worked fine after that.

Upvotes: 9

Related Questions