使用 smtplib 发送电子邮件

发布于 2024-12-11 01:53:41 字数 1208 浏览 0 评论 0原文

我有以下代码,成功使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不在是我 2024-12-18 01:53:41

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

When you use SMTPserver='smtp.gmail.com' try port number as 465, it may work.

撞了怀 2024-12-18 01:53:41

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

May be You have to change conn = SMTP(SMTPserver, 587) port also with respective to your email server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文