小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

使用帶有html正文的gmail帳戶的SMTP python發(fā)送電子郵件

 印度阿三17 2019-08-28

我正在嘗試使用SMTP庫從python腳本使用gmail帳戶發(fā)送電子郵件.它與普通的郵件正常工作正常.但是當(dāng)我嘗試使用HTML正文發(fā)送它時(shí).它不允許我發(fā)送.

# Import smtplib to provide email functions
import smtplib

# Import the email modules
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# Define email addresses to use
addr_to   = 'xxxx@localdomain.com'
addr_from = "xxxxx@gmail.com"

# Define SMTP email server details
smtp_server = 'smtp.gmail.com'
smtp_user   = 'xxxxxx@gmail.com'
smtp_pass   = 'xxxxxxx'

# Construct email
msg = MIMEMultipart('alternative')
msg['To'] = *emphasized text*addr_to
msg['From'] = addr_from
msg['Subject'] = 'Test Email From RPi'

# Create the body of the message (a plain-text and an HTML version).
text = "This is a test message.\nText and html."
html = """<html>
  <head></head>
  <body>
    <p>This is a test message.</p>
    <p>Text and HTML</p>
  </body>
</html>
"""

# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)

# Send the message via an SMTP server
s = smtplib.SMTP(smtp_server,587)
s.login(smtp_user,smtp_pass)
s.sendmail(addr_from, addr_to, msg.as_string())
s.quit()

解決方法:

在嘗試登錄之前添加這兩行,它不會(huì)給您帶來身份驗(yàn)證錯(cuò)誤.

server.ehlo()
server.starttls()

所以你的代碼應(yīng)該是這樣的:


    # Import smtplib to provide email functions
    import smtplib

    # Import the email modules
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText

    # Define email addresses to use
    addr_to   = 'xxxx@localdomain.com'
    addr_from = "xxxxx@gmail.com"

    # Define SMTP email server details
    smtp_server = 'smtp.gmail.com'
    smtp_user   = 'xxxxxx@gmail.com'
    smtp_pass   = 'xxxxxxx'

    # Construct email
    msg = MIMEMultipart('alternative')
    msg['To'] = *emphasized text*addr_to
    msg['From'] = addr_from
    msg['Subject'] = 'Test Email From RPi'

    # Create the body of the message (a plain-text and an HTML version).
    text = "This is a test message.\nText and html."

    (your html code)

    # Record the MIME types of both parts - text/plain and text/html.
    part1 = MIMEText(text, 'plain')
    part2 = MIMEText(html, 'html')

    # Attach parts into message container.
    # According to RFC 2046, the last part of a multipart message, in this case
    # the HTML message, is best and preferred.
    msg.attach(part1)
    msg.attach(part2)

    # Send the message via an SMTP server
    s = smtplib.SMTP(smtp_server,587)
    s.ehlo()
    s.starttls()
    s.login(smtp_user,smtp_pass)
    s.sendmail(addr_from, addr_to, msg.as_string())
    s.quit()

來源:https://www./content-1-418751.html

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多