Send e-mail using the python achieve
Today, along with the video to learn a little knowledge, how to use python to write a small program to automatically send e-mail.
First, we need to have a 163 E-mail (E-mail can be anything) and then we log in to your mailbox, click Settings -> POP3 / SMTP / IMAP following figure appears:
the default option is to draw on the red box is not checked, which will we chose the client license key - "On the following figure
I can turn on the side, if the click is on, jump out the window
and then send text messages to set your own password authorization: (be careful not to and consistent with their own password!)
and back our POP3 / SMTP / IMAP page
we will use the figure crossed and so part of
the preparatory work has been well let's start writing code for it
we need to
import a mail library
import smtplib
E-mail Text Library
from email.mime.text import MIMEText
Then configure your SMTP server, here we are using 163 SMIP service, SMTP server if you use other mail is also possible
SMTPServer ="smtp.163.com"
Then write the address to send mail, which is our own mail account 163
Sender = "[email protected]"
Password write the sender's mailbox, that is, we have just configured mail password authorization
passwd = "123456a"
We set up their own e-mail to be sent contents, here I casually entered the a.
message = "hockel is a good men "
Then we want to send our message content is converted into a text message, here we use the e-mail text library MIMEText beginning to import
msg = MIMEText(message)
We can send mail to set up a title I, as follows.
msg["Subject"] = "来自帅哥的问候"
Create an SMTP server smtplib.SMTP (SMTP service, port number)
mailServer = smtplib.SMTP(SMTPServer, 25)
Log in to write mailbox login (email account, password authorization)
mailServer.login(Sender, passwd)
Send a message code sendmail (sender, receiver (here the list, there may be more than the recipient), MSG: sending a message: the message content typically msg.as_string (): as_string () is msg (MIMEText object or MIMEMultipart objects) becomes str.
)
mailServer.sendmail(Sender, ["[email protected]"],msg.as_string())
Exit mailbox
mailServer.quit()
The following is to witness the miracle of the moment, we run the program, and then open the recipient's mailbox as follows
specific code to send mail as follows, welcome to the discussion:
# 发邮件的库
import smtplib
#邮件文本
from email.mime.text import MIMEText
# smtp服务器
SMTPServer ="smtp.163.com"
# 发邮件的地址
Sender = "[email protected]"
#发送者邮箱的密码
passwd = "123456a"
# 设置发送的内容
message = "hockel is a good men "
# 转换成邮件文本
msg = MIMEText(message)
# 标题
msg["Subject"] = "来自帅哥的问候"
#发送者
msg["From"] = Sender
# 创建SMTP服务器 服务器 端口号
mailServer = smtplib.SMTP(SMTPServer, 25)
#登录邮箱
mailServer.login(Sender, passwd)
# 发送邮件
for x in range(1,10):
mailServer.sendmail(Sender, ["[email protected]"],msg.as_string())
x += 1
# 退出邮箱
mailServer.quit()