Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Email 如何通过Gmail使用简单的SMTP命令发送电子邮件?_Email_Smtp_Gmail_Smtpclient_Smtp Auth - Fatal编程技术网

Email 如何通过Gmail使用简单的SMTP命令发送电子邮件?

Email 如何通过Gmail使用简单的SMTP命令发送电子邮件?,email,smtp,gmail,smtpclient,smtp-auth,Email,Smtp,Gmail,Smtpclient,Smtp Auth,出于教育目的,我需要使用SMTP的基本和简单规则通过SMTP服务器发送电子邮件 我可以用它来做这件事。Itelnet localhost 25和命令是: 我想做同样的事情,使用Gmail SMTP服务器。但是,它需要身份验证和TLS。我不知道如何为Gmail做到这一点。以下是telnet smtp.gmail.com 587的屏幕截图: 我搜索并找到了许多链接,包括aboutSTARTTLScommand。但我不能使用TLS,也不能使用命令行(或者自己用编程语言发送命令)向Gmail的SMT

出于教育目的,我需要使用SMTP的基本和简单规则通过SMTP服务器发送电子邮件

我可以用它来做这件事。I
telnet localhost 25
和命令是:

我想做同样的事情,使用Gmail SMTP服务器。但是,它需要身份验证和TLS。我不知道如何为Gmail做到这一点。以下是telnet smtp.gmail.com 587的屏幕截图:


我搜索并找到了许多链接,包括about
STARTTLS
command。但我不能使用TLS,也不能使用命令行(或者自己用编程语言发送命令)向Gmail的SMTP服务器进行身份验证。有人能帮忙吗?

要通过gmail发送,你需要使用加密连接。单独使用telnet是不可能的,但您可以使用以下工具

使用openssl中的starttls选项进行连接,将普通连接转换为加密连接

openssl s_客户端-starttls smtp-connect smtp.gmail.com:587-crlf-ign_eof

或者直接连接到ssl socket

openssl s_客户端-连接smtp.gmail.com:465-crlf-ign_eof

EHLO localhost

之后,使用base64编码的用户名/密码对服务器进行身份验证

验证普通AG15ZW1HAWXAZ21HAWWUY29TAG1CGFZC3DVCMQ=

要从命令行获取此信息,请执行以下操作:

echo -ne '\00user@gmail.com\00password' | base64
AHVzZXJAZ21haWwuY29tAHBhc3N3b3Jk
然后继续使用“邮件发件人:”如您的示例中所示

示例会话:

openssl s_client -connect smtp.gmail.com:465 -crlf -ign_eof
[... lots of openssl output ...]
220 mx.google.com ESMTP m46sm11546481eeh.9
EHLO localhost
250-mx.google.com at your service, [1.2.3.4]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH
250 ENHANCEDSTATUSCODES
AUTH PLAIN AG5pY2UudHJ5QGdtYWlsLmNvbQBub2l0c25vdG15cGFzc3dvcmQ=
235 2.7.0 Accepted
MAIL FROM: <gryphius-demo@gmail.com>
250 2.1.0 OK m46sm11546481eeh.9
rcpt to: <somepoorguy@example.com>
250 2.1.5 OK m46sm11546481eeh.9
DATA
354  Go ahead m46sm11546481eeh.9
Subject: it works

yay!
.
250 2.0.0 OK 1339757532 m46sm11546481eeh.9
quit
221 2.0.0 closing connection m46sm11546481eeh.9
read:errno=0
openssl s_客户端-连接smtp.gmail.com:465-crlf-ign_eof […大量openssl输出…] 220 mx.google.com ESMTP M46SM11546481EH.9 EHLO本地主机 250-mx.google.com为您服务,[1.2.3.4] 250号35882577 250-8比特 250-AUTH登录普通XOATH 250增强状态码 认证普通AG5pY2UudHJ5QGdtYWlsLmNvbQBub2l0c25vdG15cGFzc3dvcmQ= 235 2.7.0接受 邮寄地址: 250 2.1.0正常M46SM11546481EH.9 rcpt至: 250 2.1.5正常M46SM11546481EH.9 资料 354继续M46SM11546481EH。9 主题:有效 耶! . 250 2.0.0正常133975352 M46SM11546481EH.9 退出 221 2.0.0闭合连接M46SM11546481EH.9 读取:errno=0
不幸的是,由于我被迫使用windows服务器,我无法让openssl以上述答案所建议的方式工作

