⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mail.cxx

📁 opal的ptlib c++源程序 可以从官方网站上下载
💻 CXX
📖 第 1 页 / 共 2 页
字号:
/*
 * mail.cxx
 *
 * Electronic mail class.
 *
 * Portable Windows Library
 *
 * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 *
 * The Original Code is Portable Windows Library.
 *
 * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
 *
 * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
 * All Rights Reserved.
 *
 * Contributor(s): ______________________________________.
 *
 * $Revision: 19426 $
 * $Author: rjongbloed $
 * $Date: 2008-02-09 03:19:51 +0000 (Sat, 09 Feb 2008) $
 */

#include <ptlib.h>
#include <ptlib/mail.h>

#include <ctype.h>

#if P_HAS_CMC
#include <xcmcext.h>
#include <xcmcmsxt.h>
#endif


#define new PNEW


//////////////////////////////////////////////////////////////////////////////
// PMail

PMail::PMail()
{
  Construct();
}


PMail::PMail(const PString & username, const PString & password)
{
  Construct();
  LogOnCommonInterface(username, password, NULL);
}


PMail::PMail(const PString & username,
             const PString & password,
             const PString & service)
{
  Construct();
  LogOnCommonInterface(username, password, service);
}


PMail::~PMail()
{
  LogOff();
}


void PMail::Construct()
{
  loggedOn = PFalse;
  hUserInterface = NULL;
}


PBoolean PMail::LogOn(const PString & username, const PString & password)
{
  return LogOnCommonInterface(username, password, NULL);
}


PBoolean PMail::LogOn(const PString & username,
                  const PString & password,
                  const PString & service)
{
  return LogOnCommonInterface(username, password, service);
}


PBoolean PMail::LogOnCommonInterface(const char * username,
                                 const char * password,
                                 const char * service)
{
  if (!LogOff())
    return PFalse;

#if P_HAS_CMC
  if (cmc.IsLoaded()) {
    CMC_X_COM_support support[2];
    support[0].item_code = CMC_XS_COM;
    support[0].flags = 0;
    support[1].item_code = CMC_XS_MS;
    support[1].flags = 0;
    CMC_extension extension;
    extension.item_code = CMC_X_COM_SUPPORT_EXT;
    extension.item_data = PARRAYSIZE(support);
    extension.item_reference = support;
    extension.extension_flags = CMC_EXT_LAST_ELEMENT;
    lastError = cmc.logon((CMC_string)service,
                          (CMC_string)username,
                          (CMC_string)password,
                          NULL,
                          (CMC_ui_id)hUserInterface,
                          100,
                          hUserInterface == NULL ? 0 :
                                 (CMC_LOGON_UI_ALLOWED | CMC_ERROR_UI_ALLOWED),
                          &sessionId,
                          &extension);
    loggedOn = lastError == CMC_SUCCESS;
    return loggedOn;
  }
#endif

#if P_HAS_MAPI
  if (mapi.IsLoaded()) {
    PAssert(service == NULL, "Cannot have variable services");

    lastError = mapi.Logon((HWND)hUserInterface, username, password, 0, 0, &sessionId);
    loggedOn = lastError == SUCCESS_SUCCESS;
    return loggedOn;
  }
#endif

  lastError = 1;
  return PFalse;
}


PBoolean PMail::LogOff()
{
  if (!loggedOn)
    return PTrue;

#if P_HAS_CMC
  if (cmc.IsLoaded()) {
    lastError = cmc.logoff(sessionId,
                           (CMC_ui_id)hUserInterface,
                           hUserInterface == NULL ? 0
                              : (CMC_LOGOFF_UI_ALLOWED | CMC_ERROR_UI_ALLOWED),
                           NULL);
    switch (lastError) {
      case CMC_SUCCESS :
      case CMC_E_INVALID_SESSION_ID :
      case CMC_E_USER_NOT_LOGGED_ON :
        loggedOn = PFalse;
    }
    return lastError == CMC_SUCCESS;
  }
#endif

#if P_HAS_MAPI
  if (mapi.IsLoaded()) {
    lastError = mapi.Logoff(sessionId, (HWND)hUserInterface, 0, 0);
    if (lastError == SUCCESS_SUCCESS || lastError == MAPI_E_INVALID_SESSION)
      loggedOn = PFalse;
    return lastError == SUCCESS_SUCCESS;
  }
#endif

  lastError = 1;
  return PFalse;
}


PBoolean PMail::IsLoggedOn() const
{
  return loggedOn;
}


PBoolean PMail::SendNote(const PString & recipient,
                     const PString & subject,
                     const char * body)
{
  PStringList dummy;
  return SendNote(recipient, dummy, dummy, subject, body, dummy);
}


PBoolean PMail::SendNote(const PString & recipient,
                     const PString & subject,
                     const char * body,
                     const PStringList & attachments)
{
  PStringList dummy;
  return SendNote(recipient, dummy, dummy, subject, body, attachments);
}


PBoolean PMail::SendNote(const PString & recipient,
                     const PStringList & carbonCopies,
                     const PStringList & blindCarbons,
                     const PString & subject,
                     const char * body,
                     const PStringList & attachments)
{
#if P_HAS_CMC
  if (cmc.IsLoaded()) {
    CMC_message message;
    memset(&message, 0, sizeof(message));

    PINDEX size = carbonCopies.GetSize() + blindCarbons.GetSize() + 1;
    message.recipients = new CMC_recipient[size];
    memset(message.recipients, 0, size*sizeof(CMC_recipient));

    message.recipients[0].role = CMC_ROLE_TO;
    message.recipients[0].name = (CMC_string)(const char *)recipient;

    PINDEX count = 0;
    PStringList::const_iterator i;
    for (i = carbonCopies.begin(); i != carbonCopies.end(); i++) {
      message.recipients[++count].role = CMC_ROLE_CC;
      message.recipients[count].name = (CMC_string)(const char *)*i;
    }
    for (i = blindCarbons.begin(); i != blindCarbons.end(); i++) {
      message.recipients[++count].role = CMC_ROLE_BCC;
      message.recipients[count].name = (CMC_string)(const char *)*i;
    }
    message.recipients[count].recip_flags = CMC_RECIP_LAST_ELEMENT;

    message.subject = (CMC_string)(const char *)subject;
    message.text_note = (CMC_string)body;
    message.message_flags = CMC_MSG_LAST_ELEMENT;

    if (!attachments.IsEmpty()) {
      message.attachments = new CMC_attachment[attachments.GetSize()];
      memset(message.attachments, 0, attachments.GetSize()*sizeof(CMC_attachment));
      count = (PINDEX)-1;
      for (i = attachments.begin(); i != attachments.end(); i++) {
        message.attachments[++count].attach_type = CMC_ATT_OID_BINARY;
        message.attachments[count].attach_filename = (CMC_string)(const char *)*i;
      }
      message.attachments[count].attach_flags = CMC_ATT_LAST_ELEMENT;
    }

    lastError = cmc.send(sessionId, &message, 0, (CMC_ui_id)hUserInterface, NULL);

    delete [] message.attachments;
    delete [] message.recipients;

    return lastError == CMC_SUCCESS;
  }
#endif

#if P_HAS_MAPI
  if (mapi.IsLoaded()) {
    MapiMessage message;
    memset(&message, 0, sizeof(message));

    message.nRecipCount = carbonCopies.GetSize() + blindCarbons.GetSize() + 1;
    message.lpRecips = new MapiRecipDesc[message.nRecipCount];
    memset(message.lpRecips, 0, message.nRecipCount*sizeof(MapiRecipDesc));

    message.lpRecips[0].ulRecipClass = MAPI_TO;
    message.lpRecips[0].lpszName = (char *)(const char *)recipient;

    PINDEX count = 0;
    PStringList::const_iterator i;
    for (i = carbonCopies.begin(); i != carbonCopies.end(); i++) {
      message.lpRecips[++count].ulRecipClass = MAPI_CC;
      message.lpRecips[count].lpszName = (char *)(const char *)*i;
    }
    for (i = blindCarbons.begin(); i != blindCarbons.end(); i++) {
      message.lpRecips[++count].ulRecipClass = MAPI_BCC;
      message.lpRecips[count].lpszName = (char *)(const char *)*i;
    }

    message.lpszSubject = (char *)(const char *)subject;
    message.lpszNoteText = (char *)body;

    if (!attachments.IsEmpty()) {
      message.lpFiles = new MapiFileDesc[attachments.GetSize()];
      memset(message.lpFiles, 0, attachments.GetSize()*sizeof(MapiFileDesc));
      count = 0;
      for (i = attachments.begin(); i != attachments.end(); i++) {
        message.lpFiles[count].nPosition = (DWORD)-1;
        message.lpFiles[count++].lpszPathName = (CMC_string)(const char *)*i;
      }
    }

    lastError = mapi.SendMail(sessionId, (HWND)hUserInterface, &message, 0, 0);

    delete [] message.lpRecips;

    return lastError == SUCCESS_SUCCESS;
  }
#endif

  lastError = 1;
  return PFalse;
}


PStringArray PMail::GetMessageIDs(PBoolean unreadOnly)
{
  PStringArray msgIDs;

#if P_HAS_CMC
  if (cmc.IsLoaded()) {
    CMC_flags flags = CMC_LIST_MSG_REFS_ONLY;
    if (unreadOnly)
      flags |= CMC_LIST_UNREAD_ONLY;
    if (hUserInterface != NULL)
      flags |= CMC_ERROR_UI_ALLOWED;
    CMC_uint32 count = 0;
    CMC_message_summary * messages;

    lastError = cmc.list(sessionId,
                   NULL, flags, NULL, &count, hUserInterface, &messages, NULL);
    if (lastError == CMC_SUCCESS) {
      msgIDs.SetSize((PINDEX)count);
      for (PINDEX m = 0; m < (PINDEX)count; m++) {
        CMC_uint32 c;
        for (c = 0; c < messages[m].message_reference->length; c++)
          if (!isprint(messages[m].message_reference->string[c]))
            break;
        if (c >= messages[m].message_reference->length)
          msgIDs[m] = 'L' + PString(messages[m].message_reference->string,
                                    (PINDEX)messages[m].message_reference->length);
        else {
          PCharArray buf((PINDEX)(messages[m].message_reference->length*2 + 6));
          char * ptr = buf.GetPointer();
          *ptr++ = 'H';
          for (c = 0; c < messages[m].message_reference->length; c++) {
            sprintf(ptr, "%02x", messages[m].message_reference->string[c]);
            ptr += 2;
          }
          msgIDs[m] = buf;
        }
      }
      cmc.free_buf(messages);
    }
    return msgIDs;
  }
#endif

#if P_HAS_MAPI
  if (mapi.IsLoaded()) {
    FLAGS flags = unreadOnly ? MAPI_UNREAD_ONLY : 0;
    PINDEX count = 0;
    const char * seed = NULL;
    char msgIdBuffer[64];

    while ((lastError = mapi.FindNext(sessionId, (HWND)hUserInterface,
                      NULL, seed, flags, 0, msgIdBuffer)) == SUCCESS_SUCCESS) {
      if (count >= msgIDs.GetSize())
        msgIDs.SetSize(count+10);
      msgIDs[count] = msgIdBuffer;
      seed = msgIDs[count++];
    }

    msgIDs.SetSize(count);

    return msgIDs;
  }
#endif

  lastError = 1;
  return msgIDs;
}


#if P_HAS_CMC
class CMC_message_reference_ptr
{
  public:
    CMC_message_reference_ptr(const PString & id);
    ~CMC_message_reference_ptr() { free(ref); }
    operator CMC_message_reference *() { return ref; }
  private:
    CMC_message_reference * ref;
};

CMC_message_reference_ptr::CMC_message_reference_ptr(const PString & id)
{
  PINDEX len = id.GetLength();
  if (id[0] == 'H') {
    ref = (CMC_message_reference *)malloc(sizeof(ref)+(len-1)/2);
    ref->length = (len-1)/2;
    PINDEX i;
    for (i = 0; i < (PINDEX)ref->length; i++) {
      int val = 0;
      PINDEX j;
      for (j = 1; j <= 2; j++) {
        char c = id[i*2+j];
        if (isdigit(c))
          val += c - '0';
        else
          val += toupper(c) - '0' - 7;
      }
      ref->string[i] = (char)val;
    }
  }
  else if (id[0] == 'L') {
    ref = (CMC_message_reference *)malloc(sizeof(ref)+len-1);
    ref->length = len-1;
    memcpy(ref->string, ((const char *)id)+1, len-1);
  }
  else {
    ref = (CMC_message_reference *)malloc(sizeof(ref)+len);
    ref->length = len;
    memcpy(ref->string, (const char *)id, len);
  }
}
#endif


PBoolean PMail::GetMessageHeader(const PString & id,
                             Header & hdrInfo)
{
#if P_HAS_CMC
  if (cmc.IsLoaded()) {
    CMC_flags flags =  hUserInterface != NULL ? CMC_ERROR_UI_ALLOWED : 0;
    CMC_uint32 count = 1;
    CMC_message_summary * message;
    CMC_message_reference_ptr seed = id;

    lastError = cmc.list(sessionId,
                    NULL, flags, seed, &count, hUserInterface, &message, NULL);
    if (lastError != CMC_SUCCESS)
      return PFalse;

    hdrInfo.subject = message->subject;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -