AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 38919
Accepted
Kyle Brandt
Kyle Brandt
Asked: 2009-07-11 05:55:22 +0800 CST2009-07-11 05:55:22 +0800 CST 2009-07-11 05:55:22 +0800 CST

使用 telnet 或 netcat 发送带有附件的电子邮件

  • 772

我经常使用 telnet 或 netcat 连接 smtp 服务器发送电子邮件作为测试。

有谁知道您将如何使用 telnet 或 netcat 发送电子邮件但也附上文件?可能有更好的方法,但我仍然想知道:-)

我会对使用一点 bash shell 来实现目标的解决方案感到满意,但不想使用任何其他工具......

smtp telnet netcat
  • 9 9 个回答
  • 78219 Views

9 个回答

  • Voted
  1. Best Answer
    Kyle Brandt
    2009-07-11T10:55:09+08:002009-07-11T10:55:09+08:00

    好的,所以以每个人的评论为起点,我想出了这个愚蠢的烂摊子:-) ...

    { 
        sleep 5; 
        echo 'ehlo';
        sleep 3;
        echo 'MAIL FROM:<[email protected]>';
        sleep 3; 
        echo 'RCPT TO: <kyle@test_dest.com>';
        sleep 3;
        echo 'DATA';
        sleep 3;
        echo -e 'To:[email protected]\nMIME-Version: 1.0 (mime-construct 1.9)\nContent-Type: application/zip\nContent-Transfer-Encoding: base64\n\n';
        dd if=/dev/urandom bs=4 count=10 2>/dev/null | openssl base64;
        echo '.';
    } | telnet mx1.testdest.com 25
    
    • 10
  2. Evan Anderson
    2009-07-11T06:02:33+08:002009-07-11T06:02:33+08:00

    伊克。您将不得不对附件进行 base64 编码并创建 MIME 标头。

    与其每次都“即时”生成一条新消息,不如给自己发送一封来自“真实”电子邮件程序的非常简短的示例消息可能更容易(利用编写它的人所做的工作来放置附件)进入正确的编码并创建 MIME 标头)。

    将该消息保存到带有标头的文本文件中(当然,删除传输标头),然后将其修改/复制/粘贴到 telnet 或 netcat 以供将来的会话使用。

    • 8
  3. hayalci
    2009-07-11T11:18:57+08:002009-07-11T11:18:57+08:00

    虽然手动测试 SMTP 服务器是可能且可行的,但使用为此设计的工具会容易得多。

    这篇文章解释了 SWAKS。swaks 专为 smtp 服务器测试而设计。支持附件、身份验证和加密!

    • 6
  4. ravenmeister
    2013-09-19T05:00:11+08:002013-09-19T05:00:11+08:00

    当我在寻找相同的东西时,我偶然发现了这个条目。从这里的遮阳篷和一些额外的研究中,我设法制作了这个脚本。

    #!/bin/sh
    
    # Default reception
    TOEMAIL="[email protected]";
    # Default Subject
    SUBJECT="You got mail - $DATE";
    # Default Contents
    MSGBODY="Hello, this is the default message body";
    # Default Attachment
    #ATTACHMENT="/tmp/testoutput"
    # Default smtp server
    mailserver="smtp.server.ltd"
    mailserverPort="25"
    
    showUsage() {
            echo "$0 -a /file/to/attach [-m /message/file] [-M \"Message string\"] -s \"subject\" -r [email protected]"
            echo
            echo "The attachment (-a) is required, if no attachment is used then rather use sendmail directly."
    }
    
    fappend() {
        echo "$2">>$1;
    }
    DATE=`date`
    
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # This might need correction to work on more places, this is tested at a ubuntu 13.10 machine.  #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    domain=`grep search /etc/resolv.conf | awk '{print $2;}'`
    computer=`hostname`
    user=`whoami`
    FREMAIL="$user@$computer.$domain"
    
    while getopts "M:m:a:s:r:" opt; do
      case $opt in
            s)
              SUBJECT="$OPTARG - $DATE"
              ;;
            r)
              TOEMAIL="$OPTARG"
              ;;
            m)
              MSGBODY=`cat $OPTARG`
              ;;
            M)
              MSGBODY="$OPTARG"
              ;;
            a)
              ATTACHMENT="$OPTARG"
              ;;
            :)
              showUsage
              ;;
            \?)
              showUsage
              ;;
      esac
    done
    
    if [ "$ATTACHMENT" = "" ]; then
            showUsage
            exit 1
    fi
    
    MIMETYPE=`file --mime-type -b $ATTACHMENT`
    TMP="/tmp/tmpmail_"`date +%N`;
    BOUNDARY=`date +%s|md5sum|awk '{print $1;}'`
    FILENAME=`basename $ATTACHMENT`
    
    DATA=`cat $ATTACHMENT|base64`
    
    rm $TMP 2> /dev/null
    
    fappend $TMP "EHLO $computer.$domain"
    fappend $TMP "MAIL FROM:<$FREMAIL>"
    fappend $TMP "RCPT TO:<$TOEMAIL>"
    fappend $TMP "DATA"
    fappend $TMP "From: $FREMAIL"
    fappend $TMP "To: $TOEMAIL"
    fappend $TMP "Reply-To: $FREMAIL"
    fappend $TMP "Subject: $SUBJECT"
    fappend $TMP "Content-Type: multipart/mixed; boundary=\"$BOUNDARY\""
    fappend $TMP ""
    fappend $TMP "This is a MIME formatted message.  If you see this text it means that your"
    fappend $TMP "email software does not support MIME formatted messages."
    fappend $TMP ""
    fappend $TMP "--$BOUNDARY"
    fappend $TMP "Content-Type: text/plain; charset=UTF-8; format=flowed"
    fappend $TMP "Content-Disposition: inline"
    fappend $TMP ""
    fappend $TMP "$MSGBODY"
    fappend $TMP ""
    fappend $TMP ""
    fappend $TMP "--$BOUNDARY"
    fappend $TMP "Content-Type: $MIMETYPE; name=\"$FILENAME\""
    fappend $TMP "Content-Transfer-Encoding: base64"
    fappend $TMP "Content-Disposition: attachment; filename=\"$FILENAME\";"
    fappend $TMP ""
    fappend $TMP "$DATA"
    fappend $TMP ""
    fappend $TMP ""
    fappend $TMP "--$BOUNDARY--"
    fappend $TMP ""
    fappend $TMP "."
    fappend $TMP "quit"
    
    netcat $mailserver $mailserverPort < $TMP >> $TMP
    rc="$?"
    if [ "$rc" -ne "0" ]; then
        echo "Returncode: $rc"
        echo "Please inspect $TMP"
    else
        rm $TMP;
    fi
    

    您可能要添加的一件事是身份验证。我不需要它,所以我没有添加它。

    我认为它只需要md5sum、netcat、file、awk和base64命令,我猜它们在大多数系统中都是相当标准的。

    • 5
  5. Anrik
    2017-06-13T16:53:31+08:002017-06-13T16:53:31+08:00

    Telnet - 发送带有多个附件的电子邮件

    猫附件.zip | base64 > zip.txt
    猫附件.pdf | base64 > pdf.txt
    
    # 内容类型:文本/csv;name="$FILE" # 用于 CSV 文件
    # Content-Type: application/x-msdownload; name="$FILE" # 可执行文件
    # 内容类型:文本/xml;name="$FILE" # 用于 xml 文件或尝试 application/xml
    
    远程登录 smtp.server.dom 25
    
    直升机
    发件人:[email protected]
    RCPT 收件人:[email protected]
    数据
    主题:测试电子邮件
    发件人:[email protected]
    至:[email protected]
    MIME 版本:1.0
    内容类型:多部分/混合;边界="X-=-=-=-文本边界"
    
    --X-=-=-=-文本边界
    内容类型:文本/纯文本
    
    把你的信息放在这里...
    
    --X-=-=-=-文本边界
    内容类型:应用程序/zip;名称="文件.zip"
    内容传输编码:base64
    内容处置:附件;文件名="文件.zip"
    
    UEsDBBQAAAAIAG1+zEoQa....复制/粘贴zip.txt
    
    --X-=-=-=-文本边界
    内容类型:文本/pdf;名称="文件.pdf"
    内容传输编码:base64
    内容处置:附件;文件名="文件.pdf"
    
    UEsDBBQAAAAIAG1+zEoQa....复制/粘贴pdf.txt
    
    --X-=-=-=-文本边界
    .
    
    退出
    
    • 4
  6. psirac
    2012-08-12T06:19:28+08:002012-08-12T06:19:28+08:00

    这就是我用 bash 发送电子邮件的方法。我用它向我发送日志文件和外部 IP 地址,请随意使用:

    #!/bin/bash
    # Send email from bash with attachment
    # by Psirac - www.subk.org
    [email protected]
    [email protected]
    mailserver=smtp.test.com
    mylogin=`echo 'MYUSERNAME' | openssl base64`
    mypassword=`echo 'MYPASSWORD' | openssl base64`
    myip=`wget -qO- icanhazip.com`
    myfile=`cat /tmp/mytest.log | openssl base64`
    mydate=`date`
    
    exec 9<>/dev/tcp/$mailserver/25
    echo "HELO routeur.tripfiller" >&9
    read -r temp <&9
    #echo "$temp"
    echo "auth login" >&9
    read -r temp <&9
    #echo "$temp"
    echo "$mylogin=" >&9
    read -r temp <&9
    #echo "$temp"
    echo "$mypasswd=" >&9
    read -r temp <&9
    #echo "$temp"
    echo "Mail From: $from" >&9
    read -r temp <&9
    #echo "$temp"
    echo "Rcpt To: $to" >&9
    read -r temp <&9
    #echo "$temp"
    echo "Data" >&9
    read -r temp <&9
    #echo "$temp"
    echo "To:$to" >&9
    echo "MIME-Version: 1.0" >&9
    echo "Subject: Test mail sended at $mydate" >&9
    echo "From: $from" >&9
    echo "To: $to" >&9
    echo "Content-Type: multipart/mixed; boundary=sep" >&9
    echo "--sep" >&9
    echo "Content-Type: text/plain; charset=UTF-8" >&9
    echo "Here your text..." >&9
    echo "External IP adress: $myip" >&9
    echo "--sep" >&9
    echo "Content--Type: text/x-log; name=\"mytest.log\"" >&9
    echo "Content-Disposition: attachment; filename=\"mytest.log\"" >&9
    echo "Content-Transfer-Encoding: base64" >&9
    echo "" >&9
    echo "$myfile" >&9
    echo "--sep--" >&9
    echo "." >&9
    read -r temp <&9
    echo "$temp"
    echo "quit" >&9
    read -r temp <&9
    echo "$temp"
    9>&-
    9<&-
    #echo "All Done. See above for errors"
    exit 0
    

    希望这对你有好处;)

    赛拉克。

    • 3
  7. sangretu
    2009-07-11T06:01:28+08:002009-07-11T06:01:28+08:00

    您需要查看 SMTP 协议规范。对于技术规范来说,这是一本令人惊讶的轻松读物,它将帮助您了解电子邮件流程的工作原理。

    具体来说,要意识到附件被转换为 MIME 类型并以文本编码,因此您想通过 telnet 发送的任何附件都必须转换为文本并通过 telnet 协议传输。

    • 1
  8. sysadmin1138
    2009-07-11T06:47:53+08:002009-07-11T06:47:53+08:00

    如果您要测试的只是“附件是否交付”,您可能会使用 MIME 之前的附件标准:uuencode。与 MIME 不同,创建消息要简单得多。与 MIME 不同,它不需要任何标题。但是,并非所有邮件客户端都将 uuencoded 文件识别为附件,因此我建议您测试一下是否可以使用它。如果是这样,您只是为自己节省了很多精力。如果没有,那么通过 perl 或其他东西预先构建您的 MIMEed 消息并通过诸如 NetCat 之类的东西进行管道传输可能是要走的路。

    值得一看。

    • 1
  9. readyblue
    2011-12-12T06:23:01+08:002011-12-12T06:23:01+08:00

    这项工作有一个很棒的 Perl 脚本。你可以在这里找到它:

    http://www.logix.cz/michal/devel/smtp-cli/

    smtp-cli v2.9

    脚本来自作者:Michal Ludvig (c) 2003-2011 http://smtp-cli.logix.cz

    我自己使用它,效果很好,感谢 Michal ;)

    • 1

