我正在使用mailtrap作为smtp服务器来发送电子邮件。但是,当我发送电子邮件时,出现错误。登录凭据正确无误,出于隐私考虑,我更改了电子邮件。请注意,我正在直接连接到笔记本电脑的pycom设备上运行此程序。
def sendEmail():
sender = "Private Person <from@mail.com>"
receiver = "A Test User <to@mail.com>"
port = 2525
smtp_server = "smtp.mailtrap.io"
login = "username" # paste your login generated by Mailtrap
password = "password" # paste your password generated by Mailtrap
message = """
Subject: Hi Mailtrap
To: {receiver}
From: {sender}
This is a test e-mail message."""
try:
with smtplib.SMTP(smtp_server, port) as server:
server.login(login, password)
server.sendmail(sender, receiver, message)
print('Sent')
except smtplib.SMTPServerDisconnected:
print('Failed to connect to the server. Wrong user/password?')
except smtplib.SMTPException as e:
print('SMTP error occurred: ' + str(e))
错误消息:
Traceback (most recent call last):
File "<stdin>", line 147, in <module>
File "<stdin>", line 114, in sendEmail
File "<stdin>", line 106, in sendEmail
File "/flash/lib/smtplib.py", line 84, in __init__
File "/flash/lib/smtplib.py", line 110, in connect
AttributeError: 'module' object has no attribute 'IPPROTO_SEC'
>
第106行引用server.login(login, password)
,第114行是print('SMTP error occurred: ' + str(e))
之后的行
我已从smptlib中摘录了片段,这引起了问题
def connect(self, host, port=0, source_address=None):
if source_address:
self.source_address = source_address
if not port and (host.find(':') == host.rfind(':')):
i = host.rfind(':')
if i >= 0:
host, port = host[:i], host[i + 1:]
try:
port = int(port)
except ValueError:
raise OSError("non numeric port")
if not port:
port = self.default_port
if self.debuglevel > 0:
print('connect:', (host, port), file=stderr)
if self.tls:
ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_SEC)
self.sock = ssl.wrap_socket(ss)
else:
self.sock = socket.socket()
self.sock.settimeout(self.timeout)
try:
self.sock.connect(socket.getaddrinfo(host, port)[0][4])
except:
self.close()
(code, msg) = self.getreply()
if self.debuglevel > 0:
print("connect:", msg, file=stderr)
return (code, msg)
特别是ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_SEC)
任何帮助将不胜感激,谢谢:)