Use Python to send messages

Source: Internet
Author: User

The following example is QQ mailbox to QQ mailbox, do not know what other mailboxes are different

(1) First the sender's SMTP service needs to be opened, now send the time required authorization code



Click the Generate authorization code, may want some authentication information, through can see authorization code, copy down


(2) Writing code

#-*-Coding:utf-8-*-
from email.mime.text import mimetext
from Email.header Import Header
# The first is text content, The second format text, the third encoded format
msg = mimetext (' python Mail send Test ', ' plain ', ' utf-8 ')
# appears on sender
msg[' from '] = Header (' I am sender ', ' Utf-8 ')
# display with recipient
msg[' to '] = header (' Yourself ', ' Utf-8 ')
# is the title, most eye-catching
subject = ' Python smtp send mail Test _9.20 '
msg[' Subject '] = Header (Subject, ' Utf-8 ')
# 
send side
from_addr = ' sender @qq.com '
# must be an automatic authorization code, Need to send person's authorization code
password = ' abcdefghijklmn '

# QQ SMTP server
smtp_server = ' smtp.qq.com '
# receiver
To_ Addr = ' recipient @qq.com '
import smtplib
# server = Smtplib. SMTP (smtp_server,25)
# uses the SSL mode
server = Smtplib. Smtp_ssl (smtp_server,465)
# set to debug mode
server.set_debuglevel (1)

# login SSL server
server.login (from_ Addr,password)
# Send mail
server.sendmail (from_addr,[to_addr],msg.as_string ())
# exit
Server.quit ( )

(3) Successful delivery



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.