Send 163 mail with python

Mail is one of the earliest ways to communicate with computers. The pop3 protocol is used to receive mail, and the smtp protocol is used to send mail. Basically, all computer communications are based on the tcp/udp protocol, and the mail transfer protocol is not an exception. If you want to send emails in a programming language, you need to enable the pop3/smtp service in your email settings, which is not particularly troublesome. Just get familiar with it at the beginning, and it will be much better later, and you will basically be familiar with the road.

 

 

Python is an open source, free, and general-purpose scripting programming language. It is easy to use, powerful, and adheres to "minimalism".

The Python class library (module) is extremely rich, which makes Python almost omnipotent. Whether it is traditional Web development, PC software development, Linux operation and maintenance, or the current hot machine learning, big data analysis, and web crawlers, Python can be competent .

 

Let's show it in python 

import smtplib
from email.mime.text import MIMEText
from email.header import Header

# 第三方 SMTP 服务
mail_host = "smtp.163.com"  # 设置服务器
mail_user = "[email protected]"  # xxx是你的163邮箱用户名
mail_pass = "TTVZISPRBPGXIBIO"  # 口令是你设置的163授权密码

sender = '[email protected]'    #xxx是发送者邮箱
receivers = ['[email protected]']  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱

message = MIMEText('Python 邮件发送测试...', 'plain', 'utf-8')#邮箱内容
message['From'] = Header("me", 'utf-8')#发送者显示
message['To'] = Header("me", 'utf-8')#接受者显示

subject = 'email test'#邮箱标题
message['Subject'] = Header(subject, 'utf-8')

try:
    smtpObj = smtplib.SMTP()
    smtpObj.connect(mail_host, 25)  # 25 为 SMTP 端口号
    smtpObj.login(mail_user, mail_pass)
    smtpObj.sendmail(sender, receivers, message.as_string())
    print
    "邮件发送成功"
except smtplib.SMTPException:
    print
    "Error: 无法发送邮件"

Guess you like

Origin blog.csdn.net/weixin_42815827/article/details/128553755