我正在尝试从我的django应用程序发送电子邮件但无法这样做
# Running the shell
python manage.py shell
from django.core.mail import send_mail
send_mail('Test','This is a test', 'saikat.dasgupta2006@gmail.com',
['erdasgupta@gmail.com])
error: [Errno 10060] A connection attempt failed because the
connected party didnot properly respond after a period of time, or established connection failed because connected host has failed to respond
Updated settings.py file to include following
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'saikat.dasgupta2006@gmail.com'
EMAIL_HOST_PASSWORD = 'afchbhnaysjthdow' (application specific password)
EMAIL_PORT = 587
EMAIL_USE_TLS = True
答案 0 :(得分:4)
import smtplib
def send_mail(from_addr, to_addr_list, subject, email_body):
SMTP_SESSION = smtplib.SMTP('smtp.gmail.com', 587)
SMTP_SESSION.ehlo()
SMTP_SESSION.starttls()
SMTP_SESSION.login(settings.GMAIL_EMAIL, 'liomessi')
headers = "\r\n".join(["from: " + 'My Test Mail',
"subject: " + subject,
"mime-version: 1.0",
"content-type: text/html"])
# body_of_email can be plaintext or html!
content = headers + "\r\n\r\n" + email_body
SMTP_SESSION.sendmail(from_addr, to_addr_list, content)
send_mail(from_addr = settings.GMAIL_EMAIL, to_addr_list =['abc@gmail.com'], subject = 'Welcome the site', email_body = 'Thanks for joining our site we are glad that you are here')
我也使用django通过以下设置发送邮件,并能够向该人发送邮件。我认为你需要指定内容类型和mime版本。然后你就可以发送邮件了。