Python sends Mail via SMTP

Source: Internet
Author: User

Python version: Python3.5.2

Brief introduction

SMTP is the protocol that sends messages, and Python's built-in support for SMTP can send plain text messages, HTML messages, and messages with attachments.

Python supports SMTP support with smtplib and email two modules, email is responsible for structuring the mail, Smtplib is responsible for sending mail.

I use QQ mailbox to complete this experiment, first should configure their own mailbox to enable the SMTP function, the following steps:

Login to the homepage of the QQ mailbox, find the setting function:

Go to Settings, switch to Account tab:

Drop-down to find the following options and enable the SMTP feature:

Upon successful activation, an authorization code is generated to log on to the SMTP server. This license code is not unique, you can click to forget the words generated again.

The experimental source is as follows
__author__ = "lance#" #-*-coding = utf-8-*-from Email import encodersfrom email.header import Headerfrom Email.mime.text Import mimetextfrom email.utils import parseaddr, Formataddrimport smtplibdef _format_addr (s): name, addr = Parseaddr ( s) return formataddr (Header (name, ' Utf-8 '). Encode (), addr)) from_addr = ' [email protected] ' password = ' Your Authorization code ' TO_A DDR = ' [email protected] ' smtp_server = ' smtp.qq.com ' #要发送的消息SendMsg = ' This is SMTP test. ' #构造一个 Mimetext Object msg = Mimetext (sendmsg, ' plain ', ' utf-8 ') #依次填充对象的各个选项msg [' from '] = _format_addr (' Python <%s> '% fr OM_ADDR) msg["to" = _format_addr (' User <%s> '% to_addr) msg[' Subject '] = Header (' This is SMTP test. ', ' Utf-8 '). encod E () #构造 the SMTP server, the SMTP port for the QQ mailbox is 465 and the SSL encryption protocol server = Smtplib. Smtp_ssl (Smtp_server, 465) #启用该选项, can print out all the information that interacts with the SMTP server Server.set_debuglevel (1) server.login (from_addr, password) # Send mail, here to_addr can be a list, can send to multiple people server.sendmail (FROM_ADDR, [to_addr], msg.as_string ()) #退出 SMTP Server Server.quit ()

The mailbox that receives the message can be set to the same as the sender's address, so that when the program finishes running, its mailbox can receive the message that was just sent.

Python sends Mail via SMTP

Related Article

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.