Send plain text messages .py

smtplib Import 
from email.mime.text Import MimeText
from email.header Import Header

# third-party SMTP service
# Set the server
mail_host = "smtp.qq.com"

# Username
mail_user = "[email protected]"

# obtain an authorization code
mail_pass = "mpaocydzpzfjidge"

# sender account
sENDER = '[email protected]'

# receiving messages may be provided to your QQ-mail mailbox or other
Receivers = [ '[email protected]']

send_content = 'mail sending test the Python ... '

# The first parameter is the content of the message, the second format text, the third set encoding
message = MimeText (send_content,' Plain ',' UTF-. 8 ')

# sender
message [' From ' ] = Header ( "I sender", 'utf-8')

# recipient
message [ 'To'] = Header ( " I addressee", 'utf-8')

Subject = 'Message Title'
Message [ 'the Subject'] = Header (Subject, 'UTF-. 8')

the try:
smtpObj = smtplib.SMTP ()
# 25 is the SMTP port number
smtpObj.connect (mail_host, 25)
smtpObj.login (mail_user, mail_pass)
smtpObj.sendmail (SENDER, Receivers, message.as_string ())
Print ( "Send e-mail success ")
the except smtplib.SMTPException:
Print (" Error: Can not send Mail ")

Guess you like

Origin www.cnblogs.com/zhang-da/p/12231694.html