Connecting to smtp.gmail.com via command line

后端 未结 10 2362
粉色の甜心
粉色の甜心 2020-12-07 16:42

I am in the process of writing an application that sends mail via an valid GMail user ID and password.

I just wanted to simulate the SMTP connection on my Windows XP

相关标签:
10条回答
  • 2020-12-07 17:01

    Using Linux or OSx, do what Sorin recommended but use port 465 instead. 25 is the generic SMTP port, but not what GMail uses. Also, I don't believe you want to use -starttls smtp

    openssl s_client -connect smtp.gmail.com:465
    

    You should get lots of information on the SSL session and the response:

    220 mx.google.com ...
    

    Type in

    HELO smtp.gmail.com 
    

    and you'll receive:

    250 mx.google.com at your service
    

    From there it is not quite as straightforward as just sending SMTP messages because Gmail has protections in place to ensure you only send emails appearing to be from accounts that actually belong to you. Instead of typing in "Helo", use "Ehlo". I don't know much about SMTP so I cannot explain the difference, and don't have time to research much. Perhaps someone with more knowledge can explain.

    Then, type "auth login" and you will receive the following:

    334 VXNlcm5hbWU6
    

    This is essentially the word "Username" encoded in Base 64. Using a Base 64 encoder such as this one, encode your user name and enter it. Do the same for your password, which is requested next. You should see:

    235 2.7.0 Accepted
    

    And that's it, you're logged in.

    There is one more oddity to overcome if you're using OSx or Linux terminals. Just pressing the "ENTER" key does not apparently result in a CRLF which SMTP needs to end a message. You have to use "CTRL+V+ENTER". So, this should look like the following:

    ^M
    .^M
    250 2.0.0 OK
    
    0 讨论(0)
  • 2020-12-07 17:03

    Gmail require SMTP communication with their server to be encrypted. Although you're opening up a connection to Gmail's server on port 465, unfortunately you won't be able to communicate with it in plaintext as Gmail require you to use STARTTLS/SSL encryption for the connection.

    0 讨论(0)
  • 2020-12-07 17:05

    This is command for connect

    • server_name: smtp.gmail.com
    • server_port: 587
    • user_name__hash: echo -n '{{user_name}}' | base64
    • user_password__hash: echo -n '{{user_password}}' | base64
    openssl s_client -connect {{server_name}}:{{server_port}} -crlf -quiet -starttls smtp
    

    and steps to accept message to send

    auth login
    {{user_name__hash}}
    {{user_password__hash}}
    helo {{server_name}}
    mail from: <{{message_from}}>
    rcpt to: <{{message_to}}>
    DATA
    from: <{{message_from}}>
    to: <{{message_to}}>
    subject:{{message_subject}}
    Content-Type: text/html; charset='UTF-8'; Content-Transfer-Encoding: base64;
    MIME-Version: 1.0
    {{message_content}}
    .
    quit
    
    0 讨论(0)
  • 2020-12-07 17:09

    Start session from terminal:

    openssl s_client -connect smtp.gmail.com:25 -starttls smtp

    The last line of the response should be "250 SMTPUTF8"

    Initiate login

    auth login

    This should return "334 VXNlcm5hbWU6".

    Type username

    Type your username in base64 encoding (eg. echo -n 'your-username' | base64)

    This should return "334 UGFzc3dvcmQ6"

    Type password

    Type your password in base64 encoding (eg. echo -n 'your-password' | base64)

    Success

    You should see "235 2.7.0 Accepted" and you're are successfully logged in

    0 讨论(0)
  • 2020-12-07 17:15

    gmail uses an encrypted connection. So, even after you establish a connection, you wont be able to send any email. The encryption is a little complex to manage. Try using openssl instead.

    The thread below should help-

    How to send email using simple SMTP commands via Gmail?

    0 讨论(0)
  • 2020-12-07 17:17

    Check this post in lifehacker : Geek to Live: Back up Gmail with fetchmail . It uses a command line program. Check and see if it helps. BTW why are you using command line when there are many other nice alternatives?

    0 讨论(0)
提交回复
热议问题