在Python中使用SMTP发送电子邮件

Library:

图书馆:

smtplib

SMTP is a simple mail transfer protocol used by the email servers to categorize the emails and deliver them to the clients.

SMTP是电子邮件服务器使用的一种简单的邮件传输协议,用于对电子邮件进行分类并将其传递给客户端。

smtplib:

smtplib:

smtplib is a python library that creates an SMTP session to send emails and route the mails.

smtplib是一个python库,可创建SMTP会话来发送电子邮件和路由邮件。

使用SMTP发送电子邮件的步骤 (Steps to send emails using SMTP)

1) Create a SMTP Server:

1)创建一个SMTP服务器:

We can create an SMTP server with the help of smtplib.SMTP("smtp.gmail.com",587), smtp.gmail.com is the Gmail SMTP server address and 587 is the port number of the server.

我们可以借助smtplib.SMTP(“ smtp.gmail.com”,587)创建SMTP服务器, smtp.gmail.com是Gmail SMTP服务器地址,而587是服务器的端口号。

2) To identify our server:

2)要识别我们的服务器:

We can identify our server with the help server.elho() function, we should do this connection to our server with other servers to send mails.

我们可以使用help server.elho()函数来标识我们的服务器,我们应该将该服务器与其他服务器建立连接以发送邮件。

3) Make our server secure:

3)确保我们的服务器安全:

We can make our server secure with the help of the server.starttls(), TLS is Transport Layer Security.

我们可以借助server.starttls()来确保服务器的安全性,TLS是传输层安全性。

4) Login in our server:

4)登录我们的服务器:

As we will use the Gmail SMTP server so we can log in to the server with the help of the server.login("<email id>","<password>").

由于我们将使用Gmail SMTP服务器,因此我们可以在server.login(“ <email id>”,“ <password>”)的帮助下登录服务器。

5) Send the mail:

5)发送邮件:

We can send the mail with the help of server.sendmail(<Our mail address>,<To we want to send>, message) function.

我们可以借助server.sendmail(<我们的邮件地址>,<到我们要发送的>,message)函数来发送邮件

6) server.close():

6)server.close():

After sending the close the server with this function.

发送关闭后,具有此功能的服务器。

Python程序发送电子邮件 (Python program to send email)

# import the library
import smtplib

# setting up the Gmail smtp server
server=smtplib.SMTP("smtp.gmail.com",587)
# Identify our server before sending mail
server.ehlo()
# making our server secure
server.starttls()

# login in the server
server.login("<email id>","<password>")

# sending mail
message="Hello This is Abhinav Gangrade,how are you"
server.sendmail("<your mail id","<To whom you want to send>",message)

# closing the server
server.close()

Note: You have to change the setting of your Gmail (Less secure app to on).Otherwise, we will not be able to send mails this is because we created a virtual server, and google security will not allow anyone to login to its server so that's why we have to change the setting.

注意 :您必须将Gmail(少安全应用程序)的设置更改为开启。否则,我们将无法发送邮件,这是因为我们创建了虚拟服务器,并且Google安全性不允许任何人登录其服务器这就是为什么我们必须更改设置。

翻译自: https://www.includehelp.com/python/sending-emails-using-smtp.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值