Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have developed an Outlook Add-in application for sending an email with Zipped attachment file using MAPI lib.I was successful in attaching the files to the outlook email.But when i attach a file in the reply dialog(i.e for the replying the message) using my add-in application its not attaching to the reply dialog and a new email window is started and file is attaching to that email window.Below is the code that i use for attaching the file to outlook.Kindly help me what changes should i do to attach the file to the particular reply message window.

C++
bool SendMail(HWND hWndParent, CString strAttachmentFileName, CString strSubject, CString File)
{
    if (strAttachmentFileName.IsEmpty())
    {
        ErrorFlag = false;
        return false;
    }
    
    if (!hWndParent || !::IsWindow(hWndParent))
    {
        ErrorFlag = false;
        return false;
    }
    
    HINSTANCE hMAPI = ::LoadLibraryA(("MAPI32.DLL"));
    if (!hMAPI)
    {
        ErrorFlag = false;
        return false;
    }
    
    ULONG (PASCAL *SendMail)(ULONG, ULONG_PTR, MapiMessage*, FLAGS, ULONG);
    (FARPROC&)SendMail = GetProcAddress(hMAPI,("MAPISendMail"));
    
    if (!SendMail)
    {
        ErrorFlag = false;
        return false;
    }
    
    
    char szFileName[_MAX_PATH];
    char szPath[_MAX_PATH];
    char szSubject[_MAX_PATH];
    CString FileName = L"";
    CString FilePath = L"";
    CString FileSubject = L"";
    
    FileName = File;
    FilePath = strAttachmentFileName;
    FileSubject = strSubject;
    					
    sprintf_s(szFileName,"%s",(CT2CA)FileName);
    sprintf_s(szPath,"%s",(CT2CA)FilePath);
    sprintf_s(szSubject,"%s",(CT2CA)FileSubject);
    
    MapiFileDesc fileDesc;
    ::ZeroMemory(&fileDesc, sizeof(fileDesc));
    fileDesc.nPosition = (ULONG)-1;
    fileDesc.lpszPathName = szPath;
    fileDesc.lpszFileName = szFileName;
    
    MapiMessage message;
    ::ZeroMemory(&message, sizeof(message));
    message.lpszSubject = szSubject;
    message.nFileCount = 1;
    message.lpFiles = &fileDesc;
    		
    int nError = SendMail(0, (ULONG_PTR)hWndParent, &message, MAPI_DIALOG|MAPI_LOGON_UI, 0);
    		
    if (nError != SUCCESS_SUCCESS && nError != MAPI_USER_ABORT && nError != MAPI_E_LOGIN_FAILURE)
    {
        AfxMessageBox(L"Attachment Error");
        ErrorFlag = false;
        return false;
    }
    
    return true;
}
Posted
Updated 15-Apr-14 21:44pm
v3
Comments
Jochen Arndt 16-Apr-14 3:39am    
I don't think that you can do that with the Simple MAPI interface. You are using MAPISendMail which will start the default mail program (which may not be Outlook) to send a new message.

You may have a look at the Outlook MAPI: http://msdn.microsoft.com/en-us/library/windows/desktop/cc765775.aspx.
Rocky_Bas 17-Apr-14 22:10pm    
Thanks for the reply.
Is their any other way to attach the file in the reply window in Outlook 2010
Jochen Arndt 18-Apr-14 4:14am    
You can use OLE automation to start and control an Outlook instance. So it should be possible to do that. But I have not used OLE automation with Outlook so far and can't help you therefore.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900