Smtplib use to send mail

Install

pip install smtplib

Transmitting qq, 163 mail messages with attachments

1.qq Mail

# Module for sending mail
import smtplib

# QQ-mail / 163 mail message sent: py file sending mail content equivalent to a third-party client, by means of e-mail QQ / 163 server to send the mail.

# QQ Mail
# 1> Configure SMTP mail server host address, the future use of the server send and receive mail.
= The HOST 'smtp.qq.com'
# 2> service port configuration, a default message port 25. A
PORT = '465'
#. 3> specifies the sender and recipient.
FROM = 'my qq mail'
the TO = 'to send qq mailbox'
# 4> Mail title
SUBJECT = 'This letter should only be heaven'
# 5> message content
CONTENT = 'two curved like Organization non-Organization Juan smoke eyebrow, one pair of non-weeping like weeping mesh with dew. State of Health two dimple depression, Johnson hit one disease. Little bit of tears, her breath slightly. The flower according to the water when Xianjing Jiao, '\
' Operations like Ruoliu Fufeng. And more than than dry an awakened heart, diseases such as West wins three points. Smart handsome, absolutely Korea unparalleled, temperament refined, elegant if cents, charming and romantic. Liu Fang from the dock, '\
' at first glance a greenhouse, but the line at the birds scared Tingshu, the time that the shadow of the cloister. Come cents sleeve of a robe floating at first glance, smell the fragrance of musk orchid, lotus clothing about to move Come, listen Huanpei the sonorous. Come dimple smile Chun Tao, Cui cloud heap bun, '\
' lips are cherries and pomegranate teeth Han Xiang. Come waist of neat, folk dance back to snow, Xi Chu Chui of shine, light yellow filled to capacity. haunt among the flowers Come, should hi should be angry, wandering Ikegami Come, '\
' Ruofei if Yang. Crescent frown laugh Xi, the language without words, at first glance Limbo shift Come, and wish only to be OK. He envy of good quality Xi, ice Yurun clean, Xian Xi He's costumes, '\
' flicker article. Audemars Piguet's appearance Yung Xi, Hong Pei Yuzhuo, the United States and the attitude he Xi, Feng Zhu Lung. how their prime, Chunmei snow blossoms. how it clean, Chrysanthemum is cream. how the static, '\
' Health Valley loose.
'Born to what, from where, the letter almost men, Yao Chi Fuji, the Purple Palace unparalleled. If who zai? Predicament beauty too!'

# Create a mailing Object
# ordinary mailing form
# smtp_obj = smtplib.SMTP ()

# Data will be encrypted during transmission.
smtp_obj = smtplib.SMTP_SSL ()

# Require sender authentication and authorization.
# Smtp_obj is a third-party client object
smtp_obj.connect (host = HOST, port = PORT)

# If you use a third-party client login, requires an authorization code, you can not use a real password to prevent password disclosure.
res = smtp_obj.login (user = FROM, password = ' my license code')
Print ( 'login result:', res)

# 发送邮件
for x in range(2):
msg = '\n'.join(['From: {}'.format(FROM), 'To: {}'.format(TO), 'Subject: {}'.format(SUBJECT), '', CONTENT])
smtp_obj.sendmail(from_addr=FROM, to_addrs=[TO], msg=msg.encode('utf-8'))
2.163邮件

smtplib Import
# 163 mailbox
the HOST = 'smtp.163.com'
# 2> service port configuration, a default message port 25. A
PORT = '25'
#. 3> specifies the sender and recipient.
FROM = 'Sender'
the TO = 'recipient'

# 4> Mail title
SUBJECT = 'Lo River'
# 5> message content
CONTENT = "Pina, if fleeting, If You Long Wan Rong Yao Qiu Ju, Huamao Chun loose. If hostess Qingyun shield months, floating Yao Xi If the wind flow back to the snow far and we hope for, "\
" Jiao if the sun rose the morning glow;.. force and police, burning out if Fuqu out Lu wave Calendula fiber Te, repair short heterozygosity If the shaved shoulder, waist as some elements. extension of the neck show items, "\
" Cheng Lu Hao quality. Hozawa no increase, Magnificence Front Royal. cloud bun Mindanao Mindanao, eyebrow shaping with Juan. red lips outside the Long, inside white teeth fresh, bright eyes gaze, dimple auxiliary bearing power. "\
" Rose Zi Yan Yi, Miriam quiet of leisure. tenderness Chuo state, mei languages odd clothes Masterpieces, bone like should map. Phi Troy's Cui Can Come, Joel Yao Bi of Hua Ju. "\
" Dakin Chui of jewelry, decorated Pearl to Yao footer. shoe practice excursion of the text, drag fog raw silk garment of light. Orchids aryl micro Xi Ai, step stoppage in the mountains corner. "

