2024-04-26 21:35:24 发布
网友
import smtplib server = smtplib.SMTP('smtp.gmail.com', 587) server.ehlo() server.starttls() server.login("******@gmail.com", "*******") msg = "Hello!" server.sendmail("rajesh.debugs@gmail.com", "rjucsm@gmail.com", msg)
输出:
可能重复:SMTPAuthenticationError when sending mail using gmail and python
您需要查看https://support.google.com/accounts/answer/6010255?hl=en并为您的邮件帐户启用https://myaccount.google.com/lesssecureapps。在
您需要将email.mime与smtp一起使用。在下面试试
email.mime
smtp
from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.image import MIMEImage import smtplib strFrom = 'xyz.uvw@gmail.com' strTo = ['abc.efg@gmail.com','hij.lmn@gmail.com] attachment = '<path to attachment if any>' msgRoot = MIMEMultipart('related') msgRoot['Subject'] = 'TEST' msgRoot['From'] = strFrom msgRoot['To'] = ", ".join(strTo) msgRoot.preamble = 'This is a multi-part message in MIME format.' msgAlternative = MIMEMultipart('alternative') msgRoot.attach(msgAlternative) msgText = MIMEText('This is the alternative plain text message.') msgAlternative.attach(msgText) msgText = MIMEText('<b>Summary</b>, 'html') msgAlternative.attach(msgText) fp = open(attachment, 'rb') msgImage = MIMEImage(fp.read()) fp.close() msgImage.add_header('Content-ID', '<image1>') msgRoot.attach(msgImage) smtp = smtplib.SMTP("YOUREMAILHOST", 25, timeout=120) smtp.sendmail(strFrom, strTo, msgRoot.as_string()) smtp.close()
如果您没有设置与smtplib一起使用,那么我建议使用SendGrid发送电子邮件(如果您每天发送100封电子邮件,则免费)。在
smtplib
import sendgrid from sendgrid.helpers.mail import * sg = sendgrid.SendGridAPIClient(apikey=sendgrid_api_key) from_email = Email("test@example.com") to_email = Email("test@example.com") subject = "Sending with SendGrid is Fun" content = Content("text/plain", "content") mail = Mail(from_email, subject, to_email, content) response = sg.client.mail.send.post(request_body=mail.get()) print(response.status_code)
可能重复:SMTPAuthenticationError when sending mail using gmail and python
您需要查看https://support.google.com/accounts/answer/6010255?hl=en并为您的邮件帐户启用https://myaccount.google.com/lesssecureapps。在
您需要将
email.mime
与smtp
一起使用。在下面试试如果您没有设置与
smtplib
一起使用,那么我建议使用SendGrid发送电子邮件(如果您每天发送100封电子邮件,则免费)。在相关问题 更多 >
编程相关推荐