我编写了一个 python 脚本,用于将电子邮件从我的 gmail 帐户发送到我的另一个电子邮件。代码是:
import smtplib
fromaddr = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d3beaa93b4beb2babffdb0bcbe" rel="noreferrer noopener nofollow">[email protected]</a>'
toaddrs = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4f3b200f28222e2623612c2022" rel="noreferrer noopener nofollow">[email protected]</a>'
msg = 'my message'
username = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e38e9aa3848e828a8fcd808c8e" rel="noreferrer noopener nofollow">[email protected]</a>'
password = 'myPass'
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
print("Done")
在发送电子邮件时,我还收到“完成”输出。但问题是:我似乎无法收到电子邮件。它不会显示在收件箱中。我找不到问题:(有人可以帮忙吗?
提前致谢...
最佳答案
看看这个,这对我来说非常有用......
def send_mail(self):
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
gmailUser = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3954405c54585055795e54585055175a5654" rel="noreferrer noopener nofollow">[email protected]</a>'
gmailPassword = 'P@ssw0rd'
recipient = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b5c6d0dbd1c1daf5d2d8d4dcd99bd6dad8" rel="noreferrer noopener nofollow">[email protected]</a>'
message='your message here '
msg = MIMEMultipart()
msg['From'] = gmailUser
msg['To'] = recipient
msg['Subject'] = "Subject of the email"
msg.attach(MIMEText(message))
mailServer = smtplib.SMTP('smtp.gmail.com', 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmailUser, gmailPassword)
mailServer.sendmail(gmailUser, recipient, msg.as_string())
mailServer.close()
关于python - 使用Python使用Gmail发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43666912/