home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 217 / DPCS0306DVD.ISO / Toolkit / Internet / FileZilla / Server / FileZilla_Server-0.9.11.exe / source / interface / misc / MailMsg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2003-04-04  |  11.6 KB  |  366 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  Module: MailMsg.cpp
  4. //
  5. //    Desc: See MailMsg.h
  6. //
  7. // Copyright (c) 2003 Michael Carruth
  8. //
  9. ///////////////////////////////////////////////////////////////////////////////
  10.  
  11. #include "stdafx.h"
  12. #include "MailMsg.h"
  13.  
  14. CMailMsg::CMailMsg()
  15. {
  16.     m_sSubject        = _T("");
  17.     m_sMessage        = _T("");
  18.     m_lpCmcLogon      = NULL;
  19.     m_lpCmcSend       = NULL;
  20.     m_lpCmcLogoff     = NULL;
  21.     m_lpMapiLogon     = NULL;
  22.     m_lpMapiSendMail  = NULL;
  23.     m_lpMapiLogoff    = NULL;
  24.     m_bReady          = FALSE;
  25. }
  26.  
  27. CMailMsg::~CMailMsg()
  28. {
  29.     if (m_bReady)
  30.         Uninitialize();
  31. }
  32.  
  33. CMailMsg& CMailMsg::SetFrom(CString sAddress, CString sName)
  34. {
  35.     if (m_bReady || Initialize())
  36.     {
  37.         // only one sender allowed
  38.         if (m_from.size())
  39.             m_from.empty();
  40.  
  41.         m_from[sAddress] = sName;
  42.     }
  43.  
  44.     return *this;
  45. }
  46.  
  47. CMailMsg& CMailMsg::SetTo(CString sAddress, CString sName)
  48. {
  49.    if (m_bReady || Initialize())
  50.    {
  51.       // only one recipient allowed
  52.       if (m_to.size())
  53.          m_to.empty();
  54.  
  55.       m_to[sAddress] = sName;
  56.    }
  57.  
  58.    return *this;
  59. }
  60.  
  61. CMailMsg& CMailMsg::SetCc(CString sAddress, CString sName)
  62. {
  63.    if (m_bReady || Initialize())
  64.    {
  65.       m_cc[sAddress] = sName;
  66.    }
  67.  
  68.    return *this;
  69. }
  70.  
  71. CMailMsg& CMailMsg::SetBc(CString sAddress, CString sName)
  72. {
  73.    if (m_bReady || Initialize())
  74.    {
  75.       m_bcc[sAddress] = sName;
  76.    }
  77.  
  78.    return *this;
  79. }
  80.  
  81. CMailMsg& CMailMsg::AddAttachment(CString sAttachment, CString sTitle)
  82. {
  83.    if (m_bReady || Initialize())
  84.    {
  85.       m_attachments[sAttachment] = sTitle;
  86.    }
  87.  
  88.    return *this;
  89. }
  90.  
  91. BOOL CMailMsg::Send()
  92. {
  93.    // try mapi
  94.    if (MAPISend())
  95.       return TRUE;
  96.  
  97.    // try cmc
  98.    if (CMCSend())
  99.       return TRUE;
  100.  
  101.    return FALSE;
  102. }
  103.  
  104. BOOL CMailMsg::MAPISend()
  105. {
  106.     TStrStrMap::iterator p;
  107.     int                  nIndex = 0;
  108.     int                  nRecipients = 0;
  109.     MapiRecipDesc*       pRecipients = NULL;
  110.     int                  nAttachments = 0;
  111.     MapiFileDesc*        pAttachments = NULL;
  112.     ULONG                status = 0;
  113.     MapiMessage          message;
  114.  
  115.     USES_CONVERSION;
  116.  
  117.     if (m_bReady || Initialize())
  118.     {
  119.         nRecipients = m_to.size() + m_cc.size() + m_bcc.size() + m_from.size();
  120.         if (nRecipients)
  121.             pRecipients = new MapiRecipDesc[nRecipients];
  122.  
  123.         nAttachments = m_attachments.size();
  124.         if (nAttachments)
  125.             pAttachments = new MapiFileDesc[nAttachments];
  126.  
  127.         if (pRecipients)
  128.         {
  129.             if (m_from.size())
  130.             {
  131.                 // set from
  132.                 pRecipients[nIndex].ulReserved                 = 0;
  133.                 pRecipients[nIndex].ulRecipClass               = MAPI_ORIG;
  134.                 pRecipients[nIndex].lpszAddress                = T2A((LPTSTR)(LPCTSTR)m_from.begin()->first);
  135.                 pRecipients[nIndex].lpszName                   = T2A((LPTSTR)(LPCTSTR)m_from.begin()->second);
  136.                 pRecipients[nIndex].ulEIDSize                  = 0;
  137.                 pRecipients[nIndex].lpEntryID                  = NULL;
  138.                 nIndex++;
  139.             }
  140.  
  141.             if (m_to.size())
  142.             {
  143.                 // set to
  144.                 pRecipients[nIndex].ulReserved                 = 0;
  145.                 pRecipients[nIndex].ulRecipClass               = MAPI_TO;
  146.                 pRecipients[nIndex].lpszAddress                = T2A((LPTSTR)(LPCTSTR)m_to.begin()->first);
  147.                 pRecipients[nIndex].lpszName                   = T2A((LPTSTR)(LPCTSTR)m_to.begin()->second);
  148.                 pRecipients[nIndex].ulEIDSize                  = 0;
  149.                 pRecipients[nIndex].lpEntryID                  = NULL;
  150.                 nIndex++;
  151.             }
  152.  
  153.             if (m_cc.size())
  154.             {
  155.                 // set cc's
  156.                 for (p = m_cc.begin(); p != m_cc.end(); p++, nIndex++)
  157.                 {
  158.                     pRecipients[nIndex].ulReserved         = 0;
  159.                     pRecipients[nIndex].ulRecipClass       = MAPI_CC;
  160.                     pRecipients[nIndex].lpszAddress        = T2A((LPTSTR)(LPCTSTR)p->first);
  161.                     pRecipients[nIndex].lpszName           = T2A((LPTSTR)(LPCTSTR)p->second);
  162.                     pRecipients[nIndex].ulEIDSize          = 0;
  163.                     pRecipients[nIndex].lpEntryID          = NULL;
  164.                 }
  165.             }
  166.  
  167.             if (m_bcc.size())
  168.             {
  169.                 // set bcc
  170.                 for (p = m_bcc.begin(); p != m_bcc.end(); p++, nIndex++)
  171.                 {
  172.                     pRecipients[nIndex].ulReserved         = 0;
  173.                     pRecipients[nIndex].ulRecipClass       = MAPI_BCC;
  174.                     pRecipients[nIndex].lpszAddress        = T2A((LPTSTR)(LPCTSTR)p->first);
  175.                     pRecipients[nIndex].lpszName           = T2A((LPTSTR)(LPCTSTR)p->second);
  176.                     pRecipients[nIndex].ulEIDSize          = 0;
  177.                     pRecipients[nIndex].lpEntryID          = NULL;
  178.                 }
  179.             }
  180.         }
  181.  
  182.         if (pAttachments)
  183.         {
  184.             // add attachments
  185.             for (p = m_attachments.begin(), nIndex = 0;
  186.             p != m_attachments.end(); p++, nIndex++)
  187.             {
  188.                 pAttachments[nIndex].ulReserved        = 0;
  189.                 pAttachments[nIndex].flFlags           = 0;
  190.                 pAttachments[nIndex].nPosition         = 0xFFFFFFFF;
  191.                 pAttachments[nIndex].lpszPathName      = T2A((LPTSTR)(LPCTSTR)p->first);
  192.                 pAttachments[nIndex].lpszFileName      = T2A((LPTSTR)(LPCTSTR)p->second);
  193.                 pAttachments[nIndex].lpFileType        = NULL;
  194.             }
  195.         }
  196.  
  197.         message.ulReserved                        = 0;
  198.         message.lpszSubject                       = T2A((LPTSTR)(LPCTSTR)m_sSubject);
  199.         message.lpszNoteText                      = T2A((LPTSTR)(LPCTSTR)m_sMessage);
  200.         message.lpszMessageType                   = NULL;
  201.         message.lpszDateReceived                  = NULL;
  202.         message.lpszConversationID                = NULL;
  203.         message.flFlags                           = 0;
  204.         message.lpOriginator                      = m_from.size() ? pRecipients : NULL;
  205.         message.nRecipCount                       = nRecipients - m_from.size(); // don't count originator
  206.         message.lpRecips                          = nRecipients - m_from.size() ? &pRecipients[m_from.size()] : NULL;
  207.         message.nFileCount                        = nAttachments;
  208.         message.lpFiles                           = nAttachments ? pAttachments : NULL;
  209.  
  210.  
  211.         LHANDLE hMapiSession = NULL;
  212.         m_lpMapiLogon(NULL, NULL, NULL, 0, 0, &hMapiSession);
  213.         status = m_lpMapiSendMail(hMapiSession, 0, &message, MAPI_DIALOG, 0);
  214.         m_lpMapiLogoff(hMapiSession, NULL, 0, 0);
  215.  
  216.         if (pRecipients)
  217.             delete [] pRecipients;
  218.  
  219.         if (nAttachments)
  220.             delete [] pAttachments;
  221.     }
  222.  
  223.     return (SUCCESS_SUCCESS == status);
  224. }
  225.  
  226. BOOL CMailMsg::CMCSend()
  227. {
  228.    TStrStrMap::iterator p;
  229.    int                  nIndex = 0;
  230.    CMC_recipient*       pRecipients;
  231.    CMC_attachment*      pAttachments;
  232.    CMC_session_id       session;
  233.    CMC_return_code      status = 0;
  234.    CMC_message          message;
  235.    CMC_boolean          bAvailable = FALSE;
  236.    CMC_time             t_now = {0};
  237.  
  238.    USES_CONVERSION;
  239.  
  240.    if (m_bReady || Initialize())
  241.    {
  242.       pRecipients = new CMC_recipient[m_to.size() + m_cc.size() + m_bcc.size() + m_from.size()];
  243.       pAttachments = new CMC_attachment[m_attachments.size()];
  244.  
  245.       // set cc's
  246.       for (p = m_cc.begin(); p != m_cc.end(); p++, nIndex++)
  247.       {
  248.          pRecipients[nIndex].name                = T2A((LPTSTR)(LPCTSTR)p->second);
  249.          pRecipients[nIndex].name_type           = CMC_TYPE_INDIVIDUAL;
  250.          pRecipients[nIndex].address             = T2A((LPTSTR)(LPCTSTR)p->first);
  251.          pRecipients[nIndex].role                = CMC_ROLE_CC;
  252.          pRecipients[nIndex].recip_flags         = 0;
  253.          pRecipients[nIndex].recip_extensions    = NULL;
  254.       }
  255.  
  256.       // set bcc
  257.       for (p = m_bcc.begin(); p != m_bcc.end(); p++, nIndex++)
  258.       {
  259.          pRecipients[nIndex].name                = T2A((LPTSTR)(LPCTSTR)p->second);
  260.          pRecipients[nIndex].name_type           = CMC_TYPE_INDIVIDUAL;
  261.          pRecipients[nIndex].address             = T2A((LPTSTR)(LPCTSTR)p->first);
  262.          pRecipients[nIndex].role                = CMC_ROLE_BCC;
  263.          pRecipients[nIndex].recip_flags         = 0;
  264.          pRecipients[nIndex].recip_extensions    = NULL;
  265.       }
  266.  
  267.       // set to
  268.       pRecipients[nIndex].name                   = T2A((LPTSTR)(LPCTSTR)m_to.begin()->second);
  269.       pRecipients[nIndex].name_type              = CMC_TYPE_INDIVIDUAL;
  270.       pRecipients[nIndex].address                = T2A((LPTSTR)(LPCTSTR)m_to.begin()->first);
  271.       pRecipients[nIndex].role                   = CMC_ROLE_TO;
  272.       pRecipients[nIndex].recip_flags            = 0;
  273.       pRecipients[nIndex].recip_extensions       = NULL;
  274.  
  275.       // set from
  276.       pRecipients[nIndex+1].name                 = T2A((LPTSTR)(LPCTSTR)m_from.begin()->second);
  277.       pRecipients[nIndex+1].name_type            = CMC_TYPE_INDIVIDUAL;
  278.       pRecipients[nIndex+1].address              = T2A((LPTSTR)(LPCTSTR)m_from.begin()->first);
  279.       pRecipients[nIndex+1].role                 = CMC_ROLE_ORIGINATOR;
  280.       pRecipients[nIndex+1].recip_flags          = CMC_RECIP_LAST_ELEMENT;
  281.       pRecipients[nIndex+1].recip_extensions     = NULL;
  282.  
  283.       // add attachments
  284.       for (p = m_attachments.begin(), nIndex = 0;
  285.            p != m_attachments.end(); p++, nIndex++)
  286.       {
  287.          pAttachments[nIndex].attach_title       = T2A((LPTSTR)(LPCTSTR)p->second);
  288.          pAttachments[nIndex].attach_type        = NULL;
  289.          pAttachments[nIndex].attach_filename    = T2A((LPTSTR)(LPCTSTR)p->first);
  290.          pAttachments[nIndex].attach_flags       = 0;
  291.          pAttachments[nIndex].attach_extensions  = NULL;
  292.       }
  293.       pAttachments[nIndex-1].attach_flags        = CMC_ATT_LAST_ELEMENT;
  294.  
  295.       message.message_reference                 = NULL;
  296.       message.message_type                      = NULL;
  297.       message.subject                           = T2A((LPTSTR)(LPCTSTR)m_sSubject);
  298.       message.time_sent                         = t_now;
  299.       message.text_note                         = T2A((LPTSTR)(LPCTSTR)m_sMessage);
  300.       message.recipients                        = pRecipients;
  301.       message.attachments                       = pAttachments;
  302.       message.message_flags                     = 0;
  303.       message.message_extensions                = NULL;
  304.  
  305.       status = m_lpCmcQueryConfiguration(
  306.                   0,
  307.                   CMC_CONFIG_UI_AVAIL,
  308.                   (void*)&bAvailable,
  309.                   NULL
  310.                   );
  311.  
  312.       if (CMC_SUCCESS == status && bAvailable)
  313.       {
  314.          status = m_lpCmcLogon(
  315.                      NULL,
  316.                      NULL,
  317.                      NULL,
  318.                      NULL,
  319.                      0,
  320.                      CMC_VERSION,
  321.                      CMC_LOGON_UI_ALLOWED |
  322.                      CMC_ERROR_UI_ALLOWED,
  323.                      &session,
  324.                      NULL
  325.                      );
  326.  
  327.          if (CMC_SUCCESS == status)
  328.          {
  329.             status = m_lpCmcSend(session, &message, 0, 0, NULL);
  330.  
  331.             m_lpCmcLogoff(session, NULL, CMC_LOGON_UI_ALLOWED, NULL);
  332.          }
  333.       }
  334.  
  335.       delete [] pRecipients;
  336.       delete [] pAttachments;
  337.    }
  338.  
  339.    return ((CMC_SUCCESS == status) && bAvailable);
  340. }
  341.  
  342. BOOL CMailMsg::Initialize()
  343. {
  344.    m_hMapi = ::LoadLibrary(_T("mapi32.dll"));
  345.  
  346.    if (!m_hMapi)
  347.       return FALSE;
  348.  
  349.    m_lpCmcQueryConfiguration = (LPCMCQUERY)::GetProcAddress(m_hMapi, "cmc_query_configuration");
  350.    m_lpCmcLogon = (LPCMCLOGON)::GetProcAddress(m_hMapi, "cmc_logon");
  351.    m_lpCmcSend = (LPCMCSEND)::GetProcAddress(m_hMapi, "cmc_send");
  352.    m_lpCmcLogoff = (LPCMCLOGOFF)::GetProcAddress(m_hMapi, "cmc_logoff");
  353.    m_lpMapiLogon = (LPMAPILOGON)::GetProcAddress(m_hMapi, "MAPILogon");
  354.    m_lpMapiSendMail = (LPMAPISENDMAIL)::GetProcAddress(m_hMapi, "MAPISendMail");
  355.    m_lpMapiLogoff = (LPMAPILOGOFF)::GetProcAddress(m_hMapi, "MAPILogoff");
  356.  
  357.    m_bReady = (m_lpCmcLogon && m_lpCmcSend && m_lpCmcLogoff) ||
  358.               (m_lpMapiLogon && m_lpMapiSendMail && m_lpMapiLogoff);
  359.  
  360.    return m_bReady;
  361. }
  362.  
  363. void CMailMsg::Uninitialize()
  364. {
  365.    ::FreeLibrary(m_hMapi);
  366. }