[英]Why doesn't my bot of sending Emails using smtplib work?
请参阅下面的代码,每次我尝试使用这个 function 时,它都不起作用,我有点不知道为什么,所以有人可以帮帮我吗?
import smtplib
def send_email():
server = smtplib.SMTP('smtp.hotmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login('bot_piloto.test@hotmail.com', '*******')
subject = 'You have to see this, it is about your university'
body = 'look, these are yours pendenting homeworks: ' + name_teacher2 + ' and these are yours expired homeworks: '+ name_teacher
sender: 'bot_piloto.test@hotmail.com'
receiver: 'bot_piloto.test@hotmail.com'
msg = f"Subject: {subject}\n\n{body}"
server.sendmail(
sender,
receiver,
msg
)
server.quit()
这与您的原始代码完全不同,但这是我发现的适合我的方法。 使用或不使用它,但这种方法非常可靠。
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import ssl
def send_email():
subject = 'You have to see this, it is about your university'
body = 'look, these are yours pendenting homeworks: ' + name_teacher2 + ' and these are yours expired homeworks: '+ name_teacher
sender = 'bot_piloto.test@hotmail.com'
receiver = 'bot_piloto.test@hotmail.com'
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = receiver
body = MIMEText(body, "plain")
msg.attach(body)
#Create secure connection with server and send email
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.hotmail.com", 587, context=context) as server:
server.login(sender, "*******")
server.sendmail(
sender,
receiver,
msg.as_string()
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.