如何使用 smtplib 和 MIMEText 发送电子邮件?
我花了几天时间寻找答案,直到现在我还没有解决我的问题。 我想要的只是在发生错误时从我的程序发送一封简单的电子邮件。 我正在使用 gmail 服务器来执行此操作,它实际上发送了电子邮件,但电子邮件的消息没有出现。 代码如下:
def accept(self):
HOST = 'smtp.gmail.com'
PORT = '587'
user = '***********@gmail.com'
senha = '***********'
servidor = smtplib.SMTP()
servidor.connect(HOST, PORT)
servidor.ehlo()
servidor.starttls()
servidor.ehlo()
servidor.login(user, senha)
assunto = 'Relatorio de Erro'
para = '**************@hotmail.com'
endereco = self.nemail.text()
de = str(endereco)
texto = self.message.toPlainText()
textos = Message(texto)
corpo = MIMEText(textos)
corpo['From'] = de
corpo['To'] = user
corpo['Subject'] = assunto
servidor.set_debuglevel(1)
servidor.sendmail(user, para, corpo.as_string())
servidor.quit
这会在 MIMEText 上引发错误。说无法将 MIMEText 转换为 str。 我尝试了另一个版本,仅供测试。 发送一个简单的字符串:
def accept(self):
HOST = 'smtp.gmail.com'
PORT = '587'
user = '*************@gmail.com'
senha = '************'
servidor = smtplib.SMTP()
servidor.connect(HOST, PORT)
servidor.ehlo()
servidor.starttls()
servidor.ehlo()
servidor.login(user, senha)
assunto = 'Relatorio de Erro'
para = '*********@hotmail.com'
servidor.set_debuglevel(1)
servidor.sendmail(user, para, assunto)
servidor.quit()
电子邮件到达正确的收件箱,但没有任何消息,什么也没有。 我搜索了很多,我读了文档,我尝试了很多,以至于我什至不知道我在做什么。看来我快要明白了。 有人可以帮助我吗?
I'm looking for an answer for some days and untill now I just didn't solve my problem.
All I want is to send a simple email message from my program if an error occure.
I'm using the gmail server to do that, and it actually sends the email, but the message of the email doesn't appear.
Here's the code:
def accept(self):
HOST = 'smtp.gmail.com'
PORT = '587'
user = '***********@gmail.com'
senha = '***********'
servidor = smtplib.SMTP()
servidor.connect(HOST, PORT)
servidor.ehlo()
servidor.starttls()
servidor.ehlo()
servidor.login(user, senha)
assunto = 'Relatorio de Erro'
para = '**************@hotmail.com'
endereco = self.nemail.text()
de = str(endereco)
texto = self.message.toPlainText()
textos = Message(texto)
corpo = MIMEText(textos)
corpo['From'] = de
corpo['To'] = user
corpo['Subject'] = assunto
servidor.set_debuglevel(1)
servidor.sendmail(user, para, corpo.as_string())
servidor.quit
This raises an error on the MIMEText. Says that can't convert MIMEText to str.
The I tried another version, just for test.
Sending a simple string:
def accept(self):
HOST = 'smtp.gmail.com'
PORT = '587'
user = '*************@gmail.com'
senha = '************'
servidor = smtplib.SMTP()
servidor.connect(HOST, PORT)
servidor.ehlo()
servidor.starttls()
servidor.ehlo()
servidor.login(user, senha)
assunto = 'Relatorio de Erro'
para = '*********@hotmail.com'
servidor.set_debuglevel(1)
servidor.sendmail(user, para, assunto)
servidor.quit()
The email comes to the right inbox, but it has no message, nothing.
I searched a lot, I'd read the docs, I tried it so much that I don't even know what I'm doing. It looks like I'm almost getting.
Can someone help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是根据我在进行系统维护时使用的代码修改的工作版本。
Here is a working version modified from code I use when we do system maintenance.