# Create a mailing Object
# ordinary mailing form
smtp_obj = smtplib.SMTP ()

# Data will be encrypted during transmission.
# Smtp_obj = smtplib.SMTP_SSL ()

# Require sender authentication and authorization.
# Smtp_obj is a third-party client object
smtp_obj.connect (host = HOST, port = PORT)

# If you use a third-party client login, requires an authorization code, you can not use a real password to prevent password disclosure.
res = smtp_obj.login (user = FROM, password = ' authorization code')
Print ( 'login result:', res)

# 发送邮件
msg = '\n'.join(['From: {}'.format(FROM), 'To: {}'.format(TO), 'Subject: {}'.format(SUBJECT), '', CONTENT])
smtp_obj.sendmail(from_addr=FROM, to_addrs=TO, msg=msg.encode('utf-8'))
3.发送附件

# Sending mail with attachments.

import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart, MIMEBase
from email import encoders

= HOST "smtp.qq.com"
PORT = "465"
SUBJECT = "test message"
the FROM = "Sender"
the TO = "Recipients"

# 1> Create an object used to send messages with file attachments
# related: the format of the message content, in the form of embedded on display.
message = MIMEMultipart ( 'related')

# Add a different type of message content to the message object.

# Send html content is the message, the message contains a picture.
# Parameter 2: specifies the message content type, the default is plain, that there is no any plain text format.
message_html = MIMEText ( '<h1> contains a picture message: </ h1> <p> Next will show this image of </ p> <img src = "cid: images">', 'html', 'utf8 ')

# 2> need message_html object is added to the message, waiting to be sent.
message.attach (message_html)

def load_image(path, cid):
data = open(path, 'rb')
message_img = MIMEImage(data.read())
data.close()

# Cid binding to the picture, based on the future value of this cid to find img tag inside the label corresponding.
message_img.add_header ( 'Content-ID', cid)

# Return MIMEImage object, the message object into the
return message_img

# Specify an image to the img tag
message.attach (load_image ( 'scrapy_img.png', 'images'))

# Document attachments, images and other attachments.
# If the data is typically a binary data format, when the designated second parameter, use base64, a data transmission format.
= MimeText message_docx (Open ( 'Test.docx', 'RB') Read (), 'Base64', 'UTF8'.)
# message_docx [ 'the Content-Disposition'] = 'Attachment; filename = Test.docx'
message_docx. the add_header ( 'Content-Disposition', 'Attachment', filename = 'mytest.docx')
message.attach (message_docx)


message_docx1 = MIMEText(open('测试.docx', 'rb').read(), 'base64', 'utf8')

# If the file name is in Chinese:
# add_header () can properly display Chinese;
# message_docx1 [ 'Content-Disposition'] is unable to properly display Chinese.

message_docx1['Content-Disposition'] = 'attachment;filename=测试.docx'
# message_docx1.add_header('content-disposition', 'attachment', filename='测试.docx')
message.attach(message_docx1)


message_image = MIMEText(open('scrapy_img.png', 'rb').read(), 'base64', 'utf8')
# message_image['Content-Disposition'] = 'attachment;filename=test.png'
message_image.add_header('content-disposition', 'attachment', filename='mytest.png')
message.attach(message_image)

message['From'] = FROM
message['Subject'] = SUBJECT
message['To'] = TO

client = smtplib.SMTP_SSL()
client.connect(HOST, PORT)
print('result: ',client.login(FROM, '授权码'))

print('发送结果:',client.sendmail(from_addr=FROM, to_addrs=[TO], msg=message.as_string()))
————————————————

Guess you like

Origin www.cnblogs.com/valorchang/p/11389174.html