简体   繁体   English

使用smtplib发送电子邮件

[英]Sending Email using smtplib

I have following code which successfully sent email using Gmail address. 我有以下代码可以使用Gmail地址成功发送电子邮件。 But when i tried to use email account other than Gmail which is one of domain email. 但是,当我尝试使用Gmail以外的其他电子邮件帐户时,这是域电子邮件之一。 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 domains@smoothplus.com . 我使用的电子邮件是domains@smoothplus.com I also tried updating SMTPserver = 'smtp.gmail.com' to SMTPserver = 'smtpout.secureserver.net' the smptp of my domain but it also did not work. 我还尝试将SMTPserver = 'smtp.gmail.com'更新为我域的SMTPserver = 'smtpout.secureserver.net' ,但它也无法正常工作。 Please help. 请帮忙。

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

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

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

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