how to use ssl with smtp in c++ - c++

im looking for code example or tutorial on what i need to do to code sending email
via email client ( that i already have ) that will support ssl authentication
i guess using open ssl

Boost.Asio and the Sockets Library both have SSL support. The latter has SMTP server class but unfortunately no client. I found an example client but it uses the simple C socket.h.
Update: Here are some real working examples. They all support SSL or TLS.
Msmtp: An SMTP client and SMTP plugin for mail user agents such as Mutt
sSMTP: Extremely simple MTA to get mail off the system to a Mailhub
libESMTP: lib that implements the client side of the SMTP protocol

Related

using c++ wss server with websocket++ or other c++ websocket lib

I am trying to write a c++ websocket server and have browser/chrome clients connect over websockets, for a multiplayer game. The websocket c++ library I'm using atm is websocketpp or websocket++. I wrote an app that allows clients to connect over ws and localhost, but when I add an ip for the address, connections don't occur at all. Now I think I have to use ssl and wss for ip connection? I tried it and there is some connection activity, but then the handshake times out. Could I be experiencing cross-orgin issues, or what, do i need ssl? I am new to websockets. Could the problem be my ssl certs I made with openssl? I can post code, or if you are familiar with a c++ library to do websockets, what is it? Is this even a possible thing to do?
There could be multiple reasons why it won't connect over ip.
The first is port forwarding. On a local network it's not necessary but running a server over a remote network, portforwarding has to be done. You can just run your server then use a simple port checker (there's many websites for them) to see if a connection can be established.
The other reason could be as you said ssl. If you are running your client on a web host, the host may require a connection to be made over ssl/wss for websockets. If your server isn't running a valid ssl certificate then this could prevent the client from connecting to your server. I know for exampe Github pages requires the server to be running wss or valid ssl certificates on the server side in order for a client connection to be established; however, if you use a custom domain name for Github pages then you can disable the need for ssl.
In order to get valid ssl certificates you would need to register a domain for your ip address then either buy certificates or use free certificates from zerossl or other distributors.
Here is a game I have written which connects to a c++ server which I'm running on my own machine with its own domain with valid ssl certificates and the client is running on github pages with a custom domain I have registered.
It's basically multiplayer minesweeper where the objective is to locate the flags rather than avoid them.

How to read from a secure websocket with Tink in C++

I'm working on a C++ client app that needs to read from a secure websocket. After looking into some websocket libraries I would like to use easywsclient but it does not support TLS. I tried adding support for URLs that start with wss using OpenSSL by following this article but would like to avoid loading certificates and private keys from files (if possible). I looked into how this is done in boost asio and they recommend using certify to access a platform's TLS keystore. I also researched chrome browser and found that it uses BoringSSL which lead me to Tink. However, I can not find an example of how to use Tink/BoringSSL to secure a websocket and deal with client side certificates and keys. Can someone point me in the right direction?
Clients that want to read from a websocket do not have to setup cerificates and private keys, this is only relevant for a web socket server that must suport tls/ssl.
As I do not know much about C++ , I can only give you an example in PHP for a client connecting to a websocket server using TLS/SSL. Here the client prepares a $context in order to use TLS/SSL vai a stream.
$context = stream_context_create();
stream_context_set_option($context, 'ssl', 'allow_self_signed', true);
stream_context_set_option($context, 'ssl', 'verify_peer', false);
stream_context_set_option($context, 'ssl', 'verify_peer_name', false);
Later on the connection is made using this context,
$this->socketMaster = stream_socket_client(
"$prot$Address$Port", $errno, $errstr, 30,
STREAM_CLIENT_CONNECT, $context
);
For the full source go to:
https://github.com/napengam/phpWebSocketServer/blob/master/phpClient/websocketCore.php

how to send email using c++ without specifying smtp server

I want to send email over SMTP using c++ code, how should I handle it in generic way that a random user don't have to handle SMTP server specification? (Windows)
Without specification means user should not have to write IP address of SMTP server etc.
Install an smtp relay server on the same host where the code runs and set the SMTP server in the code to 'localhost'.
Jasper's answer is correct. You need to install an SMTP server on the host that your C++ program is running on. One option is qmail. See www.lifewithqmail.org for more info, including a step-by-step guide on how to install it.
Most linux SMTP servers (including qmail) will create a smylink /usr/sbin/sendmail which you can use to send an outgoing message. In general, the syntax is:
/usr/sbin/sendmail recipient#domain.tld < /path/to/file/containing/the/message
See the help file for more info, including flags that you can use to specify the envelope sender, etc.
You can send a message from your C++ program by doing a system call to /usr/sbin/sendmail similar to the above.
You could use Simple MAPI, but you need an email program supporting the MAPI-interface like Outlook.

Send mail in c++ in windows using gmail as smtp relayer

Does anyone know how to send an email with an attachment(txt file) using a gmail as an smtp relayer in windows environment.I have looked at some sample code here but that was for the linux os. So far i could not find any sample code for windows.Help would be appreciated.
Google mail server will only accept secure connection, SSL or TLS. Here are the addresses and ports GMail uses: http://support.google.com/mail/bin/answer.py?hl=en&answer=13287
So, you need to use SSL/TLS-enabled socket like this one: CSslSocket - SSL/TLS enabled CSocket.
Or, if you want to implement SMTP over SSL yourself, using SChannel API, this sample will guide you through: C++ SSPI Schannel TLS example (this shows how to break through SSL and you are to complete with with SMTP plain text conversation sending an email).

sending email with gmail smtp ( secure layer ) in c++

does any one had success with gmail smtp servers ? smtp.gmail.com
to send emails from c++ code ?
i know its using secure layer but i have no idea how to implement such one .
This is what i used, It was for linux though, It should Technically work on windows
http://johnwiggins.net/jwsmtp/
The Tutorials are there and straigt forwards
http://johnwiggins.net/jwsmtp/example1.html
Here is a copy and paste from the site showing Ports and SMTP Server. Credit goes to john wiggins
jwsmtp::mailer mail(to.c_str( ),
from.c_str( ),
subject.c_str( ),
mailmessage.c_str( ),
smtpserver.c_str( ),
jwsmtp::mailer::SMTP_PORT,
false);
To Authenticate
mail.username("loginname");
mail.password("secret");
mail.authtype(mailer::PLAIN);
Currently only LOGIN and PLAIN authentication are supported, LOGIN by default,
to set to PLAIN call the authtype function
Your ISP could be blocking the secure SMTP port.
You could try to the same in Thunderbird and verify that the port is open first.
Sending to GMail over SSL connection on port 465 does work, and in a straightforward way. You establish connection, you do SSL initialization/handshake, then send EHLO command and it is the usual way from there. You also need login or plain authentication with the server to make it accept your messages.