smtp_ssl_host = 'smtp.webfaction.com'
smtp_ssl_port = ***
username = '******'
password = '******'
sender = '*******'
targets = [' ******', '******', '*****', '*******', '********',
'**********', '**********']
msg = MIMEText(message)
msg['Subject'] = 'Device Status Information'
msg['From'] = sender
msg['To'] = ', '.join(targets)
server = smtplib.SMTP_SSL(smtp_ssl_host, smtp_ssl_port)
server.login(username, password)
server.sendmail(sender, targets, msg.as_string())
server.quit()
现在我得到了像
这样的例外 Traceback (most recent call last):
File "/usr/local/bin/ping.py", line 271, in <module>
thread1 = myThread(in_data)
File "/usr/local/bin/ping.py", line 128, in __init__
status_check = self.status_check(lane=lane, direction=direction, anpr=anpr_state, axle=axle_state, camera=camera_state, profiler=profiler_state, user=user_state, weighbridge=weighbridge_state, tollcontrol=tollcontrol_state, timestamp=tt)
File "/usr/local/bin/ping.py", line 221, in status_check
self.send_email(message=message)
File "/usr/local/bin/ping.py", line 256, in send_email
server = smtplib.SMTP_SSL(smtp_ssl_host, smtp_ssl_port)
File "/usr/lib/python2.7/smtplib.py", line 793, in __init__
SMTP.__init__(self, host, port, local_hostname, timeout)
File "/usr/lib/python2.7/smtplib.py", line 256, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.7/smtplib.py", line 316, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python2.7/smtplib.py", line 798, in _get_socket
new_socket = socket.create_connection((host, port), timeout)
File "/usr/lib/python2.7/socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known
在上面的例外 self.status_check(lane = lane,direction = direction,anpr = anpr_state,axle = axle_state,camera = camera_state,profiler = profiler_state,user = user_state,weighbridge = weighbridge_state,tollcontrol = tollcontrol_state, timestamp = tt)是一个函数,我调用另一个函数 self.send_email(message = message),并将消息作为参数,并在 self.send_email(message = message)中功能我写了一封电子邮件发送代码,我得到了以上异常。
注意:以上所有代码我都在主管*
中运行它在localhost中运行良好但在主管中运行时出现异常
答案 0 :(得分:0)
1-为什么你在使用smtp.SMTP时使用smtp.SMTP_SSL?
import smtplib
server = smtplib.SMTP('smtp.webfaction.com', 587)
2-为什么你不启动TLS协议:
server.ehlo()
server.starttls()
3-有一个例子:
import smtplib
from email.mime.text import MIMEText
from email.header import Header
targets = ['facebook@facebook.com', 'msn@msn.com' , 'gmail@gmail.com', 'hotmail@hotmail.com' , 'yahoo@yahoo.com']
server = 'smtp.webfaction.com'
port = 587;
server = smtplib.SMTP(server, port)
server.ehlo()
server.starttls()
server.login(username, password)
msgbody = 'This Is an Device Status Information'
msg = MIMEText(msgbody, 'plain', 'utf-8')
msg['Subject'] = Header('Device Status Information', 'utf-8')
msg['From'] = Header(sender, 'utf-8')
target = []
target.append(','.join(map(str, targets)))
msg['To'] = Header(target, 'utf-8')
txt = msg.as_string()
server.sendmail(sender, target, txt)
server.quit()