繁体   English   中英

使用smtplib发送电子邮件

[英]Sending Email using 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

我使用的电子邮件是domains@smoothplus.com 我还尝试将SMTPserver = 'smtp.gmail.com'更新为我域的SMTPserver = 'smtpout.secureserver.net' ,但它也无法正常工作。 请帮忙。

当您使用SMTPserver='smtp.gmail.com'尝试将端口号设为465时,它可能会起作用。

可能是您还必须更改电子邮件服务器的conn = SMTP(SMTPserver, 587)端口。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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