使用smtplib发送邮件
我有一段代码,可以成功用Gmail地址发送邮件。但是当我尝试用其他邮箱,比如一个域名邮箱时,就出现了socket错误。我需要改些什么吗?
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
我使用的邮箱是 domains@smoothplus.com
。我还尝试把 SMTPserver = 'smtp.gmail.com'
改成 SMTPserver = 'smtpout.secureserver.net'
,这是我域名的SMTP服务器,但还是不行。请帮帮我。
2 个回答
4
当你使用 SMTPserver='smtp.gmail.com'
时,可以试试端口号465,可能会有效果。
3
你可能需要根据你的邮箱服务器,修改一下这行代码:conn = SMTP(SMTPserver, 587)
,把端口号也换成合适的。