菜鷄日記——Computer Networking: Exp.2 Implement an Email Client with SMTP on Windows

Targets

  1. Learn more about SMTP.
  2. Write an email client based on SMTP.

Steps

  1. Implement the email client with a programming language.
  2. Test your email client by sending and receiving mails.

Result

Step 1: Program in Python. Implement a simplefied QQ email client.

emailclient.py

from pwreader import input_pw
import receiver as rcv
import sender as sdr


# Welcome words.
print('Welcome to Wyatt\'s QQ email client!\n')


# Read username and password of the email.
username = input('Username: ')
password = input_pw('Password: ')   # a password input method that can replace password characters with asterisks (*)


# Show menu and realize function that user have chosen
while True:
    print('\nOption 0: Exit.')      # this client provides 3 options for user
    print('Option 1: Read emails.')
    print('Option 2: Send email.')
    opt = int(input('\nYour chosen: '))

    if opt == 0:
        break
    if opt == 1:
        rcv.receive(username, password)
    if opt == 2:
        sdr.send(username, password)

 pwreader.py

# Read password and replace password characters with asterisks (*).
def input_pw(text):
    import msvcrt

    print(text, end='', flush=True)

    chars = []
    while True:
        new_char = msvcrt.getch()

        if new_char in b'\r\n':
            msvcrt.putch(b'\n')
            break
        elif new_char == b'\b':
            if chars:
                chars.pop()
                msvcrt.putch(b'\b')
                msvcrt.putch(b' ')
                msvcrt.putch(b'\b')
        else:
            chars.append(new_char)
            msvcrt.putch(b'*')

    return (b''.join(chars)).decode()

 receiver.py

import imaplib
import email


# Get all mails of certain user from QQ email.
def receive(username, password):
    # Establish connection with email server using IMAP and SSL through port 993.
    connection = imaplib.IMAP4_SSL('imap.qq.com', 993)

    # Login.
    try:
        connection.login(username, password)    # caution: password here is the authorized code of user's QQ email
        print('\nSuccessful login.')
    except:
        print('\nFail to login.')
        return

    # Get mails from email server and save them in eml files.
    connection.select()     # select a mailbox
    tpy, mails = connection.search('ALL')   # search mailbox for all mails
    for num_str in mails[0].split():    # mails[0] is a string recording blank separated mail indexes
        try:
            typ, mail = connection.fetch(num_str, '(UID BODY.PEEK[])')
            content = mail[0][1]

            # Store contents of a mail in an eml file.
            filename = num_str.decode() + '.eml'
            file = open(filename, 'wb')
            file.write(content)
            file.close()
        except:
            print('\nSomething wrong with mail No. ' + num_str + '.')
    print('\nReception completed, you can surf them in eml files.')

    # Disconnect.
    connection.close()

 sender.py

import smtplib
from mesgreader import input_mesg
from email.mime.text import MIMEText
from email.header import Header


# Write and send an email to a certain email address.
def send(username, password):
    # Set up connection with email server using SMTP and SSL through port 465.
    connection = smtplib.SMTP_SSL('smtp.qq.com', 465)

    # Login.
    try:
        connection.login(username, password)
        print('\nSuccessful login')
    except smtplib.SMTPException as exp:
        print('\nFail to login:', exp)
        return

    # Indicate user to write an email.
    destination = input('\nDestination email address: ')
    subject = input('Email subject: ')
    message = input_mesg('Content (ends with string \'-end\'): ', endflag='-end')

    # Complete the SMTP message.
    message = MIMEText(message, 'plain', 'utf-8')
    message['From'] = Header(username)      # set header contents
    message['To'] = Header(destination)
    message['Subject'] = Header(subject)

    # Send out.
    try:
        connection.sendmail(username, destination, message.as_string())
        print('\nSuccessful send')
    except smtplib.SMTPException as exp:
        print('\nFail to send:', exp)
        return

    # Disconnect.
    connection.close()

receiver.py

import imaplib
import email


# Get all mails of certain user from QQ email.
def receive(username, password):
    # Establish connection with email server using IMAP and SSL through port 993.
    connection = imaplib.IMAP4_SSL('imap.qq.com', 993)

    # Login.
    try:
        connection.login(username, password)    # caution: password here is the authorized code of user's QQ email
        print('\nSuccessful login.')
    except:
        print('\nFail to login.')
        return

    # Get mails from email server and save them in eml files.
    connection.select()     # select a mailbox
    tpy, mails = connection.search('ALL')   # search mailbox for all mails
    for num_str in mails[0].split():    # mails[0] is a string recording blank separated mail indexes
        try:
            typ, mail = connection.fetch(num_str, '(UID BODY.PEEK[])')
            content = mail[0][1]

            # Store contents of a mail in an eml file.
            filename = num_str.decode() + '.eml'
            file = open(filename, 'wb')
            file.write(content)
            file.close()
        except:
            print('\nSomething wrong with mail No. ' + num_str + '.')
    print('\nReception completed, you can surf them in eml files.')

    # Disconnect.
    connection.close()

 mesgreader.py

import msvcrt


# Read email body.
def input_mesg(text, endflag='\r'):
    print(text)

    message = ''
    while True:
        line = input()
        if line == endflag:
            break
        else:
            message += '\n' + line

    return message

Step 2: Test clint.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值