[英]SMTP Sending e-mail issue with Gmail
我有一個使用 SMTP 發送 .png 文件的腳本。 當我使用 hotmail 帳戶時;
smtplib.SMTP('smtp.live.com', 587)
它可以毫無問題地工作。 但是當我使用 gmail 帳戶時;
smtplib.SMTP('smtp.gmail.com', 587)
引發錯誤: SMTPServerDisconnected: Connection unexpectedly closed
我已將smtplib.SMTP('smtp.gmail.com', 587)
更改為smtplib.SMTP('localhost')
但沒有用。 我該如何解決這個 gmail 問題?
試試這個代碼,它對我來說很好用,
import smtplib
## email sending function
def email_sender(input_message, email_to, client):
''' function to send email '''
to = email_to
gmail_user = '' ## email of sender account
gmail_pwd = '' ## password of sender account
smtpserver = smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_pwd)
header = 'To:' + to + '\n' + 'From: ' + gmail_user + '\n' +'Subject:site down! \n'
input_message = input_message + client
msg = header + input_message
smtpserver.sendmail(gmail_user, to, msg)
smtpserver.close()
您可以使用 smtplib 和電子郵件來發送電子郵件,在我按照以下步驟操作后,此代碼對我有用。
步驟是
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.