Launch the default email client to open the Send Email window with a pre-selected file attachment

I need to add the "Create and send by email" function to our application. Our program creates an output file, and then I have to start the default email client to open the "write message" window, and with the output file selected as an attachment.

I have seen other programs do this, even if the default Thunderbird client is Outlook instead.

+4
source share
2 answers

I ended up using MAPI to achieve it. I used LoadLibrary and GetProcAddress to get the necessary functions.

The code I use is:

bool MailSender::Send(HWND hWndParent, LPCTSTR szSubject) { if (!m_hLib) return false; LPMAPISENDMAIL SendMail; SendMail = (LPMAPISENDMAIL) GetProcAddress(m_hLib, "MAPISendMail"); if (!SendMail) return false; vector<MapiFileDesc> filedesc; for (std::vector<attachment>::const_iterator ii = m_Files.begin(); ii!=m_Files.end(); ii++) { MapiFileDesc fileDesc; ZeroMemory(&fileDesc, sizeof(fileDesc)); fileDesc.nPosition = (ULONG)-1; fileDesc.lpszPathName = (LPSTR) ii->path.c_str(); fileDesc.lpszFileName = (LPSTR) ii->name.c_str(); filedesc.push_back(fileDesc); } std::string subject; if (szSubject) subject = utf16to8(szSubject).c_str(); else { for (std::vector<attachment>::const_iterator ii = m_Files.begin(); ii!=m_Files.end(); ii++) { subject += ii->name.c_str(); if (ii+1 != m_Files.end()) subject += ", "; } } MapiMessage message; ZeroMemory(&message, sizeof(message)); message.lpszSubject = (LPSTR) subject.c_str(); message.nFileCount = filedesc.size(); message.lpFiles = &filedesc[0]; int nError = SendMail(0, (ULONG)hWndParent, &message, MAPI_LOGON_UI|MAPI_DIALOG, 0); if (nError != SUCCESS_SUCCESS && nError != MAPI_USER_ABORT && nError != MAPI_E_LOGIN_FAILURE) return false; return true; } 
+3
source

Using the mailto scheme may be a solution, but it will be difficult due to restrictions on which fields are considered safe (see RFC 2368 and 6067 for details if you want to go this route).

Another solution would be to find out which mail client is installed and, where possible, launch it and specify everything you need using the command line. See here for Thunderbird and here for Outlook.

+2
source

Source: https://habr.com/ru/post/1337359/


All Articles