3

我正在使用本文中的代码使用gmail 发送电子邮件。

代码如下

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

当我运行它时,我得到以下套接字错误。

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

编辑:追溯

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`

如何获得脚本使用端口 587 的权限?还是gmail可以使用更高的端口号?

谢谢

4

2 回答 2

8

问题是 McAfee Antivirus。它阻塞了套接字连接。

来自 McAfee 访问保护日志

23/08/2013 10:23:54 am 被端口阻止规则阻止 C:\Program Files (x86)\Python26\python.exe 防病毒标准保护:防止群发邮件蠕虫发送邮件 74.125.25.108:25

我完全禁用了 McAfee,之后它运行良好。

于 2013-08-22T22:41:55.303 回答
8

对我来说,问题也是迈克菲。但我建议不要像 user1494941 那样完全禁用 McAfee,而是将 python.exe 添加到例外程序列表中:

在 VirusScan 控制台中,

  1. 右键单击“访问保护”,选择“属性”
  2. 在类别中,选择“防病毒标准保护”
  3. 在“规则”面板中,选择“防止群发邮件蠕虫发送电子邮件”
  4. 选择“编辑”,编辑此保护的规则。
  5. 将 python.exe 添加到要排除的进程列表中,然后确定。
于 2013-10-09T15:33:42.880 回答