使用 python 发送电子邮件时如何解决身份验证错误
how to solve authentication error when sending email with python
我正在尝试使用 python 使用 gmail 帐户发送电子邮件,但我正在验证 error.I 已验证电子邮件地址和密码,但没有 problem.I甚至在 gmail 中启用不太安全的应用程序
from email.message import EmailMessage
import os
import smtplib
context = ssl.create_default_context()
with smtplib.SMTP_SSL('smtp.gmail.com',465, context = context) as
smtp:
smtp.login('monitorweight100@gmail.com','monitor weight')
msg = EmailMessage()
msg['Subject'] = 'confirm weight'
msg['From'] = 'monitorweight100@gmail.com'
msg['To'] = 'helloworld@gmail.com'
msg.set_content('Detected recent activity')
msg.add_alternative("""\
<!DOCTYPE html>
<html>
<head>
<link href="https://fonts.googleapis.com/css?
family=Niramit" rel="stylesheet">
</head>
<body style="margin:0;">
<h1>Did you recently weigh yourself?</h1>
<img src="https://www.google.com/url?sa=i&rct=j&q=&esrc=s&source=images&cd=&ved=2ahUKEwiOrNW1xPfkAhV57OAKHcZoDKAQjRx6BAgBEAQ&url=https%3A%2F%2Fwww.iconfinder.com%2Ficons%2F3776312%2Fcontrol_diet_healthcare_medical_examination_monitor_scale_weight_icon&psig=AOvVaw2j4tjnCN0wRNR_voOfWKMZ&ust=1569898092735783" alt= "monitorweight" width="900" height="300">
<p>Dear User</p>
<p>We found some weight that was from your wii
balance board,was it you?</p>
<p>Please confirm that it was you by clicking
on the link below</p>
<button><a href="https://www.google.com">Click
to confirm</button>
</body>
</html>
""",subtype='html')
smtp.connect('smtp.gmail.com',465)
smtp.ehlo()
smtp.sendmail('monitorweight100@gmail.com','helloworld@gmail.com',msg)
smtp.quit()
Traceback (most recent call last):
File "/home/pi/smart-scale/mailtest.py", line 36, in <module>
smtp.sendmail('monitorweight100@gmail.com','helloworld@gmail.com',msg)
File "/usr/lib/python3.5/smtplib.py", line 862, in sendmail
raise SMTPSenderRefused(code, resp, from_addr)
smtplib.SMTPSenderRefused: (530, b'5.5.1 Authentication Required. Learn more at\n5.5.1 https://support.google.com/mail/?p=WantAuthError h125sm29106842wmf.31 - gsmtp', 'monitorweight100@gmail.com')
您正在使用 monitorweight@gmail.com 登录并尝试将 From 设置为另一个,monitorweight100@gmail.com.
保持不变 & 问题应该得到解决。
顺便说一句 msg 足以满足 smtp.sendmali().
试试这个..
msg['From'] = 'monitorweight@gmail.com'
# other codes...
smtp.semdmail(msg)
我正在尝试使用 python 使用 gmail 帐户发送电子邮件,但我正在验证 error.I 已验证电子邮件地址和密码,但没有 problem.I甚至在 gmail 中启用不太安全的应用程序
from email.message import EmailMessage
import os
import smtplib
context = ssl.create_default_context()
with smtplib.SMTP_SSL('smtp.gmail.com',465, context = context) as
smtp:
smtp.login('monitorweight100@gmail.com','monitor weight')
msg = EmailMessage()
msg['Subject'] = 'confirm weight'
msg['From'] = 'monitorweight100@gmail.com'
msg['To'] = 'helloworld@gmail.com'
msg.set_content('Detected recent activity')
msg.add_alternative("""\
<!DOCTYPE html>
<html>
<head>
<link href="https://fonts.googleapis.com/css?
family=Niramit" rel="stylesheet">
</head>
<body style="margin:0;">
<h1>Did you recently weigh yourself?</h1>
<img src="https://www.google.com/url?sa=i&rct=j&q=&esrc=s&source=images&cd=&ved=2ahUKEwiOrNW1xPfkAhV57OAKHcZoDKAQjRx6BAgBEAQ&url=https%3A%2F%2Fwww.iconfinder.com%2Ficons%2F3776312%2Fcontrol_diet_healthcare_medical_examination_monitor_scale_weight_icon&psig=AOvVaw2j4tjnCN0wRNR_voOfWKMZ&ust=1569898092735783" alt= "monitorweight" width="900" height="300">
<p>Dear User</p>
<p>We found some weight that was from your wii
balance board,was it you?</p>
<p>Please confirm that it was you by clicking
on the link below</p>
<button><a href="https://www.google.com">Click
to confirm</button>
</body>
</html>
""",subtype='html')
smtp.connect('smtp.gmail.com',465)
smtp.ehlo()
smtp.sendmail('monitorweight100@gmail.com','helloworld@gmail.com',msg)
smtp.quit()
Traceback (most recent call last):
File "/home/pi/smart-scale/mailtest.py", line 36, in <module>
smtp.sendmail('monitorweight100@gmail.com','helloworld@gmail.com',msg)
File "/usr/lib/python3.5/smtplib.py", line 862, in sendmail
raise SMTPSenderRefused(code, resp, from_addr)
smtplib.SMTPSenderRefused: (530, b'5.5.1 Authentication Required. Learn more at\n5.5.1 https://support.google.com/mail/?p=WantAuthError h125sm29106842wmf.31 - gsmtp', 'monitorweight100@gmail.com')
您正在使用 monitorweight@gmail.com 登录并尝试将 From 设置为另一个,monitorweight100@gmail.com.
保持不变 & 问题应该得到解决。
顺便说一句 msg 足以满足 smtp.sendmali().
试试这个..
msg['From'] = 'monitorweight@gmail.com'
# other codes...
smtp.semdmail(msg)