user1092042
user1092042

Reputation: 1295

Code for sending email in C++

Hi guys,

i know there are external libraries out there like jwsmtp and vmime or poco which help you send email in c++. However i am having trouble configuring them and linking them. Therefore i would like to know if anyone has the source code for sending an email in c++(windows 7 os) through my gmail account.

Upvotes: 1

Views: 2545

Answers (3)

Eric
Eric

Reputation: 1697

In case this of use to anyone, here is some C++ code that basically formulates an EMail message with attachments, subject, and body text on Microsoft Windows. It launches the default EMail (MAPI) client with attachments added and other attributes filled in. Alas it does not interact directly with an SMTP server. I use this in our product to get users to send me debug traces and what-not.

If you want to use the code below, you'll need to replace ptiString and ptiStringArray which are just in-house classes for managing strings and string arrays. TObjList is just a class container managing template. Gives an array of is all.

Hope this helps...

the .h

//---------------------------------------------------------------------------
// Only for Microsoft Windows
// uses Mapi32.dll
// uses MAPI to launch the EMail client, add attachments and message body
// to whatever client is in use.  It doesn't seem to work with Windows Live Mail
// see this link:   http://msdn.microsoft.com/en-us/library/windows/desktop/dd296721(v=vs.85).aspx
// and be aware of UAC crap-o-la:
// http://social.msdn.microsoft.com/Forums/office/en-US/63e9f5b2-f5f2-4cf8-bdc2-ca1fad88ebe5/problem-with-outlook-and-mapisendmail-returns-mapiefailure-when-outlook-is-running
//

#ifndef emailsenderH
#define emailsenderH

#include <windows.h>
#include <mapi.h>
#include "ptiString/ptiStringArray.h"
#include "ptiString/TObjList.h"

//---------------------------------------------------------------------------

class EMailSender
{
private:

    HWND m_hLib;

    ptiString Subject;
    ptiStringArray Body;
    TObjList<MapiFileDesc> Attachments;

    // don't copy this.
    EMailSender(const EMailSender &rhs);
    EMailSender &operator =(const EMailSender &rhs);

public:

    EMailSender();
    ~EMailSender();
    void AddAttachment(ptiString &filename);
    void AddAttachments(ptiStringArray &filename);
    void SetMessage(const ptiStringArray &);
    void SetMessage(const wchar_t *);
    void SetSubject(const ptiString &subj);
    void SetSubject(const wchar_t *subj);
    bool isEMailSupported();
    bool Send(HWND hWndParent);

};



#endif

the .cpp

#include "emailsender.h"


//---------------------------------------------------------------------------
EMailSender::EMailSender():m_hLib(0)
{
    m_hLib = LoadLibrary( "mapi32.dll" );
}

EMailSender::~EMailSender()
{
    if(m_hLib)
        FreeLibrary(m_hLib);
}

bool EMailSender::isEMailSupported()
{
    if(m_hLib)
        return(true);

    return(false);
}

void EMailSender::AddAttachment(ptiString &filename)
{
    MapiFileDesc *pFile = Attachments.Add();
    ZeroMemory( pFile, sizeof(MapiFileDesc) );
    pFile->nPosition = (unsigned long)-1;
    char *fname = const_cast<char *>( filename.toUTF8() );

    // convert filename to full path filename
    long length = MAX_PATH;

    char *Buffer = new char[length];
    int retval = GetFullPathName(fname, length, Buffer, 0);

    if(length && length>retval)
    {
        filename = Buffer;
        fname = const_cast<char *>( filename.toUTF8() );
        pFile->lpszPathName = fname;
    }

    delete [] Buffer;
}

void EMailSender::SetSubject(const wchar_t *subj)
{
    Subject = subj;
}

void EMailSender::SetSubject(const ptiString &subj)
{
  Subject = subj;
}

void EMailSender::SetMessage(const wchar_t *msg)
{
    Body = msg;
}

void EMailSender::SetMessage(const ptiStringArray &msg)
{
    Body = msg;
}

void EMailSender::AddAttachments(ptiStringArray &filename)
{
    for(int i=0; i<filename.Count(); i++)
        AddAttachment(filename[i]);
}

bool EMailSender::Send(HWND hWndParent)
{
        if (!m_hLib)
                return false;

        LPMAPISENDMAIL SendMail = (LPMAPISENDMAIL) GetProcAddress(m_hLib, "MAPISendMail");

        if (!SendMail)
                return false;


        MapiMessage message;
        ZeroMemory( &message, sizeof(MapiMessage) );
        message.lpszSubject = (LPSTR) Subject.toUTF8();

        MapiFileDesc *pFile = 0;
        int fcnt = Attachments.Count();
        message.nFileCount = fcnt;

        if(fcnt)
        {
            pFile = new MapiFileDesc[fcnt];
            for(int i=0; i<fcnt; i++)
                memcpy( &pFile[i], Attachments[i], sizeof(MapiFileDesc) );

            message.lpFiles = pFile;
        }


        ptiString note( Body.GetText() );
        char *cnote = const_cast<char *>( note.toUTF8() );
        message.lpszNoteText = cnote;

        int nError = SendMail(0, (ULONG)hWndParent, &message, MAPI_LOGON_UI|MAPI_DIALOG, 0);

        if (nError != SUCCESS_SUCCESS && nError != MAPI_USER_ABORT && nError != MAPI_E_LOGIN_FAILURE)
                return false;

        if(pFile)
        delete [] pFile;
        return true;
}

Upvotes: 0

syvex
syvex

Reputation: 7756

I've always had luck building with Poco. The only trick is that first you have to build OpenSSL and change the Poco build script to its location.

Upvotes: 1

Niklas B.
Niklas B.

Reputation: 95308

If you really want to do it the hard way, you have to use a TLS library like OpenSSL or the Windows Schannel API to establish a TLS connection with the server. An example of how this can be done can be found here: http://www.coastrd.com/c-schannel-smtp

However, I think that it will be much easier to get those external libraries to work.

Upvotes: 2

Related Questions