Overview
Features
Download
Documentation
Community
Add-Ons & Services

Using SecureSMTPClientSession with GMail

Please post support and help requests here.

Using SecureSMTPClientSession with GMail

Postby peterk » 28 Oct 2011, 06:27

Hi all,

I am trying to send an e-mail through GMail's smtp severs. They require authentication, so I am using SecureSMTPClientSession. Here is the code:

Code: Select all
Poco::Net::MailMessage message;
   message.setSender("sender@sender.com");
   message.addRecipient(Poco::Net::MailRecipient(Poco::Net::MailRecipient::PRIMARY_RECIPIENT, "blah@blah.blah", "Blah Blaherson"));
   message.setSubject("Test Message");
   message.setContent("Hello\r\nblah blah\r\n\r\nJohn\r\n");

   Poco::Net::SecureSMTPClientSession session("smtp.gmail.com",587);
   session.login("smtp.gmail.com");
   session.startTLS();
   session.login("smtp.gmail.com",Poco::Net::SMTPClientSession::AUTH_LOGIN,"gmail_account","gmail_password");
   session.sendMessage(message);


Everything compiles without any errors or warnings, but the code segfaults on session.startTLS(). What am I doing wrong? Has anybody on this forum done this before?

Thanks,
P.
peterk
 
Posts: 3
Joined: 28 Oct 2011, 06:20

Re: Using SecureSMTPClientSession with GMail

Postby guenter » 29 Oct 2011, 08:09

You'll either need to create a Poco::Net::Context object (for client use) and pass it to startTLS(), or use the Util application framework (Poco::Util::Application or Poco::Util::ServerApplication) and create a configuration file with an appropriate OpenSSL configuration.

See the download sample in the NetSSL_OpenSSL/samples directory for how to perform proper NetSSL initialization and for how to create the Context object.
guenter
 
Posts: 1138
Joined: 11 Jul 2006, 16:27
Location: Austria

Re: Using SecureSMTPClientSession with GMail

Postby peterk » 30 Oct 2011, 16:21

Sorry, I still can't figure out how to do it. Can you provide some example code applicable to this case, using Poco::Net::Context?
peterk
 
Posts: 3
Joined: 28 Oct 2011, 06:20

Re: Using SecureSMTPClientSession with GMail

Postby guenter » 01 Nov 2011, 20:57

Here's the Mail sample from Net modified to work with SecureSMTPClientSession. Tested with gmail. Note that you need to call login() twice. Once without credentials, the second time (after switching to a secure connection) with your username and password.


Code: Select all
#include "Poco/Net/MailMessage.h"
#include "Poco/Net/MailRecipient.h"
#include "Poco/Net/SecureSMTPClientSession.h"
#include "Poco/Net/StringPartSource.h"
#include "Poco/Net/SSLManager.h"
#include "Poco/Net/KeyConsoleHandler.h"
#include "Poco/Net/ConsoleCertificateHandler.h"
#include "Poco/SharedPtr.h"
#include "Poco/Path.h"
#include "Poco/Exception.h"
#include <iostream>


using Poco::Net::MailMessage;
using Poco::Net::MailRecipient;
using Poco::Net::SMTPClientSession;
using Poco::Net::SecureSMTPClientSession;
using Poco::Net::StringPartSource;
using Poco::Net::SSLManager;
using Poco::Net::Context;
using Poco::Net::KeyConsoleHandler;
using Poco::Net::PrivateKeyPassphraseHandler;
using Poco::Net::InvalidCertificateHandler;
using Poco::Net::ConsoleCertificateHandler;
using Poco::SharedPtr;
using Poco::Path;
using Poco::Exception;


class SSLInitializer
{
public:
   SSLInitializer()
   {
      Poco::Net::initializeSSL();
   }
   
   ~SSLInitializer()
   {
      Poco::Net::uninitializeSSL();
   }
};


const unsigned char PocoLogo[] =
{
   #include "PocoLogo.hpp"
};


int main(int argc, char** argv)
{
   SSLInitializer sslInitializer;

   if (argc != 4)
   {
      Path p(argv[0]);
      std::cerr << "usage: " << p.getBaseName() << " <mailhost> <sender> <recipient>" << std::endl;
      std::cerr << "       Send an email greeting from <sender> to <recipient>," << std::endl;
      std::cerr << "       the SMTP server at <mailhost>." << std::endl;
      return 1;
   }
   
   std::string mailhost(argv[1]);
   std::string sender(argv[2]);
   std::string recipient(argv[3]);
   
   try
   {
      // Note: we must create the passphrase handler prior Context
      SharedPtr<InvalidCertificateHandler> pCert = new ConsoleCertificateHandler(false); // ask the user via console
      Context::Ptr pContext = new Context(Context::CLIENT_USE, "", "", "", Context::VERIFY_RELAXED, 9, true, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
      SSLManager::instance().initializeClient(0, pCert, pContext);

      MailMessage message;
      message.setSender(sender);
      message.addRecipient(MailRecipient(MailRecipient::PRIMARY_RECIPIENT, recipient));
      message.setSubject("Hello from the POCO C++ Libraries");
      std::string content;
      content += "Hello ";
      content += recipient;
      content += ",\r\n\r\n";
      content += "This is a greeting from the POCO C++ Libraries.\r\n\r\n";
      std::string logo(reinterpret_cast<const char*>(PocoLogo), sizeof(PocoLogo));
      message.addContent(new StringPartSource(content));
      message.addAttachment("logo", new StringPartSource(logo, "image/gif"));
      
      SecureSMTPClientSession session(mailhost);
      session.login();
      session.startTLS(pContext);
      session.login(SMTPClientSession::AUTH_LOGIN, "username", "password");
      session.sendMessage(message);
      session.close();
   }
   catch (Exception& exc)
   {
      std::cerr << exc.displayText() << std::endl;
      return 1;
   }
   return 0;
}
guenter
 
Posts: 1138
Joined: 11 Jul 2006, 16:27
Location: Austria

Re: Using SecureSMTPClientSession with GMail

Postby peterk » 03 Nov 2011, 03:22

Thank you!
peterk
 
Posts: 3
Joined: 28 Oct 2011, 06:20

Re: Using SecureSMTPClientSession with GMail

Postby vortex » 09 May 2012, 12:30

The following code worked fine for me. But I would like to store the certificate on a location on disk (on a trusted list location), and then use this code to send an email without the console handler getting called, therefore i modified your code in the following manner:
Code: Select all
...
Context::Ptr pContext = new Context(Context::CLIENT_USE, "", "", "C:\\TrustedCertificates\\", Context::VERIFY_RELAXED, 9, true, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
..

And i have saved the gmail certificate as a .pem file in the path "C:\\TrustedCertificates\\", but still, i got an invalid certificate handler call.
What is the way to resolve this?
My final aim would be to save new certificates in an untrusted location on disk (i guess this could be handled with an InvalidCertificateHandler implementation) and check the trusted certificates from a trusted location.
Is there a way to bring this about?
vortex
 
Posts: 2
Joined: 16 Mar 2012, 16:39


Return to Support

Who is online

Users browsing this forum: No registered users and 1 guest

cron