相关问题

  • 我应该使用什么策略在 linux 上安装 smtp 服务器?用于多线程服务

  • 适用于 Linux 的 SMTP 服务器,配置简单

  • 如何修复“无效”的 SMTP 服务器问题?

  • 如何配置 sendmail 以拒绝日期标题与实时相差太远的电子邮件?

  • 推荐的个人邮件服务器设置 [关闭]

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    从 IP 地址解析主机名

    • 8 个回答
  • Marko Smith

    如何按大小对 du -h 输出进行排序

    • 30 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    Windows 中执行反向 DNS 查找的命令行实用程序是什么?

    • 14 个回答
  • Marko Smith

    如何检查 Windows 机器上的端口是否被阻塞?

    • 4 个回答
  • Marko Smith

    我应该打开哪个端口以允许远程桌面?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    MikeN 在 Nginx 中,如何在维护子域的同时将所有 http 请求重写为 https? 2009-09-22 06:04:43 +0800 CST
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    0x89 bash中的双方括号和单方括号有什么区别? 2009-08-10 13:11:51 +0800 CST
  • Martin Hope
    kch 如何更改我的私钥密码? 2009-08-06 21:37:57 +0800 CST
  • Martin Hope
    Kyle Brandt IPv4 子网如何工作? 2009-08-05 06:05:31 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve