Skip to main content
 首页 » 编程设计

python之从 Python 发送电子邮件

2025年05月04日28虾米哥

我正在尝试使用 smtp 服务器从 python 发送电子邮件,但它抛出了 错误。我该如何解决? 我也已获得 gmail 的许可使用此功能 这是代码

import smtplib 
 
 
content='Hello I am just checking email.' 
mail=smtplib.SMTP('smtp.gmail.com',587) 
mail.ehlo() 
mail.starttls() 
mail.login('My email','Mypassword') 
mail.send('From email','destiation password',content) 
mail.close() 

这段代码抛出这个错误 TypeError: send() 接受 2 个位置参数,但给出了 4 个

请修复此错误。

请您参考如下方法:

sendmail 是你应该使用的:

smtplib.SMTP.sendmail(self, from_addr, to_addrs, msg, mail_options=[], rcpt_options=[]) 

This command performs an entire mail transaction.

The arguments are:

- from_addr    : The address sending this mail. 
- to_addrs     : A list of addresses to send this mail to.  A bare 
                 string will be treated as a list with 1 address. 
- msg          : The message to send. 
- mail_options : List of ESMTP options (such as 8bitmime) for the 
                 mail command. 
- rcpt_options : List of ESMTP options (such as DSN commands) for 
                 all the rcpt commands.