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

📄 sendmail.cpp

📁 Windows mobile程序开发的发送邮件的示例程序,Microsoft Embedded virual c++工程。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
        while (*psz == L' ' && *psz != '\0')
        {
            ++psz;
        }

        if (*psz == L'\0')
        {
            break;
        }

        LPWSTR pszEnd = psz;
        while (*pszEnd != L' ' && *pszEnd != ';' && *pszEnd != '\0')
        {
            ++pszEnd;
        }

        int cch = pszEnd - psz;

        wcsncpy(pszLocalRecips, psz, cch);
        *(pszLocalRecips + cch)= L'\0';

        plst->aEntries[plst->cEntries].cValues = 3;
        plst->aEntries[plst->cEntries].rgPropVals = pval;

        // Set the type(To, Cc, Bcc)...
        pval->ulPropTag = PR_RECIPIENT_TYPE;
        pval->Value.ul = ulType;
        ++pval;
        
        // Set the address type(SMTP is the only type currently supported)
        pval->ulPropTag = PR_ADDRTYPE;
        pval->Value.lpszW = L"SMTP";
        ++pval;
        
        // Set the address...
        pval->ulPropTag = PR_EMAIL_ADDRESS;
        pval->Value.lpszW = pszLocalRecips;
        ++pval;

        ++plst->cEntries;

        pszLocalRecips += wcslen(pszLocalRecips)+ 1;

        if (*pszEnd != L'\0')
        {
            ++pszEnd;
        }

        psz = pszEnd;
    }
}



// **************************************************************************
// Function Name: CEMAPI_SetMessageRecipients
// Purpose: Sets a message's recipient(s)to a hard-coded value

// Arguments:
// INOUT LPMESSAGE pmsg - pointer to IMessage interface
//
// Return Values:
//    HRESULT
//    returns S_OK if the message's recipients are successfully modified. Otherwise an error
//	  is returned
// 
// Description:  
//
//	This function defines a hard-coded string which represents the recipients for the given
//	message. The list of recipients is semi-colon delimited. The string for each field(To,
//	Cc, and Bcc)is parsed and a count of recipients is calculated.  A buffer is allocated 
//	to hold the list of recipients. The recipients are added to the the buffer by field type,
//	and the the buffer is passed into IMessage::ModifyRecipients.

HRESULT CEMAPI_SetMessageRecipients(
    LPMESSAGE pmsg
    )
{
    ULONG cRecipients = 0;
    ULONG cchTotalRecips = 0;
	
	//hard-coded recip values. Delimit with semi-colons if more than address is used per field
	//Example: LPWSTR pszTo = L"foo@bar.com;test@bar.com";
    LPWSTR pszTo = L"bogus@domain.example.com"; 
    LPWSTR pszCc = L"";
    LPWSTR pszBcc = L"";
    ADRLIST* plst = NULL;
    LPSPropValue pval = NULL;
    BYTE* pb = NULL;
    LPWSTR pszLocalRecips;
    HRESULT hr;

    if (pszTo)
    {
        cRecipients += CountRecipsInString(pszTo);
        cchTotalRecips += wcslen(pszTo)+ 3;
    }

    if (pszCc)
    {
        cRecipients += CountRecipsInString(pszCc);
        cchTotalRecips += wcslen(pszCc)+ 3;
    }

    if (pszBcc)
    {
        cRecipients += CountRecipsInString(pszBcc);
        cchTotalRecips += wcslen(pszBcc)+ 3;
    }

    //
    // Allocate one big block of memory to hold all the strings.
    // The block is arranged as follows:
    //
    //  ADRLIST               SPropValue's         Copy of addresses
    // |---------------------|--------------------|---------------------|
    //

    DWORD cb = sizeof(ADRLIST)+((sizeof(ADRENTRY)+ sizeof(SPropValue)* 3)* cRecipients);

    pb = new BYTE[cb +(cchTotalRecips * sizeof(WCHAR))];
    if (pb == NULL)
    {
        return E_OUTOFMEMORY;
    }

    ZeroMemory(pb, cb +(cchTotalRecips * sizeof(WCHAR)));

    plst =(ADRLIST*)pb;

    pszLocalRecips =(LPWSTR)(pb + cb);

    pb += sizeof(ADRLIST)+(sizeof(ADRENTRY)* cRecipients);

    pval =(SPropValue*)pb;

    CEMAPI_AddRecipients(pszTo, MAPI_TO, plst, pszLocalRecips, pval);
    CEMAPI_AddRecipients(pszCc, MAPI_CC, plst, pszLocalRecips, pval);
    CEMAPI_AddRecipients(pszBcc, MAPI_BCC, plst, pszLocalRecips, pval);

    hr = pmsg->ModifyRecipients(MODRECIP_ADD, plst);

    delete[](BYTE*)plst;
    
    return hr;
}



// **************************************************************************
// Function Name: CEMAPI_SetMessageProps
// Purpose: Sets some hardcoded properties on a given message

