叶小贤 发表于 2019-11-7 15:10:20

关于PYTHON发送邮件问题

我查了一些资料,实际操作时发现连接不上服务器,请问哪里有问题??

报错如下:
Traceback (most recent call last):
File "D:/pythontest/venv/ABC/163mailDemo.py", line 12, in <module>
    smtp = smtplib.SMTP_SSL(mailserver,465)# 连接邮箱服务器,smtp的端口号是25
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 1030, in __init__
    source_address)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 251, in __init__
    (code, msg) = self.connect(host, port)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 336, in connect
    self.sock = self._get_socket(host, port, self.timeout)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 1036, in _get_socket
    self.source_address)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\socket.py", line 704, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\socket.py", line 745, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: getaddrinfo failed



代码如下:

import smtplib
from email.mime.text import MIMEText

mailserver ="smtp.163.com"#邮箱服务器地址
username_send = 'xxxxxxxx@163.com'#邮箱用户名
password = 'xxxxxxx'   #邮箱密码:需要使用授权码
username_recv = 'xxxxx@qq.com'#收件人,多个收件人用逗号隔开
mail = MIMEText('这是发用的邮件内容')
mail['Subject'] = '这是邮件主题'
mail['From'] = username_send#发件人
mail['To'] = username_recv#收件人;[]里的三个是固定写法,别问为什么,我只是代码的搬运工
smtp = smtplib.SMTP(mailserver,port=25)# 连接邮箱服务器,smtp的端口号是25
smtp.login(username_send,password)#登录邮箱
smtp.sendmail(username_send,username_recv,mail.as_string())# 参数分别是发送者,接收者,第三个是把上面的发送邮件的内容变成字符串
smtp.quit() # 发送完毕后退出smtp
print ('success')

塔利班 发表于 2019-11-7 15:31:47

授权码是网易给的么,没写成密码了吧

叶小贤 发表于 2019-11-7 15:56:37

塔利班 发表于 2019-11-7 15:31
授权码是网易给的么,没写成密码了吧

smtp = smtplib.SMTP(mailserver,port=25)# 连接邮箱服务器,smtp的端口号是25

这行代码就已经报错了。。还没到登陆那一步

塔利班 发表于 2019-11-7 16:29:01

叶小贤 发表于 2019-11-7 15:56
smtp = smtplib.SMTP(mailserver,port=25)# 连接邮箱服务器,smtp的端口号是25

这行代码就已经报错了 ...

发件好像是126不是163

一个账号 发表于 2019-11-8 19:57:04

贴代码要按"<>"!

heavensman 发表于 2020-5-6 14:32:41

我也遇到过这个问题,网易太强大了,别说我,连小甲鱼的鱼C给我发邮件也被拦截掉了,建议换一个QQ邮箱再试试
页: [1]
查看完整版本: 关于PYTHON发送邮件问题