Gmail的SMTP发送电子邮件问题 [英] SMTP Sending e-mail issue with Gmail
本文介绍了Gmail的SMTP发送电子邮件问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个使用SMTP发送.png文件的脚本.当我使用Hotmail帐户时;
I have a script that sends a .png file with SMTP. When I use a hotmail account;
smtplib.SMTP('smtp.live.com', 587)
它可以正常工作.但是当我使用Gmail帐户时;
It works without any problem. But when I use a gmail account;
smtplib.SMTP('smtp.gmail.com', 587)
引发错误: SMTPServerDisconnected:连接意外关闭
我已将 smtplib.SMTP('smtp.gmail.com',587)
更改为 smtplib.SMTP('localhost')
,但没有用.如何解决这个Gmail问题?
I've changed smtplib.SMTP('smtp.gmail.com', 587)
to smtplib.SMTP('localhost')
but didn't work. How can I fix this gmail problem?
推荐答案
您可以使用smtplib和email发送电子邮件,在我按照以下步骤操作后,此代码对我有用.
you can user smtplib and email for sending emails, this code working for me after i follow this steps.
步骤是
- 登录Gmail.
- 点击右上角的齿轮.
- 选择设置.
- 点击转发和POP/IMAP.
- 选择启用IMAP.6.单击保存更改"
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
me = "your email"
my_password = r"your password"
you = "to email id"
msg = MIMEMultipart('alternative')
msg['Subject'] = "Alert"
msg['From'] = me
msg['To'] = you
html = '<html><body><p>Hi, I have the following alerts for you!</p></body></html>'
part2 = MIMEText(html, 'html')
msg.attach(part2)
s = smtplib.SMTP_SSL('smtp.gmail.com')
s.login(me, my_password)
s.sendmail(me, you, msg.as_string())
print s
s.quit()
这篇关于Gmail的SMTP发送电子邮件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文