然而,我还是能够得到一个类似的程序,名为stunnel(可以从下载)。我的想法是从你那里得到的,但我不得不稍微改变一下说明。以下是我所做的:

  • 在windows机箱上安装telnet客户端
  • 下载stunnel。(我下载并安装了一个名为stunnel-4.56-installer.exe的文件)
  • 安装后,您需要找到
    stunnel.conf
    config文件,在我的例子中,我将其安装到
    C:\ProgramFiles(x86)\stunnel
  • 然后,您需要在文本查看器(如记事本)中打开此文件。查找
    [gmail smtp]
    并删除下面客户端行上的分号(在stunnel.conf文件中,以分号开头的每一行都是注释)。你应该以这样的方式结束:

    [gmail-smtp]
    client = yes
    accept = 127.0.0.1:25
    connect = smtp.gmail.com:465
    
    Email (shown): bob@gmail.com
    Password (hidden): 
    AGJvYkBnbWFpbC5jb20AYm9iaXN0aGViZXN0cGVyc29uZXZlcg==
    
    完成此操作后,保存
    stunnel.conf
    文件并重新加载配置(要执行此操作,请使用stunnel GUI程序,然后单击configuration=>reload)

  • 现在,您应该可以在windows telnet客户端中发送电子邮件了
    转到Start=>run=>cmd

    打开cmd后,键入以下内容并按Enter键:

    telnet localhost 25
    
    然后,您将看到类似于以下内容的内容:

    220 mx.google.com ESMTP f14sm1400408wbe.2
    
    然后,您需要通过键入以下内容并按enter键进行答复:

    helo google
    
    这将给您以下答复:

    250 mx.google.com at your service
    
    250-mx.google.com at your service, [212.28.228.49]
    250-SIZE 35651584
    250-8BITMIME
    250-AUTH LOGIN PLAIN XOAUTH
    250 ENHANCEDSTATUSCODES
    
    334 VXNlcm5hbWU6
    
    334 UGFzc3dvcmQ6
    
    250 2.1.0 OK x23sm1104292weq.10
    
    250 2.1.5 OK x23sm1104292weq.10
    
    354  Go ahead x23sm1104292weq.10
    
    250 2.0.0 OK 1288307376 x23sm1104292weq.10
    
    221 2.0.0 closing connection x23sm1104292weq.10
    Connection to host lost.
    
    如果收到此消息,则需要键入以下内容并按enter键:

    ehlo google
    
    AUTH LOGIN
    
    MAIL FROM:<test@gmail.com>
    
    RCPT TO:<recipient@gmail.com>
    
    DATA
    
    然后,这将为您提供以下响应:

    250 mx.google.com at your service
    
    250-mx.google.com at your service, [212.28.228.49]
    250-SIZE 35651584
    250-8BITMIME
    250-AUTH LOGIN PLAIN XOAUTH
    250 ENHANCEDSTATUSCODES
    
    334 VXNlcm5hbWU6
    
    334 UGFzc3dvcmQ6
    
    250 2.1.0 OK x23sm1104292weq.10
    
    250 2.1.5 OK x23sm1104292weq.10
    
    354  Go ahead x23sm1104292weq.10
    
    250 2.0.0 OK 1288307376 x23sm1104292weq.10
    
    221 2.0.0 closing connection x23sm1104292weq.10
    Connection to host lost.
    
    现在,您应该准备好使用您的Gmail详细信息进行身份验证。要执行此操作,请键入以下命令并按enter键:

    ehlo google
    
    AUTH LOGIN
    
    MAIL FROM:<test@gmail.com>
    
    RCPT TO:<recipient@gmail.com>
    
    DATA
    
    然后,这将为您提供以下响应:

    250 mx.google.com at your service
    
    250-mx.google.com at your service, [212.28.228.49]
    250-SIZE 35651584
    250-8BITMIME
    250-AUTH LOGIN PLAIN XOAUTH
    250 ENHANCEDSTATUSCODES
    
    334 VXNlcm5hbWU6
    
    334 UGFzc3dvcmQ6
    
    250 2.1.0 OK x23sm1104292weq.10
    
    250 2.1.5 OK x23sm1104292weq.10
    
    354  Go ahead x23sm1104292weq.10
    
    250 2.0.0 OK 1288307376 x23sm1104292weq.10
    
    221 2.0.0 closing connection x23sm1104292weq.10
    Connection to host lost.
    
    这意味着我们已经准备好使用gmail地址和密码进行身份验证

    但是,由于这是一个加密会话,我们必须发送base64编码的电子邮件和密码。要对电子邮件和密码进行编码,您可以使用转换器程序或在线网站对其进行编码(例如,或在谷歌上搜索“base64在线编码”)。我建议您在完成此操作之前不要再次触摸cmd/telnet会话

    比如说test@gmail.com将成为dGVzdEBnbWFpbC5jb20=并且密码将成为cGFzc3dvcmQ=

    完成此操作后,将转换后的base64用户名复制并粘贴到cmd/telnet会话中,然后按enter键。这将给您以下响应:

    250 mx.google.com at your service
    
    250-mx.google.com at your service, [212.28.228.49]
    250-SIZE 35651584
    250-8BITMIME
    250-AUTH LOGIN PLAIN XOAUTH
    250 ENHANCEDSTATUSCODES
    
    334 VXNlcm5hbWU6
    
    334 UGFzc3dvcmQ6
    
    250 2.1.0 OK x23sm1104292weq.10
    
    250 2.1.5 OK x23sm1104292weq.10
    
    354  Go ahead x23sm1104292weq.10
    
    250 2.0.0 OK 1288307376 x23sm1104292weq.10
    
    221 2.0.0 closing connection x23sm1104292weq.10
    Connection to host lost.
    
    现在将转换后的base64密码复制并粘贴到cmd/telnet会话中,然后按enter键。如果两个登录凭据都正确,则应给出以下响应:

    235 2.7.0 Accepted
    
    您现在应按以下格式输入发件人电子邮件(应与用户名相同),然后按enter键:

    ehlo google
    
    AUTH LOGIN
    
    MAIL FROM:<test@gmail.com>
    
    RCPT TO:<recipient@gmail.com>
    
    DATA
    
    您现在可以输入类似格式的收件人电子邮件地址,然后按enter键:

    ehlo google
    
    AUTH LOGIN
    
    MAIL FROM:<test@gmail.com>
    
    RCPT TO:<recipient@gmail.com>
    
    DATA
    
    现在,您需要键入以下内容并按enter键:

    ehlo google
    
    AUTH LOGIN
    
    MAIL FROM:<test@gmail.com>
    
    RCPT TO:<recipient@gmail.com>
    
    DATA
    
    这将给您以下答复:

    250 mx.google.com at your service
    
    250-mx.google.com at your service, [212.28.228.49]
    250-SIZE 35651584
    250-8BITMIME
    250-AUTH LOGIN PLAIN XOAUTH
    250 ENHANCEDSTATUSCODES
    
    334 VXNlcm5hbWU6
    
    334 UGFzc3dvcmQ6
    
    250 2.1.0 OK x23sm1104292weq.10
    
    250 2.1.5 OK x23sm1104292weq.10
    
    354  Go ahead x23sm1104292weq.10
    
    250 2.0.0 OK 1288307376 x23sm1104292weq.10
    
    221 2.0.0 closing connection x23sm1104292weq.10
    Connection to host lost.
    
    现在我们可以开始撰写信息了!要执行此操作,请以以下格式输入消息(提示:在记事本中执行此操作,并将整个消息复制到cmd/telnet会话中):

    这将给您以下答复:

    250 mx.google.com at your service
    
    250-mx.google.com at your service, [212.28.228.49]
    250-SIZE 35651584
    250-8BITMIME
    250-AUTH LOGIN PLAIN XOAUTH
    250 ENHANCEDSTATUSCODES
    
    334 VXNlcm5hbWU6
    
    334 UGFzc3dvcmQ6
    
    250 2.1.0 OK x23sm1104292weq.10
    
    250 2.1.5 OK x23sm1104292weq.10
    
    354  Go ahead x23sm1104292weq.10
    
    250 2.0.0 OK 1288307376 x23sm1104292weq.10
    
    221 2.0.0 closing connection x23sm1104292weq.10
    Connection to host lost.
    
    现在,您需要通过键入以下内容并按enter键结束会话:

    QUIT
    
    这将给您以下答复:

    250 mx.google.com at your service
    
    250-mx.google.com at your service, [212.28.228.49]
    250-SIZE 35651584
    250-8BITMIME
    250-AUTH LOGIN PLAIN XOAUTH
    250 ENHANCEDSTATUSCODES
    
    334 VXNlcm5hbWU6
    
    334 UGFzc3dvcmQ6
    
    250 2.1.0 OK x23sm1104292weq.10
    
    250 2.1.5 OK x23sm1104292weq.10
    
    354  Go ahead x23sm1104292weq.10
    
    250 2.0.0 OK 1288307376 x23sm1104292weq.10
    
    221 2.0.0 closing connection x23sm1104292weq.10
    Connection to host lost.
    

    您的电子邮件现在应该在收件人的邮箱中

    正如没有人提到的那样-我建议使用很棒的工具来达到这个目的-swaks

    # yum info swaks
    Installed Packages
    Name        : swaks
    Arch        : noarch
    Version     : 20130209.0
    Release     : 3.el6
    Size        : 287 k
    Repo        : installed
    From repo   : epel
    Summary     : Command-line SMTP transaction tester
    URL         : http://www.jetmore.org/john/code/swaks
    License     : GPLv2+
    Description : Swiss Army Knife SMTP: A command line SMTP tester. Swaks can test
                : various aspects of your SMTP server, including TLS and AUTH.
    
    它有很多选择,几乎可以做任何你想做的事情

    GMAIL:STARTTLS,SSLv3(是的,2016年GMAIL仍然支持SSLv3)

    $echo“你好,世界”