简体   繁体   中英

Sending mail via smtp in Django

I'm trying to send a simple email using python/django shell and have been strugling with this problem for the last few hours:
in django shell i do the following:

from django.core.mail import send_mail
send_mail('django mail', 'this was sent with django', 'myaddress@gmail.com',['myaddress@gmail.com'], fail_silently=False)

the returned result should be 1, meaning that the mail is sent successfully, but insead, i get this:

    raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (534, b'5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbu9\n5.7.14 0cv5jjPsAITCLvsSIKoDuJcz5I18H7PMX8Nsxz2ajtgAJfxls4wIKIVMUENCrFmoXNHdgM\n5.7.14 NpSKlFYuaGHtwqDodV09jIf_GaDklCUUzJLY7oSJITQqXADDWxYRU7LUbVRFPxwpd2cKzl\n5.7.14 g70grCboTaCtEofq3-5edwoRC0ukZT-z97AgOelTTvSteaEjuf5n7F417VvFFE1hXcBnyg\n5.7.14 n2NWXBFMlV_74532aXU0vguceCC84> Please log in via your web browser and\n5.7.14 then try again.\n5.7.14  Learn more at\n5.7.14  https://support.google.com/mail/answer/78754 2sm2181268wrn.24 - gsmtp')

I read the url suggested in the error, and also a few similar questions on the web, and there were no good it them for me.

also, in the settings.py file, the required code is included:

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'  
EMAIL_HOST_USER = 'me@gmail.com'  
EMAIL_HOST_PASSWORD = 'mypassword'  
EMAIL_PORT = 587

as i mentioned above, the other questions/problems on this site weren't the same. I've already enable the access for less secure apps on my gmail account, and also i have no two step verification.

IIRC to send email using gmail SMTP from third-party apps (your own Django app), you need to enable what they call Less Secure Apps .

You can read more here: https://support.google.com/accounts/answer/6010255

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM