简体   繁体   English

套接字错误 - 使用Gmail发送电子邮件

[英]Socket Error - Sending email using gmail

I am using the code from this article to send an email using gmail. 我使用从代码文章使用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? 如何获得脚本使用端口587的权限? Or is there a higher port number that gmail can use? 或者gmail可以使用更高的端口号?

Thanks 谢谢

The problem was McAfee Antivirus. 问题是McAfee Antivirus。 It was blocking the socket connection. 它阻止了套接字连接。

From McAfee Access Protection Log 来自McAfee Access Protection日志

23/08/2013 10:23:54 am 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 23/08/2013 10:23:54 am阻塞端口阻塞规则C:\\ Program Files(x86)\\ Python26 \\ python.exe防病毒标准防护:防止群发邮件蠕虫发送邮件74.125.25.108:25

I disabled McAfee completely and it worked fine after that. 我完全禁用了McAfee,之后它运行良好。

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: 但是我没有像user1494941那样完全禁用McAfee,我建议将python.exe添加到例外程序列表中:

In the VirusScan Console, 在VirusScan控制台中,

  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. 将python.exe添加到要排除的进程列表中,然后单击“确定”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM