您可以使用以下Python代码示例来通过Gmail SMTP服务器发送账户验证代码:
import smtplib
from email.mime.text import MIMEText
def send_verification_code(email, code):
sender_email = "your_email@gmail.com"
sender_password = "your_password"
subject = "Account Verification Code"
message = f"Your verification code is: {code}"
msg = MIMEText(message)
msg['Subject'] = subject
msg['From'] = sender_email
msg['To'] = email
try:
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(sender_email, sender_password)
server.sendmail(sender_email, email, msg.as_string())
server.quit()
print("Verification code sent successfully!")
except Exception as e:
print("Error sending verification code:", str(e))
# 调用函数来发送验证代码
send_verification_code("recipient_email@example.com", "123456")
请确保在代码中替换以下值:
your_email@gmail.com
:您的Gmail邮箱地址。your_password
:您的Gmail邮箱密码。recipient_email@example.com
:您要发送验证代码的收件人邮箱地址。123456
:您要发送的具体验证代码。请注意,为了能够成功发送邮件,您需要启用Gmail帐户中的SMTP访问权限。您可以在Gmail设置中的“账户和导入”部分中找到此选项。
上一篇:本地主机SSL问题