出现投递失败可能是由于Gsuite账户的SMTP设置不正确或被限制导致的。以下是一个使用Gmail账户发送邮件的示例代码,可以作为解决方法:
import smtplib
from email.mime.text import MIMEText
def send_email(subject, message, sender_email, receiver_email, password):
# 设置邮件内容
msg = MIMEText(message)
msg['Subject'] = subject
msg['From'] = sender_email
msg['To'] = receiver_email
# 连接SMTP服务器
with smtplib.SMTP('smtp.gmail.com', 587) as server:
server.starttls()
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, msg.as_string())
# 示例用法
subject = "测试邮件"
message = "这是一封测试邮件。"
sender_email = "your_gmail_account@gmail.com"
receiver_email = "recipient_email@example.com"
password = "your_gmail_password"
send_email(subject, message, sender_email, receiver_email, password)
请确保你已经将your_gmail_account@gmail.com
替换为你的Gmail账户,recipient_email@example.com
替换为接收邮件的邮箱地址,以及your_gmail_password
替换为你的Gmail账户密码。
如果你仍然无法使用Gsuite账户发送邮件,建议检查Gsuite账户的SMTP设置,并确保已经启用SMTP服务。另外,还可以尝试使用Gsuite账户的授权码(而不是密码)作为password
参数来进行登录。