1

更新弄清楚为什么,gmail在端口465和587上提供smtp服务器,465是SSL,587是TLS,ESMTP想要TLS,这就是为什么它不能与465,ssl服务器一起使用......我想(我知道作品我只是不确定为什么如果有人能解释为什么请这样做:))


我正在尝试使用 SMTP 和 Twisted 发送电子邮件,但没有任何反应,没有错误,没有输出,也没有电子邮件.....

输出

MacBookPro:EmailSender User$ python send_mime_email.py 
What is your email address? [REMOVED]@gmail.com
What is your username (typically same as email address)? [REMOVED]@gmail.com
What is your password? [REMOVED]
What is your smtp server details? smtp.gmail.com:465
-------Email Details-------
Subject: Hello
Body: Hi, how are you?
Attachment: FTP_Commands.txt
-------Logging-------
2013-01-20 20:43:44-0500 [-] Log opened.
2013-01-20 20:43:44-0500 [-] Creating email
2013-01-20 20:43:44-0500 [-] Created Message.
2013-01-20 20:43:44-0500 [-] Starting factory <twisted.mail.smtp.ESMTPSenderFactory instance at 0xa3c508>
2013-01-20 20:43:44-0500 [-] Sending Email
2013-01-20 20:53:44-0500 [ESMTPSender,client] SMTP Client retrying server. Retry: 5
^C2013-01-20 20:54:47-0500 [-] Received SIGINT, shutting down.
2013-01-20 20:54:47-0500 [ESMTPSender,client] SMTP Client retrying server. Retry: 4
2013-01-20 20:54:47-0500 [-] Main loop terminated.

代码

import sys
import email
import email.mime.application

import sys

from OpenSSL.SSL import SSLv3_METHOD

from twisted.mail.smtp import ESMTPSenderFactory
from twisted.internet.ssl import ClientContextFactory
from twisted.internet.defer import Deferred
from twisted.internet import reactor
from twisted.python import log

try:
    from cStringIO import cStringIO as StringIO
except ImportError:
    from StringIO import StringIO


def create_email(address, subject, body, data):
    print "Creating email"
    # email block

    # text body
    msg = email.mime.Multipart.MIMEMultipart()
    msg['Subject'] = subject

    # send it to ourselves to make it simple
    msg['From'] = address
    msg['To'] = address

    # body
    body = email.mime.Text.MIMEText(body)
    msg.attach(body)
    att = email.mime.application.MIMEApplication(data, _subtype="binary")
    att.add_header('Content-Disposition','attachment',filename="data.bin")
    msg.attach(att)

    print "Created Message."

    # Create a context factory which only allows SSLv3 and does not verify
    # the peer's certificate.
    return str(msg)

def send_email(smtp_server, smtp_port, username, password, from_, to, msg):


    contextFactory = ClientContextFactory()
    contextFactory.method = SSLv3_METHOD

    resultDeferred = Deferred()

    mime_obj = StringIO(str(msg))

    senderFactory = ESMTPSenderFactory(
                                       username,
                                       password,
                                       from_,
                                       to,
                                       mime_obj,
                                       resultDeferred,
                                       contextFactory=contextFactory)

    reactor.connectTCP(smtp_server, smtp_port, senderFactory)
    print "Sending Email"
    return resultDeferred



if __name__ == '__main__':
    email_address = raw_input("What is your email address? ")
    username      = raw_input("What is your username (typically same as email address)? ")
    password      = raw_input("What is your password? ")
    sd            = raw_input("What is your smtp server details? ")
    ss, sp        = sd.split(":")
    sp            = int(sp)
    print "-------Email Details-------"
    subject = raw_input("Subject: ")
    body    = raw_input("Body: ")
    attachment_file = raw_input("Attachment: ")
    o = open(attachment_file, "rb")
    data = o.read()
    o.close()
    print "-------Logging-------"

    log.startLogging(sys.stdout)
    email_data = create_email(email_address, subject, body, data)
    send_email(ss, sp, username, password, email_address, email_address, email_data)
    reactor.run()
4

2 回答 2

1

我认为这里有一些棘手的、令人困惑的术语会使事情变得混乱。“SSL”和“TLS”实际上最常用作同一事物的不同名称。对词汇稍微严格一些(尽管在技术上可能仍然不是绝对正确的),有一个名为“SSLv2”的协议和另一个名为“SSLv3”的协议(对 SSLv2 的更新)和另一个名为“TLSv1”的协议(对 SSLv3 的更新)。

许多人说“SSL”是指这三件事中的任何一个,或者可能是全部。

此外,许多人说“TLS”是指这三件事中的任何一件,或者可能是全部!

此外,还有一个单独的概念,即在已用于交换某些(非加密)数据的已建立连接上开始使用这些协议之一。有时就是人们说“TLS”时的意思,这就是您在问题顶部说“TLS”时的意思。

gmail 在端口 465 上提供的是一个强制 SSL(SSLv2、SSLv3 或 TLSv1,我没有检查过)的 SMTP 服务器,它从连接的最开始 - 即在交换任何 SMTP 流量之前开始。

gmail 在端口 587 上提供的是一个没有任何 SSL(SSLv2、SSLv3 或 TLSv1 中的任何一个)的 SMTP 服务器,但它支持 SMTP 功能,用于协商开关以在稍后的某个时间打开 SSL。

当然,就客户端而言,这里的区别在于他们是否应该在 TCP 连接建立后立即启动 SSL(SSLv2、SSLv3 或 TLSv1),或者他们是否应该开始使用 SMTP,然后可能稍后协商一个“升级”到 SSL(SSLv2、SSLv3 或 TLSv1)。

如果客户端和服务器不同意这里究竟应该发生什么,结果可能是“挂起”连接 - 例如,服务器等待客户端启动 SSL 握手,客户端等待服务器向其发送 SMTP问候。

于 2013-01-22T21:44:17.137 回答
0

解决了问题是我使用的是 smtp.gmail.com:465 而不是 smtp.gmail.com:587。

于 2013-01-21T03:55:45.947 回答