// Arguments:
// INOUT LPMESSAGE pmsg - pointer to IMessage interface
//
// Return Values:
//    HRESULT
//    returns S_OK if the properties are successfully set, otherwise an error is returned
// 
// Description:  
//
//	This function sets up message recipients, sets the message properties, then writes the
//	the message body to the message via an IStream interface.	

HRESULT CEMAPI_SetMessageProps(
    LPMESSAGE pmsg
    )
{
    SPropValue  rgprops[3]  = {0};
    ULONG       cProps      = 0;
    HRESULT     hr;
    LPWSTR      pszSubject  = L"Subject";
    LPWSTR      pszBody     = L"Body.";
    LPSTREAM    pstm        = NULL;


    // Set the recipients up.
	hr = CEMAPI_SetMessageRecipients(pmsg);
    EXIT_ON_FAILED(hr);

    // Set the flags and a subject if they exist.
	rgprops[cProps].ulPropTag = PR_MESSAGE_FLAGS;
    rgprops[cProps].Value.ul = MSGFLAG_FROMME | MSGFLAG_UNSENT;
    ++cProps;
    
    rgprops[cProps].ulPropTag = PR_MSG_STATUS;
    rgprops[cProps].Value.ul = MSGSTATUS_RECTYPE_SMTP;
    ++cProps;

    if (pszSubject != NULL)
    {
        rgprops[cProps].ulPropTag = PR_SUBJECT;
        rgprops[cProps].Value.lpszW = pszSubject;
        ++cProps;
    }
	
	// Apply the property values to this message
    hr = pmsg->SetProps(cProps, rgprops, NULL);
    EXIT_ON_FAILED(hr);

    // Stream the body in...
	hr = pmsg->OpenProperty(PR_BODY, NULL, STGM_WRITE, MAPI_MODIFY, 
        (LPUNKNOWN*)&pstm);
    EXIT_ON_FAILED(hr);

    pstm->Write(pszBody,(wcslen(pszBody)+ 1)* sizeof(WCHAR), NULL);
    pstm->Release();

FuncExit:
    return hr;
}

// **************************************************************************
// Function Name: CEMAPI_AttachFile
// Purpose: Attaches a bogus file to the outgoing message

// Arguments:
// INOUT LPMESSAGE pmsg - pointer to IMessage interface
//
// Return Values:
//    HRESULT
//    returns S_OK if the "file" is successfully attached, otherwise an error is returned
// 
// Description:  
//
//	This function sets up a file attachment and associates it with the specified message,
//	and reads in the file data from via IStream. The "file" in this example is just dummy 
//  data. Normally data from a real file would be streamed in and written to the attachment. 

HRESULT CEMAPI_AttachFile(
    LPMESSAGE pmsg
    )
{
	IAttach * pAttachment = NULL;
	SPropValue rgpropsAttch[3];
	ULONG ulAttachNum;
	HRESULT hr;
	// bogus filename...normally would reference a real file
	WCHAR wcsAttachmentName[] = L"test.txt";
	DWORD dwFileSize;
	ULONG ulStatus = MSGSTATUS_RECTYPE_SMTP;
	IStream * pstmAttachment = NULL;
	// our fake file data, normally would be streamed in from a file
	char szBuf[] = "Hello, Windows CE";
	
	
	dwFileSize = strlen(szBuf)+1;
	// create the attachment
    hr = pmsg->CreateAttach(NULL, 0, &ulAttachNum, &pAttachment);
    EXIT_ON_FAILED(hr);
 
    rgpropsAttch[0].ulPropTag    =   PR_ATTACH_FILENAME;
    rgpropsAttch[0].Value.lpszW  =   wcsAttachmentName;
 
    rgpropsAttch[1].ulPropTag    =   PR_ATTACH_SIZE;
    rgpropsAttch[1].Value.ul     =   dwFileSize;
 
    rgpropsAttch[2].ulPropTag    =   PR_MSG_STATUS;
    rgpropsAttch[2].Value.ul     =   ulStatus;
 
    hr = pAttachment->SetProps(sizeof(rgpropsAttch)/ sizeof(rgpropsAttch[0]), rgpropsAttch, NULL);
    EXIT_ON_FAILED(hr);
 
    // open a stream on the attachment
    hr = pAttachment->OpenProperty(PR_ATTACH_DATA_BIN, NULL /*&IID_IStream*/, STGM_WRITE, MAPI_MODIFY,
                                    reinterpret_cast <IUnknown **>(&pstmAttachment));
    EXIT_ON_FAILED(hr);

    // store chunk from our fake file buffer into the stream
    hr = pstmAttachment->Write(szBuf, 5, NULL);
    EXIT_ON_FAILED(hr);

    // commit it
    hr = pstmAttachment->Commit(STGC_DEFAULT);
    EXIT_ON_FAILED(hr);

   
FuncExit:
	MAPIFreeBuffer(rgpropsAttch);
	RELEASE_OBJ(pAttachment);
	RELEASE_OBJ(pstmAttachment);
	return hr;

}

⌨️ 快捷键说明

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