为什么我的使用 smtplib 发送电子邮件的机器人不起作用?



看看下面的代码,每次我尝试使用这个函数时,它都不起作用,我有点不知道为什么,所以有人可以帮我一把?

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}nn{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()
)

相关内容

最新更新