有没有办法找到每个电子邮件提供商的smtp服务器的地址?

时间:2019-08-01 14:09:40

标签: python email smtp

我正在用python编写脚本,我希望用户输入其个人电子邮件地址和密码,输入其他电子邮件详细信息(如下面的代码所示),然后通过他的电子邮件将某个文件发送到他选择的地址。为此,我必须知道smtp服务器地址和端口号才能连接到他的电子邮件提供商。有没有一种方法可以仅使用用户提供的地址来查找其smtp服务器地址和端口(某些服务器可能不需要,某些情况下不需要)? 我的代码是这样的:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import stdiomask

path = path of the file
sender = input("Please enter the sender's address:")
# code for what i am asking here, server is the server's address, portNo is #the port number
pwd = stdiomask.getpass(prompt="Please enter the senders password: ", mask = '*') #to perfect maybe
receiver = input("Please enter the receivers address: ")
subject = input("Please enter the subject that you want to be included in the email: ")
body = input("Please write any message that you want to be written in the email: ")
msg = MIMEMultipart()
msg['From'] = sender
msg['To'] = receiver
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))
attachment = open(path, "rb")
p = MIMEBase('application', 'octet-stream')
p.set_payload(attachment.read())
encoders.encode_base64(p)
p.add_header('Content-Disposition', "attachment; filename = %s" % path.replace(Directory part of the file path which i do not need, ""))
msg.attach(p)
s = smtplib.SMTP(server, portNo)
s.starttls()
s.login(sender, pwd)
text = msg.as_string()
s.sendmail(sender, receiver, text)
s.quit()

2 个答案:

答案 0 :(得分:0)

通常,SMTP具有以下结构。

smtp.gmail.com smtp.domain.com

但是,在少数情况下SMTP不会像域一样。

答案 1 :(得分:0)

https://www.emailsettings.email/search/pop3-imap-smtp-gmail.com-email-settings

此站点允许您查找有关电子邮件服务器的信息,并且它们提供api。 https://emailsettings.firetrust.com/settings?q=test.user@gmail.com

获取该信息,将其转换为JSON,然后查找data['settings'][2]['address']