{"id":93,"date":"2010-10-28T23:28:00","date_gmt":"2010-10-28T23:28:00","guid":{"rendered":"https:\/\/www.tech-and-dev.com\/blog\/2010\/10\/28\/sending-an-email-using-gmail-smtp-server-from-telnet-using-stunnel-in-windows\/"},"modified":"2021-02-22T01:20:07","modified_gmt":"2021-02-22T01:20:07","slug":"sending-email-using-gmail-smtp-server","status":"publish","type":"post","link":"https:\/\/www.tech-and-dev.com\/blog\/2010\/10\/sending-email-using-gmail-smtp-server.html","title":{"rendered":"Sending an email using gmail smtp server from telnet (using stunnel) in windows."},"content":{"rendered":"<div dir=\"ltr\" style=\"text-align: left;\">\n<div style=\"text-align: justify;\">\n<div style=\"clear: both; text-align: center;\"><img decoding=\"async\" src=\"https:\/\/www.tech-and-dev.com\/blog\/wp-content\/uploads\/2021\/02\/gmail-logo.png\" border=\"0\" \/><\/div>\n<p>This article explains how to send an email from gmail smtp server using telnet and stunnel on a windows operating system.<\/p>\n<\/div>\n<div style=\"text-align: justify;\"><\/div>\n<div style=\"text-align: justify;\">Sometimes you may want to test how an email is sent in telnet, maybe in order to create a program that will send and receive emails.<\/div>\n<div style=\"text-align: justify;\"><\/div>\n<div style=\"text-align: justify;\">For example, developers who use J2ME to write mobile applications know that there is no existent library or API that allow you to send or receive an email through smtp servers that requires authentication and SSL, and in order to implement such an application, the developer must use SecureSocket and Inputsteam and Outputstream and must know how the smtp server (or pop3 in case of receiving an email) works and how is it gonna respond to commands.<\/div>\n<div style=\"text-align: justify;\"><\/div>\n<div style=\"text-align: justify;\">By default, Telnet doesn&#8217;t support secure connections or SSL, for that we&#8217;re gonna have to redirect (tunnel) the telnet commands to the stunnel to encrypt them and send them to the gmail smtp server.<\/div>\n<div style=\"text-align: justify;\"><\/div>\n<div style=\"text-align: justify;\">First, we&#8217;re gonna need <b>stunnel<\/b> which can be downloaded from <a href=\"http:\/\/www.stunnel.org\/\">http:\/\/www.stunnel.org<\/a><\/div>\n<div style=\"text-align: justify;\"><\/div>\n<div style=\"text-align: justify;\">After downloading and installing stunnel, we have to configure the <b>stunnel.conf<\/b> file located in stunnel directory.<\/div>\n<div style=\"text-align: justify;\"><\/div>\n<div style=\"text-align: justify;\">in the stunnel.conf, every line that start with a semicolon is a comment,<\/div>\n<div style=\"text-align: justify;\">Make sure to uncomment\u00a0 <b>client = yes<\/b><\/div>\n<div style=\"text-align: justify;\">and edit(or create) the smtps part as following:<\/div>\n<div style=\"text-align: justify;\"><b>[smtps]<br \/>\naccept\u00a0 = 2301<br \/>\nconnect = smtp.gmail.com:465<\/b><\/div>\n<div style=\"text-align: justify;\"><\/div>\n<div style=\"text-align: justify;\">&#8211; smtps is for secure smtp servers, (Note: If [smtps] doesn&#8217;t work, try to use [ssmtp] instead)<\/div>\n<div style=\"text-align: justify;\">&#8211; Accept indicates the port that you want to use for tunneling,<\/div>\n<div style=\"text-align: justify;\">&#8211; Connect indicates the server we&#8217;re connecting to, in our case, its the gmail smtp server, with port 465.<\/div>\n<div style=\"text-align: justify;\">&#8211; Port 465 is the port for smtps, while port 587 is for TLS\/STARTTLS.<\/div>\n<div style=\"text-align: justify;\"><\/div>\n<div style=\"text-align: justify;\"><\/div>\n<div style=\"text-align: justify;\">the stunnel.conf file should now look like the following:<\/div>\n<div style=\"text-align: justify;\">\n<p><span style=\"color: #444444;\"><span style=\"font-family: 'Helvetica Neue',Arial,Helvetica,sans-serif;\">socket = l:TCP_NODELAY=1<br \/>\nsocket = r:TCP_NODELAY=1<\/span><\/span><\/p>\n<p>client = yes<\/p>\n<p>[smtps]<br \/>\naccept\u00a0 = 2301<br \/>\nconnect = smtp.gmail.com:465<\/p>\n<\/div>\n<div style=\"text-align: justify;\"><\/div>\n<div style=\"text-align: justify;\">Save the stunnel.conf file and in stunnel program click on configuration=&gt;Reload.<\/div>\n<div style=\"text-align: justify;\"><\/div>\n<div style=\"text-align: justify;\"><\/div>\n<div style=\"text-align: justify;\">Now it&#8217;s time to send the email.<\/div>\n<div style=\"text-align: justify;\"><\/div>\n<div style=\"text-align: justify;\">In windows, go to start=&gt;run=&gt;cmd<\/div>\n<div style=\"text-align: justify;\">Once the command line is opened write the following: <b>telnet localhost 2301<\/b><\/div>\n<div style=\"text-align: justify;\"><\/div>\n<div style=\"text-align: justify;\">you should see the following:<\/div>\n<div style=\"color: red; text-align: justify;\">220 mx.google.com ESMTP f14sm1400408wbe.2<\/div>\n<div style=\"text-align: justify;\"><\/div>\n<div style=\"text-align: justify;\">we send a hello message:<\/div>\n<div style=\"text-align: justify;\"><span style=\"color: blue;\">helo google<\/span><\/div>\n<div style=\"color: red; text-align: justify;\">250 mx.google.com at your service<\/div>\n<div style=\"text-align: justify;\"><span style=\"color: blue;\">ehlo google<\/span><\/div>\n<div style=\"text-align: justify;\"><span style=\"color: red;\">250-mx.google.com at your service, [212.28.228.49]<\/span><\/div>\n<div style=\"text-align: justify;\"><span style=\"color: red;\">250-SIZE 35651584<\/span><\/div>\n<div style=\"text-align: justify;\"><span style=\"color: red;\">250-8BITMIME<\/span><\/div>\n<div style=\"text-align: justify;\"><span style=\"color: red;\">250-AUTH LOGIN PLAIN XOAUTH<\/span><\/div>\n<div style=\"text-align: justify;\"><span style=\"color: red;\">250 ENHANCEDSTATUSCODES<\/span><\/div>\n<div style=\"text-align: justify;\"><\/div>\n<div style=\"text-align: justify;\"><span style=\"color: blue;\"><span style=\"color: black;\">Then the authentication request:<\/span><\/span><\/div>\n<div style=\"text-align: justify;\"><span style=\"color: blue;\"><span style=\"color: black;\"><span style=\"color: blue;\">\u00a0AUTH LOGIN<\/span><\/span><\/span><\/div>\n<div style=\"text-align: justify;\"><span style=\"color: blue;\"><span style=\"color: black;\"><span style=\"color: red;\">334 VXNlcm5hbWU6<\/span><\/span><\/span><\/div>\n<div style=\"text-align: justify;\"><\/div>\n<div style=\"text-align: justify;\"><span style=\"color: blue;\"><span style=\"color: black;\"><span style=\"color: black;\">Now we have to authenticate by using our gmail address and password.<\/span><\/span><\/span><\/div>\n<div style=\"text-align: justify;\"><span style=\"color: blue;\"><span style=\"color: black;\"><span style=\"color: black;\">However since this is an encrypted session, we&#8217;re gonna have to send the email and password encrypted in base64. To encrypt your email and password, you can use a program or an online website to encrypt it (search on google for: <i>base64 online encryption<\/i>).<\/span><\/span><\/span><\/div>\n<div style=\"text-align: justify;\"><span style=\"color: blue;\"><span style=\"color: black;\"><span style=\"color: black;\">For example the sentence &#8220;<b>my email address<\/b>&#8221; in base64 will become: <b>bXkgZW1haWwgYWRkcmVzcw==<\/b><\/span><\/span><\/span><\/div>\n<div style=\"text-align: justify;\"><span style=\"color: blue;\"><span style=\"color: black;\"><span style=\"color: black;\">and the word &#8220;<b>password<\/b>&#8221; will become: <b>cGFzc3dvcmQ=<\/b><\/span><\/span><\/span><\/div>\n<div style=\"text-align: justify;\"><\/div>\n<div style=\"text-align: justify;\"><span style=\"color: blue;\"><span style=\"color: black;\"><span style=\"color: black;\">Now you should have your email address and password encrypted in base64, go back to the command line and write your base64 email first, (I will use &#8220;my email address&#8221; as my email address and &#8220;password&#8221; as my password for this example):<\/span><\/span><\/span><\/div>\n<div style=\"color: blue; text-align: justify;\">bXkgZW1haWwgYWRkcmVzcw==<\/div>\n<div style=\"color: red; text-align: justify;\">334 UGFzc3dvcmQ6<\/div>\n<div style=\"color: red; text-align: justify;\"><\/div>\n<div style=\"color: black; text-align: justify;\">Enter your password:<\/div>\n<div style=\"color: blue; text-align: justify;\">cGFzc3dvcmQ=<\/div>\n<div style=\"color: blue; text-align: justify;\"><span style=\"color: red;\">235 2.7.0 Accepted<\/span><\/div>\n<div style=\"color: blue; text-align: justify;\"><\/div>\n<div style=\"color: blue; text-align: justify;\"><span style=\"color: black;\">Enter your email:<\/span><\/div>\n<div style=\"color: blue; text-align: justify;\">MAIL FROM:&lt;etienne.rached@gmail.com&gt;<\/div>\n<div style=\"color: blue; text-align: justify;\"><span style=\"color: red;\">250 2.1.0 OK x23sm1104292weq.10<\/span><\/div>\n<div style=\"color: blue; text-align: justify;\"><\/div>\n<div style=\"color: blue; text-align: justify;\"><span style=\"color: black;\">Enter the recipient email (I&#8217;ll email myself for testing):<\/span><\/div>\n<div style=\"color: blue; text-align: justify;\"><span style=\"color: black;\"><span style=\"color: blue;\">RCPT TO:&lt;etienne.rached@gmail.com&gt;<\/span><br \/>\n<span style=\"color: red;\">250 2.1.5 OK x23sm1104292weq.10<\/span><\/span><\/div>\n<div style=\"color: blue; text-align: justify;\"><span style=\"color: black;\">\u00a0<\/span><\/div>\n<div style=\"color: blue; text-align: justify;\"><span style=\"color: black;\">Now comes the message part:<\/span><\/div>\n<div style=\"color: blue; text-align: justify;\">DATA<\/div>\n<div style=\"color: blue; text-align: justify;\"><span style=\"color: red;\">354\u00a0 Go ahead x23sm1104292weq.10<\/span><\/div>\n<div style=\"color: blue; text-align: justify;\">\n<p>From: Etienne &lt;etienne.rached@gmail.com&gt;<br \/>\nTo: Meeeeeeee &lt;etienne.rached@gmail.com&gt;<br \/>\nSubject: Testing email from telnet<br \/>\nThis is the body<\/p>\n<p>Adding more lines to the body message.<\/p>\n<\/div>\n<div style=\"color: blue; text-align: justify;\"><\/div>\n<div style=\"color: blue; text-align: justify;\"><\/div>\n<div style=\"color: blue; text-align: justify;\"><\/div>\n<div style=\"color: blue; text-align: justify;\"><\/div>\n<div style=\"color: blue; text-align: justify;\"><\/div>\n<div style=\"color: blue; text-align: justify;\"><\/div>\n<div style=\"color: blue; text-align: justify;\"><\/div>\n<div style=\"color: blue; text-align: justify;\"><\/div>\n<div style=\"color: blue; text-align: justify;\"><\/div>\n<div style=\"color: blue; text-align: justify;\">\n<p><span style=\"color: black;\">Finish the DATA with a dot:<\/span><br \/>\n.<br \/>\n<span style=\"color: red;\">250 2.0.0 OK 1288307376 x23sm1104292weq.10<\/span><\/p>\n<p><span style=\"color: black;\">And quit:<\/span><span style=\"color: red;\"><br \/>\n<\/span><br \/>\nQUIT<br \/>\n<span style=\"color: red;\">221 2.0.0 closing connection x23sm1104292weq.10<\/span><br \/>\n<span style=\"color: red;\">Connection to host lost.<\/span><\/p>\n<\/div>\n<div style=\"color: blue; text-align: justify;\"><\/div>\n<div style=\"color: blue; text-align: justify;\"><span style=\"color: black;\">The email should now be sent and you can find it in your &#8220;sent mail&#8221; in your gmail.<\/span><\/div>\n<div style=\"color: blue; text-align: justify;\"><\/div>\n<div style=\"color: blue; text-align: justify;\"><\/div>\n<div style=\"color: blue; text-align: justify;\"><span style=\"color: black;\">Using Telnet with stunnel is very easy, I will try and show how to receive an email with pop3 using telnet, and how to send an email using TLS in my future posts.<\/span><\/div>\n<div style=\"color: blue; text-align: justify;\"><\/div>\n<div style=\"color: blue; text-align: justify;\"><span style=\"color: black;\">Hope you enjoyed this one \ud83d\ude42 <\/span><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This article explains how to send an email from gmail smtp server using telnet and stunnel on a windows operating system. Sometimes you may want to test how an email is sent in telnet, maybe in order to create a program that will send and receive emails. For example, developers who use J2ME to write [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[13,14,131,132,133,129,128],"tags":[],"class_list":["post-93","post","type-post","status-publish","format-standard","hentry","category-email","category-gmail","category-j2me","category-javame","category-smtp","category-stunnel","category-telnet"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/posts\/93","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/comments?post=93"}],"version-history":[{"count":1,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/posts\/93\/revisions"}],"predecessor-version":[{"id":396,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/posts\/93\/revisions\/396"}],"wp:attachment":[{"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/media?parent=93"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/categories?post=93"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/tags?post=93"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}