我准备写一个邮件群发的小程序,想用2000 server下的SMTP服务,做邮件发送服务器,那位这样大侠用过?

解决方案 »

  1.   

    HMODULE hMod = LoadLibrary("MAPI32.DLL"); if (hMod == NULL) //加载动态库失败
    {
    AfxMessageBox(AFX_IDP_FAILED_MAPI_LOAD);
    return;
    } //获取发送邮件的函数地址
    ULONG (PASCAL *lpfnSendMail)(ULONG, ULONG, MapiMessage*, FLAGS, ULONG);
    (FARPROC&)lpfnSendMail = GetProcAddress(hMod, "MAPISendMail"); if (lpfnSendMail == NULL)
    {
    AfxMessageBox(AFX_IDP_INVALID_MAPI_DLL);
    return;
    } int nFileCount = m_list.GetCount(); //有多少个附件需要发送 //分配内存保存附件信息 不能使用静态数组,因为不知道要发送附件的个数
    MapiFileDesc* pFileDesc = (MapiFileDesc*)malloc(sizeof(MapiFileDesc) * nFileCount);
    memset(pFileDesc,0,sizeof(MapiFileDesc) * nFileCount); //分配内存保存附件文件路径
    TCHAR* pTchPath = (TCHAR*)malloc(MAX_PATH * nFileCount); CString szText;
    for(int i = 0;i < nFileCount;i++)
    {
    TCHAR* p = pTchPath + MAX_PATH * i;
    m_list.GetText(i,szText);
    strcpy(p,szText); (pFileDesc + i)->nPosition = (ULONG)-1;
    (pFileDesc + i)->lpszPathName = p;
    (pFileDesc + i)->lpszFileName = p;
    } //收件人结构信息
    MapiRecipDesc recip;
    memset(&recip,0,sizeof(MapiRecipDesc));
    recip.lpszAddress = m_szEmailMAPI.GetBuffer(0);
    recip.ulRecipClass = MAPI_TO; //邮件结构信息
    MapiMessage message;
    memset(&message, 0, sizeof(message));
    message.nFileCount = nFileCount; //文件个数
    message.lpFiles = pFileDesc; //文件信息
    message.nRecipCount = 1; //收件人个数
    message.lpRecips  = &recip; //收件人
    message.lpszSubject = m_szSubject.GetBuffer(0); //主题
    message.lpszNoteText= m_szText.GetBuffer(0); //正文内容 //保存本程序窗口指针,因为发完邮件后要返回本程序的窗口
    CWnd* pParentWnd = CWnd::GetSafeOwner(NULL, NULL); //发送邮件
    int nError = lpfnSendMail(0, 0,
    &message, MAPI_LOGON_UI|MAPI_DIALOG, 0); if (nError != SUCCESS_SUCCESS && nError != MAPI_USER_ABORT 
    && nError != MAPI_E_LOGIN_FAILURE)
    {
    AfxMessageBox(AFX_IDP_FAILED_MAPI_SEND);
    } //返回程序
    pParentWnd->SetActiveWindow(); //不要忘了释放分配的内存
    free(pFileDesc);
    free(pTchPath);
    FreeLibrary(hMod);
      

  2.   

    http://www.csdn.net/Develop/read_article.asp?id=21217