使用 smtplib 发送电子邮件
我有以下代码,成功使用 Gmail 地址发送电子邮件。但是当我尝试使用 Gmail 以外的电子邮件帐户(域电子邮件之一)时。它给了我套接字错误。我需要改变什么吗?
def sendEmail(userName, password, subject, content, toEmail, fromEmail):
print 'Sending email to: %s' % toEmail
SMTPserver = 'smtp.gmail.com'
sender = fromEmail
destination = [toEmail]
USERNAME = userName
PASSWORD = password
text_subtype = 'plain'
try:
content = content
subject = subject
msg = MIMEText(content, text_subtype)
msg['Subject'] = subject
msg['From'] = sender
conn = SMTP(SMTPserver, 587)
conn.ehlo()
conn.starttls()
conn.ehlo()
conn.login(USERNAME, PASSWORD)
try:
conn.sendmail(sender, destination, msg.as_string())
print 'Email sent successfully.'
finally:
conn.close()
except Exception, exc:
raise exc
我正在使用的电子邮件是 [电子邮件受保护] .我还尝试将
SMTPserver = 'smtp.gmail.com'
更新为 SMTPserver = 'smtpout.secureserver.net'
我域的 smptp,但它也不起作用。请帮忙。
I have following code which successfully sent email using Gmail address. But when i tried to use email account other than Gmail which is one of domain email. It gaves me socket error. Do i need to change something?
def sendEmail(userName, password, subject, content, toEmail, fromEmail):
print 'Sending email to: %s' % toEmail
SMTPserver = 'smtp.gmail.com'
sender = fromEmail
destination = [toEmail]
USERNAME = userName
PASSWORD = password
text_subtype = 'plain'
try:
content = content
subject = subject
msg = MIMEText(content, text_subtype)
msg['Subject'] = subject
msg['From'] = sender
conn = SMTP(SMTPserver, 587)
conn.ehlo()
conn.starttls()
conn.ehlo()
conn.login(USERNAME, PASSWORD)
try:
conn.sendmail(sender, destination, msg.as_string())
print 'Email sent successfully.'
finally:
conn.close()
except Exception, exc:
raise exc
The email which i am using is [email protected]
. I also tried updating SMTPserver = 'smtp.gmail.com'
to SMTPserver = 'smtpout.secureserver.net'
the smptp of my domain but it also did not work. Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您使用
SMTPserver='smtp.gmail.com'
尝试端口号为 465 时,它可能会工作。When you use
SMTPserver='smtp.gmail.com'
try port number as 465, it may work.可能您还必须根据您的电子邮件服务器更改
conn = SMTP(SMTPserver, 587)
端口。May be You have to change
conn = SMTP(SMTPserver, 587)
port also with respective to your email server.