是的,您可以使用Google API发送电子邮件,即使您不使用G Suite。下面是一个使用Google SMTP服务器发送电子邮件的示例代码:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# 设置发件人和收件人
sender_email = 'your_email@gmail.com'
receiver_email = 'recipient_email@example.com'
# 创建一个带有文本和HTML内容的MIMEMultipart对象
message = MIMEMultipart("alternative")
message["Subject"] = "邮件主题"
message["From"] = sender_email
message["To"] = receiver_email
# 添加纯文本内容
text = "这是邮件的纯文本内容"
plain_text = MIMEText(text, "plain")
message.attach(plain_text)
# 添加HTML内容
html = "这是邮件的HTML内容
"
html_text = MIMEText(html, "html")
message.attach(html_text)
# 连接到Google SMTP服务器
smtp_server = "smtp.gmail.com"
smtp_port = 587
smtp_username = sender_email
smtp_password = "your_password"
smtp_connection = smtplib.SMTP(smtp_server, smtp_port)
smtp_connection.starttls()
smtp_connection.login(smtp_username, smtp_password)
# 发送邮件
smtp_connection.sendmail(sender_email, receiver_email, message.as_string())
# 关闭连接
smtp_connection.quit()
请确保替换示例代码中的your_email@gmail.com
,recipient_email@example.com
和your_password
为您自己的信息。
请注意,使用此方法发送大量邮件可能会受到Google的限制。如果您计划发送大量邮件,请考虑使用专业的邮件传输服务。