CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2009
    Posts
    12

    Sending email with attachment from visual C++ 2005.

    Hello everyone.

    I am currently working on a MFC application and need to send (non-document) files created by the program over email. (either with SMTP client on the machine or web mail)

    I have searched the web and some forum and cannot find a code that work. They either have errors, or not Unicode compliant, even when converted they still don't work.

    What are the steps necessary to accomplish this. Everything code example seems to use MAPI, but like I said non of them worked.

    see here

    http://www.codeproject.com/Articles/...mail-recipient
    http://www.codeproject.com/Articles/...ents-to-emails

    A simpler approach would be to modify or override the event handler OnFileSendMail() that is built into MFC document view framework and use the menu id ID_FILE_SENDMAIL.

    To get this feature all you have to do is give any menu item the resource id ID_FILE_SENDMAIL and MFC will call the installed mail client for you. The problem is MFC will attach any open document that is in focus when you select the menu item. The file that I wish to send is not associated with the document so I was wondering if there is a way to find the OnFileSendMail() handler and modify it to send my file instead. I am not too keen on how MFC route command handler such as ID_FILE_NEW, ID_FILE_SENDMAIL etc. These methods/functions are hidden from view or I don't know where to find them.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Sending email with attachment from visual C++ 2005.

    I use this code to send mail. Doesn't have an attachment, but I would imagine adding one is simple.

    Code:
    MapiMessage msg;
    memset(&msg, 0, sizeof(MapiMessage));
    msg.lpszSubject = "Put your subject here;
    msg.lpszNoteText = "Put the message here";
    msg.lpszMessageType = NULL;
    
    MapiRecipDesc recipient;
    memset(&recipient, 0, sizeof(MapiRecipDesc));
    recipient.ulRecipClass = MAPI_TO;
    CString cstrAddress = strAddress;
    recipient.lpszAddress = (char*)(const char*)cstrAddress;
    msg.lpRecips = &recipient;
    msg.nRecipCount = 1;
    LPMAPISENDMAIL lpfnMAPISendMail = NULL;
    HINSTANCE hlibMAPI = LoadLibrary("MAPI32.DLL");
    lpfnMAPISendMail = (LPMAPISENDMAIL) GetProcAddress (hlibMAPI, "MAPISendMail");
    return (*lpfnMAPISendMail)(0, (ULONG)hWnd, &msg, 0 , 0);

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Sending email with attachment from visual C++ 2005.

    Quote Originally Posted by Neil1947 View Post
    They either have errors, or not Unicode compliant, even when converted they still don't work.
    ... Everything code example seems to use MAPI, but like I said non of them worked.

    see here

    http://www.codeproject.com/Articles/...mail-recipient
    http://www.codeproject.com/Articles/...ents-to-emails
    Define "they still don't work".
    And have a look at A set of MFC classes to encapsulate sending and receiving mail using Simple MAPI
    Victor Nijegorodov

  4. #4
    Join Date
    Jun 2009
    Posts
    12

    Re: Sending email with attachment from visual C++ 2005.

    Don't work, meaning code compile and link without errors or warning but does absolutely nothing when call upon. Did not invoke the email client installed on the machine.

    The ID_FILE_SENDMAIL / OnFileSendMail() did just that. It pop up MS Outlook and attach the document file to it (ready to send)

    I think the second link create two type that was not declared. hr and CLSID_SendMail.

    Error hr not declared and CLSID_SendMail not declared. I correct that, still pop up no email client.

    HRESULT hr;
    CLSID CLSID_SendMail;

    I insert the else to check for success and sure it fail

    if (SUCCEEDED(hr)) {
    POINTL pt = {0,0};
    DWORD dwEffect = 0;
    pDropTarget->DragEnter(pDataObject, MK_LBUTTON, pt, &dwEffect);
    pDropTarget->Drop(pDataObject, MK_LBUTTON, pt, &dwEffect);

    ::Sleep(6*1000);

    pDropTarget->Release();
    } else AfxMessageBox(L"No instance created. code don't work");

    -----------------------------------------------------------------------------------------------

    In the following code, which header file should I include, MapiMessage undeclared identifier, where is the MapiMessage class?

    MapiMessage msg;
    memset(&msg, 0, sizeof(MapiMessage));
    msg.lpszSubject = "Put your subject here;
    msg.lpszNoteText = "Put the message here";
    msg.lpszMessageType = NULL;

    MapiRecipDesc recipient;
    memset(&recipient, 0, sizeof(MapiRecipDesc));
    recipient.ulRecipClass = MAPI_TO;
    CString cstrAddress = strAddress;
    recipient.lpszAddress = (char*)(const char*)cstrAddress;
    msg.lpRecips = &recipient;
    msg.nRecipCount = 1;
    LPMAPISENDMAIL lpfnMAPISendMail = NULL;
    HINSTANCE hlibMAPI = LoadLibrary("MAPI32.DLL");
    lpfnMAPISendMail = (LPMAPISENDMAIL) GetProcAddress (hlibMAPI, "MAPISendMail");
    return (*lpfnMAPISendMail)(0, (ULONG)hWnd, &msg, 0 , 0);

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Sending email with attachment from visual C++ 2005.

    Quote Originally Posted by Neil1947 View Post
    In the following code, which header file should I include, MapiMessage undeclared identifier, where is the MapiMessage class?
    MSDN and/or Google can provide that information for you.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Sending email with attachment from visual C++ 2005.

    Quote Originally Posted by Neil1947 View Post
    Don't work, meaning code compile and link without errors or warning but does absolutely nothing when call upon. Did not invoke the email client installed on the machine.

    The ID_FILE_SENDMAIL / OnFileSendMail() did just that. It pop up MS Outlook and attach the document file to it (ready to send)

    I think the second link create two type that was not declared. hr and CLSID_SendMail.

    Error hr not declared and CLSID_SendMail not declared. I correct that, still pop up no email client.

    HRESULT hr;
    CLSID CLSID_SendMail;

    I insert the else to check for success and sure it fail

    Code:
    if (SUCCEEDED(hr))  {
        POINTL pt = {0,0};
        DWORD dwEffect = 0;
        pDropTarget->DragEnter(pDataObject, MK_LBUTTON, pt, &dwEffect);
        pDropTarget->Drop(pDataObject, MK_LBUTTON, pt, &dwEffect);
    
        ::Sleep(6*1000);
    
        pDropTarget->Release();
      } else AfxMessageBox(L"No instance created. code don't work");
    Please, use Code tags while posting code snippets!
    Is it the full code or you omitted some important parts of it?
    What is the value of hr and what is the function that returns it?
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured