Platform SDK: CDO for Windows 2000 |
The following example shows how to work with messages stored in the Simple Mail Transfer Protocol (SMTP) service drop directory.
Dim iDropDir as New CDO.DropDirectory Dim iMsgs as CDO.IMessages Dim iMsg as CDO.Message Dim iStream as ADODB.Stream Dim strTo as String Dim ToRecipients as Variant Dim strEmailName as String Dim strAccountName as String Dim strFileName as String Dim strMailboxDir as String Set iMsgs = iDropDir.GetMessages For Each iMsg in iMsgs strFileName = iMsgs.FileName(iMsg) ' trim to get the short file name ' from the full path strFileName = Right(strFileName, Len(strFileName) - InStrRev(strFileName,"\") ) ' Get the To recipients...and assume they are all local accounts strTo = iMsg.To ToRecipients = Split(strTo,",") Dim j, lpos, rpos, posdiff ' loop through recipients and get account names for each ' Each address will be in either the "Name" <name@micrsoft.com> or ' simply <name@microsoft.com> ' We get the account name by getting the string ' between "<" and "@" For j = LBound(ToRecipients) to UBound(ToRecipients) strEmailName = ToRecipients(j) lpos = InStr(strEmailName,"<") rpos = InStr(strEmailName,"@") posdiff = rpos - lpos - 1 strAccountName = Mid(strEmailName,lpos + 1, posdiff) ' For the purposes of this example, ' each account's mailbox directory resides in the ' directory c:\mailboxes. For user Joe, their account ' directory would be c:\mailboxes\joe strMailboxDir = "c:\mailboxes\" & strAccountName ' Get the message stream Set iStream = iMsg.GetStream ' write the stream to the user's mailbox directory ' the file name is the same as the one in the drop directory iStream.SaveToFile strMailboxDir & "\" & strFileName iStream.Close Set iStream = Nothing Next j Next iMsg ' once we're done, delete the picked up messages ' this deletes the files from the file system as well. iMsgs.DeleteAll Set iMsgs = Nothing Set iDropDir = Nothing
#include "stdafx.h" #import "d:\program files\common files\system\ado\msado15.dll" no_namespace raw_interfaces_only #import <cdosys.dll> no_namespace raw_interfaces_only #include <iostream.h> #include <wchar.h> void main(int argc, char* argv[]) { CoInitialize(NULL); IDropDirectory* pDropDir = NULL; IMessages* pMsgs = NULL; IUnknown* pUnk = NULL; IEnumVARIANT* pEnum = NULL; CoCreateInstance( __uuidof(DropDirectory), NULL, CLSCTX_SERVER, __uuidof(IDropDirectory), (void**)&pDropDir); pDropDir->GetMessages(L"c:\\inetpub\\mailroot\\drop",&pMsgs); long count = 0; pMsgs->get_Count(&count); cout << "message count: " << count << endl; pMsgs->get__NewEnum(&pUnk); pUnk->QueryInterface(__uuidof(IEnumVARIANT),(void**)&pEnum); ULONG cFetched = 0; VARIANT varMsg; VariantInit(&varMsg); while (1) { cFetched = 0; pEnum->Next(1,&varMsg,&cFetched); cout << "fetched: " << cFetched << endl; if(cFetched == 0) break; IMessage* pMsg = NULL; varMsg.pdispVal->QueryInterface(__uuidof(IMessage),(void**)&pMsg); BSTR szFilename; pMsgs->get_FileName(varMsg,&szFilename); varMsg.pdispVal->Release(); // no longer need IDispatch in VARIANT VariantClear(&varMsg); cout << "Filename:" << _bstr_t(szFilename) << endl; wchar_t* pwchar; pwchar = wcsrchr(szFilename,'\\'); _bstr_t mailfilename(pwchar+1); BSTR szTo; pMsg->get_To(&szTo); wchar_t* pWChar = szTo; wchar_t szbuffer[100]; wchar_t chLt = '<'; wchar_t chAt = '@'; wchar_t* pFirst; unsigned int curPos = 0; _bstr_t mailboxesroot = "c:\\mailboxes\\"; for(unsigned int k = 0;k<wcslen(pWChar);k++){ if(pWChar[k] == chLt) { pFirst = &pWChar[k]; curPos = k; } else if(pWChar[k] == chAt) { cout << "amp" << endl; wcsncpy(szbuffer,pFirst+1,k-curPos-1); buffer[k-curPos-1] = (wchar_t)'\0'; _bstr_t filen = mailboxesroot + _bstr_t(buffer) + "\\" + _bstr_t(mailfilename); _Stream* pStrm = NULL; pMsg->GetStream(&pStrm); pStrm->SaveToFile(filen,adSaveCreateOverWrite); pStrm->Release(); } } ::SysFreeString(szTo); ::SysFreeString(szFilename); pMsg->Release(); } // end while(1) CoUninitialize(); }
Dim iDropDir Set iDropDir = CreateObject("CDO.DropDirectory") Dim iMsgs Dim iMsg Dim iStream Dim strTo Dim ToRecipients Dim strEmailName Dim strAccountName Dim strFileName Dim strMailboxDir Set iMsgs = iDropDir.GetMessages For Each iMsg in iMsgs ' process messages Next iMsgs.DeleteAll Set iMsgs = Nothing Set iDropDir = Nothing