socket实验——stmp简单邮件代理

Q&A

1、email应用的组成?
邮件客户端、邮件服务器、SMTP协议

2、为什么email要使用客户端服务器的结构,而不是直接在用户间建立连接?
想象自己不在线和对方不在线的情况

3、SMTP协议,简单邮件传输协议
传输层协议:TCP
端口:25
传输三个阶段:握手,建立连接,关闭
交互模式:命令相应
Email消息只能包含7位ASCII码,为了支持发送pdf等格式,使用了MIME(多媒体邮件扩展),在邮件头部添加额外的行来声明有多媒体内容,编码格式等内容
利用回车换行确定消息的结束

4、SMTP的格式:
HELO、MAILFROM、RCPT TO、DATA(邮件内容) 、QUIT

5、pop协议,邮件访问协议,从服务器上获取邮件使用的协议
命令相应
两种模式:下载并删除模式,下载并保持模式
POP3是无状态的
此外还有IMAP协议,所有消息保存在服务器,允许用户利用文件夹组织消息,支持跨对话等,有状态

编写实现smtp客户端

这个是《计算机网络——自顶向下方法》的socket实验,要求手动发送smtp规定的几个组成部分,还是比较简单,但在实现的时候要多一个验证的步骤,遇到了一些问题。
配置的是服务器是smtp.163.com,端口25
代码:

from socket import *
import base64
msg_from='*******'                                 #发送方邮箱
username = base64.b64encode(msg_from.encode()).decode()
password = base64.b64encode('*******'.encode()).decode()
msg = "\r\n I love computer networks!"
endmsg = "\r\n.\r\n"
# Choose a mail server 
mailserver = 'smtp.163.com'
# Create socket called clientSocket and establish a TCP connection with mailserver

clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((mailserver,25))
recv = clientSocket.recv(2048)
recv = recv.decode()
print (recv)
if recv[:3] != '220':
    print ('220 reply not received from server.')

# Send HELO command and print server response.
heloCommand = 'HELO Alice\r\n'
clientSocket.send(heloCommand.encode())
recv1 = clientSocket.recv(2048)
recv1 = recv1.decode()
print (recv1)
if recv1[:3] != '250':
    print ('250 reply not received from server.')

#AUTH
clientSocket.sendall('AUTH LOGIN\r\n'.encode())
recv = clientSocket.recv(2048).decode()
print(recv)
if (recv[:3] != '334'):
    print('334 reply not received from server')
    
clientSocket.sendall((username + '\r\n').encode())
recv = clientSocket.recv(1024).decode()
print(recv)
if (recv[:3] != '334'):
    print('334 reply not received from server')

clientSocket.sendall((password + '\r\n').encode())
recv = clientSocket.recv(1024).decode()
print(recv)
if (recv[:3] != '235'):
    print('235 reply not received from server')


# Send MAIL FROM command and print server response.

clientSocket.send('MAIL FROM:<**********>\r\n'.encode())
mailfromMessage = clientSocket.recv(1024)
print(mailfromMessage)

# Send RCPT TO command and print server response.

clientSocket.send('RCPT TO:<*********>\r\n'.encode())
RCPTMessage = clientSocket.recv(1024)
print(RCPTMessage)

# Send DATA command and print server response.

clientSocket.send('DATA\r\n'.encode())
DATAMessage = clientSocket.recv(1024)
print(DATAMessage)

# Send message data.

clientSocket.send(msg.encode())

# Message ends with a single period.

clientSocket.send(endmsg.encode())

# Send QUIT command and get server response.

clientSocket.send('QUIT\r\n'.encode())
quitMessage = clientSocket.recv(1024)
print(quitMessage)

收到服务器发送回的消息:
在这里插入图片描述
查看邮箱确实收到了邮件

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Ubuntu 20.04是一款非常流行的Linux操作系统,它可以非常方便地配置邮件服务器SMTP和IMAP。 首先,我们需要安装邮件服务器软件。Ubuntu 20.04系统中默认安装了Postfix邮件服务器,我们只需要打开终端输入以下命令进行安装即可: sudo apt-get update sudo apt-get install postfix 接下来,我们需要配置Postfix邮件服务器。我们可以在/etc/postfix/main.cf文件中进行配置。以下是一个简单的配置示例: myhostname = mail.example.com mydestination = example.com, mail.example.com, localhost.example.com, localhost mynetworks = 127.0.0.0/8, [::1]/128 smtpd_banner = $myhostname ESMTP smtpd_relay_restrictions = permit_mynetworks, permit_sasl_authenticated, defer_unauth_destination smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd smtp_sasl_security_options = noanonymous 在上述示例配置中,邮件服务器的主机名是mail.example.com,邮件服务器可以发送电子邮件到example.com,mail.example.com和本地主机名localhost.example.com。邮件服务器还可以忽略其他来源的Emails。 我们还需要配置SMTP和IMAP认证。可以使用Dovecot软件提供这些服务。我们可以使用以下命令来安装Dovecot: sudo apt-get install dovecot-core dovecot-imapd dovecot-pop3d 接下来我们可以在/etc/dovecot/conf.d/10-auth.conf和/etc/dovecot/conf.d/10-master.conf两个配置文件中进行认证配置。 最后,重启邮件服务器和Dovecot服务,我们就可以通过SMTP和IMAP协议使用邮件服务器发送和接收邮件了! ### 回答2: 配置邮件服务器SMTP和IMAP是一个相对复杂的过程,需要正确地设置各种参数,以确保基于Ubuntu 20.04的邮件服务器可以顺利地工作。下面是一些步骤供参考: 1. 安装邮件服务器软件:Ubuntu 20.04中默认安装了Postfix邮件服务器软件,运行以下命令进行安装: sudo apt-get update sudo apt-get install postfix 2. 配置SMTP服务器:默认情况下,Postfix配置为仅在本地主机上接受邮件。要允许其他计算机发送邮件,请编辑/etc/postfix/main.cf,在“myhostname”行下添加以下行: mydestination = example.com, localhost.example.com, localhost 其中“example.com”应替换为您的域名。保存配置更改并重新启动Postfix服务: sudo service postfix restart 3. 配置IMAP服务器:Ubuntu 20.04默认安装Dovecot IMAP服务器软件。编辑/etc/dovecot/conf.d/10-auth.conf文件,在“disable_plaintext_auth”行下添加以下行: auth_mechanisms = plain login 接下来,在/etc/dovecot/conf.d/10-master.conf文件中找到以下行,将其注释掉: #unix_listener /var/spool/postfix/private/auth { #mode = 0660 #user = postfix #group = postfix #} 最后,重新启动Dovecot服务: sudo service dovecot restart 4. 测试SMTP和IMAP服务器:你可以使用常见邮件客户端,如Outlook或Thunderbird,登录到你的SMTP和IMAP服务器并发送/接收邮件来测试服务器的配置。 以上是配置Ubuntu 20.04的邮件服务器SMTP和IMAP的一些基本步骤。这个过程可以有很多的细节,需要逐一处理以确保正确地连接和使用邮件服务器。 ### 回答3: Ubuntu20.04作为一款免费的开源操作系统,具有众多功能强大的特性,配置邮件服务器 stmp imap也是非常方便的。下面介绍一下如何在Ubuntu20.04上配置邮件服务器。 首先,我们需要安装基本的邮件服务软件,其中包括 Postfix、Dovecot、SASL 和 AnonTLS 等。我们可以使用以下命令来完成软件的安装: sudo apt-get update sudo apt-get install postfix dovecot-core dovecot-imapd dovecot-pop3d sasl2-bin libsasl2-modules-ldap openssl-blacklist 安装完毕后,我们需要通过以下命令来配置 Postfix: sudo nano /etc/postfix/main.cf 在该文件中,我们需要做如下配置: myhostname = your.mail.server.com mydomain = your.mail.server.com myorigin = your.mail.server.com inet_interfaces = all mydestination = your.mail.server.com, localhost smtpd_banner = $myhostname ESMTP 在这些设置中,myhostname、mydomain 和 myorigin 都应该设置为您的邮件服务器的主机名。接下来,我们需要配置 Dovecot,在 /etc/dovecot/dovecot.conf 文件中增加以下设置: protocols = imap pop3 auth_mechanisms = plain login disable_plaintext_auth = no ssl = no pop3_uidl_format = %08Xu%08Xv 在这些设置中,protocols 配置了 Dovecot 所支持的客户端访问协议,auth_mechanisms 配置了认证方式,disable_plaintext_auth 设置为 no 表示能够使用明文密码认证登录。pop3_uidl_format 是用于 POP3 下载时保证每个邮件的唯一性的设置。 此时我们已完成 SMTP 和 IMAP 邮件服务器的基本配置。接下来我们需要设置邮件用户的身份认证信息。具体的说明如下: 1. 在 /etc/saslpasswd2 文件中新建帐户信息,每行只能记录一个帐户,格式为“username: password”。 2. 我们需要设置 saslauthd 的配置参数,在 /etc/default/saslauthd 文件中设置以下参数: START=yes AUTHMECH=shadow MECHANISMS="ldap plain" ARGS="-c -m /var/run/saslauthd -O /etc/saslauthd.conf" 3. 根据上述命令执行后,我们需要在 /etc/saslauthd.conf 文件中设置 SASL 认证的基本参数: verbosity: 0 mech_options: <empty> pwcheck_method: saslauthd mech_list: plain login 4. 最后,我们需要设置用户帐户在 LDAP 数据库中的信息,在 /etc/postfix/sasl/smtpd.conf 文件中增加以下设置: pwcheck_method: saslauthd mech_list: plain login log_level: 4 auxprop_plugin: sasldb mech_disable: CRAM-MD5 DIGEST-MD5 至此,我们在 Ubuntu20.04 上已经设置好了邮件服务器,我们可以通过 IMAP、POP3、SMTP 等协议访问邮件服务器,获取邮箱中存储的邮件信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值