|
I have the same question about your question2.
who can help us?
Thank you!
|
|
|
|
|
I have fixed Outlook modal behaver with a new thread started in place of SendMail function call before. Function called by thread SendMailBackground call MAPISendMail. Message is not modal any more in my application but it is still modal in Outlook.
code sample:
MapiFileDesc* mapi_files;
mapi_files = new MapiFileDesc[num_of_files];
MapiFileDesc* current_file = mapi_files;
for (i=0; i<num_of_files; i++)
="" {=""
="" zeromemory(current_file,="" sizeof(mapifiledesc));
="" current_file-="">nPosition = (ULONG)-1;
current_file->lpszPathName = localPaths[i];
current_file->lpszFileName = localPaths[i];
current_file++;
}
MapiMessage* message = new MapiMessage;
ZeroMemory(message, sizeof(MapiMessage));
message->lpszSubject = szSubject);
message->nFileCount = num_of_files;
message->lpFiles = mapi_files;
//SendMail(0, (unsigned long)HWND_DESKTOP, message, API_LOGON_UI|MAPI_DIALOG, 0);
DWORD dwThreadId = 0;
CreateThread(NULL, 0,
SendMailBackroung, (LPVOID)message, 0, &dwThreadId);
// end of message sending in primary thread
// SendMailBackground function
DWORD static WINAPI SendMailBackroung(LPVOID ptr)
{
MapiMessage* message = (MapiMessage*)ptr;
ULONG (PASCAL *SendMail)(LHANDLE, ULONG, MapiMessage*, FLAGS, ULONG);
DWORD isOK = 1;
int i = 0;
HINSTANCE hMAPI = LoadLibrary(_T("MAPI32.DLL"));
if(hMAPI)
{
(FARPROC&)SendMail = GetProcAddress(hMAPI, "MAPISendMail");
if (SendMail)
isOK = SendMail(0, (ULONG)HWND_DESKTOP, message, MAPI_LOGON_UI|MAPI_DIALOG, 0);
FreeLibrary(hMAPI);
}
for (i=0; i<message->nFileCount; i++)
{
delete message->lpFiles->lpszFileName;
}
delete message->lpFiles;
delete message->lpszSubject;
delete message->lpszNoteText;
delete message;
return isOK;
}
|
|
|
|
|
Has anyone done this in Borland C++?
|
|
|
|
|
I have got it going now with C++ Builder sort of. I have set the following
#define TCHAR char
#define _T LPCSTR
#define CString char
plus a few other minor changes.
This starts outlook express and everything appears correct however the New Message Window is locked up apart from being able to type into the TO: edit box. So you can't send the mail unless you enter Control N for a new window, then delete new window then the original window works fine as it should. Any ideas?
|
|
|
|
|
Figured it out. Use Hide() to give Outlook Express control followed by a Show() to give it back to the application.
|
|
|
|
|
Can you post the whole list of changes for borland c++ builder please?
|
|
|
|
|
This Borland C++ version is a simple function that sends attachments, subject and body. The only thing missing I would like is an automatic SEND funcion
// EmailTo.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#include <assert.h>
#include <mapidefs.h>
#include <mapi.h>
#pragma hdrstop
#include "EmailTo.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
int EMailTo(TCustomForm *MainForm, HWND hWndParent, string strAddress, string strName, string strAttachmentFileName,
string strSubject, string strBody)
{ // Send an email with attachments
// Typical call:
// EMailTo(this, m_hWnd, "sales@mycompany.com", "sales", "c:\\Devel_Test_Email\\attachment.txt",
// "Here's the subject", "Here is the body");
//
// by JLB 22 May 2006
//
// This was an adaption of an MFC routine
// There were several routines to SetFocus and other things that I have had real bad experiences with
// so they are all commented out and the Hide() and Show() commands take care of them all
// The original MFC commmands and the equivalent BCB commands commented out as follows:
// The MFC commands are commented out with //MFC// and the equivalent
// Borland commands are commented out with //BCB//
// If a command does both thenit is commented out with //BCB&MFC//
//
HINSTANCE hMail = NULL;
hMail = ::LoadLibraryA("MAPI32.DLL");
if (hMail == NULL)
{
//MessageBox(AFX_IDP_FAILED_MAPI_LOAD);
Application->MessageBox("Failed to load MAPI32.DLL","Email Error - Send Failed",
MB_OK | MB_ICONERROR | MB_SYSTEMMODAL);
return 1;
}
assert(hMail != NULL);
ULONG (PASCAL *lpfnSendMail)(ULONG, ULONG, MapiMessage*, FLAGS, ULONG);
(FARPROC&)lpfnSendMail = GetProcAddress(hMail, "MAPISendMail");
if (lpfnSendMail == NULL)
{
//MessageBox(AFX_IDP_INVALID_MAPI_DLL);
Application->MessageBox("Invalid MAPI32.DLL","Email Error - Send Failed",
MB_OK | MB_ICONERROR | MB_SYSTEMMODAL);
return 1;
}
assert(lpfnSendMail != NULL);
TCHAR szOriginator[_MAX_PATH];
TCHAR szAddress[_MAX_PATH];
TCHAR szName[_MAX_PATH];
TCHAR szFileName[_MAX_PATH];
TCHAR szPath[_MAX_PATH];
TCHAR szSubject[_MAX_PATH];
//TCHAR szBody[_MAX_PATH];
int iMaxPath = _MAX_PATH; // _MAX_PATH seems to be 260 so increase it as it doesn't seem to do any damage
char szBody[1024]; // as _MAX_PATH seems to be a minimum rather than a maximum and
// // lpszNoteText is a LPTSTR anyway so this could be increased if required
// Set the attachments
//
strcpy(szAddress, "SMTP:"); // Start with SMTP:
strcat(szAddress, strAddress.c_str()); // Now append the actual email address
strcpy(szName, strName.c_str()); // Name in To: box eg Authorization
strcpy(szPath, strAttachmentFileName.c_str()); // Attachment file name
strcpy(szFileName, strAttachmentFileName.c_str()); // If NULL then taken from szPath
strcpy(szSubject, strSubject.c_str()); // Subject
if(strBody.size() > 1023)
{ // See note just above about increasing body size
Application->MessageBox("Text Body Too Large","Email Error - Text Body > 1023 characters",
MB_OK | MB_ICONERROR | MB_SYSTEMMODAL);
return 1;
}
strcpy(szBody, strBody.c_str()); // Body separated by "\n" for carriage returns
//
// Set the file description
//
MapiFileDesc fileDesc;
::ZeroMemory(&fileDesc, sizeof(fileDesc));
fileDesc.nPosition = (ULONG)-1;
fileDesc.lpszPathName = szPath;
fileDesc.lpszFileName = szFileName;
//
// make a recipient
//
MapiRecipDesc recipDesc;
::ZeroMemory(&recipDesc, sizeof(recipDesc));
recipDesc.lpszName = szName; // Name in To: box eg Authorization
recipDesc.ulRecipClass = MAPI_TO; // As in MAPI_CC or MAPI_BC etc
recipDesc.lpszAddress = szAddress; // Actual email address prepended with SMTP:
//
//
// make an originator - Not used
//
strcpy(szOriginator, "originator@mycompany.com");
MapiRecipDesc origDesc;
::ZeroMemory(&origDesc, sizeof(origDesc));
origDesc.lpszName = szName; //
origDesc.ulRecipClass = MAPI_ORIG; // As in MAPI_CC or MAPI_BC etc
origDesc.lpszAddress = szOriginator; // Actual email address prepended with SMTP:
//
// prepare the message
//
// Set the Subject
MapiMessage message;
::ZeroMemory(&message, sizeof(message));
message.lpszSubject = szSubject;
message.nFileCount = 1;
message.lpFiles = &fileDesc;
message.nRecipCount = 1;
//message.lpOriginator = &origDesc;
message.lpRecips = &recipDesc;
message.lpszNoteText = szBody;
// prepare for modal dialog box
//MFC// AfxGetApp()->EnableModeless(FALSE);
//BCB// HRESULT STDMETHODCALLTYPE EnableModeless(FALSE);
/////HWND hWndTop;
/////HWND* pParentWnd = HWND::GetSafeOwner(NULL, &hWndTop);
// some extra precautions are required to use
// MAPISendMail as it tends to enable the parent
// window in between dialogs (after the login
// dialog, but before the send not dialog).
//MFC//pParentWnd->SetCapture();
//BCB// SetCapture(pParentWnd);
//BCB&MFC// ::SetFocus(NULL);
//MFC// pParentWnd->m_nFlags |= WF_STAYDISABLED;
MainForm->Hide();
int nError = lpfnSendMail(0, (ULONG_PTR)hWndParent, &message, MAPI_LOGON_UI|MAPI_DIALOG, 0);
// after returning from the MAPISendMail call,
// the window must be re-enabled and focus
// returned to the frame to undo the workaround
// done before the MAPI call.
//BCB&MFC// ::ReleaseCapture();
//MFC// pParentWnd->m_nFlags &= ~WF_STAYDISABLED;
MainForm->Show();
//MFC// pParentWnd->EnableWindow(TRUE);
//BCB&MFC// ::SetActiveWindow(NULL);
//MFC// pParentWnd->SetActiveWindow();
//MFC// pParentWnd->SetFocus();
//MFC// if (hWndTop != NULL)
//MFC// ::EnableWindow(hWndTop, TRUE);
//MFC// AfxGetApp()->EnableModeless(TRUE);
if (nError != SUCCESS_SUCCESS && nError != MAPI_USER_ABORT && nError != MAPI_E_LOGIN_FAILURE)
{
// AfxMessageBox(AFX_IDP_FAILED_MAPI_SEND);
Application->MessageBox("Failed to send email","Email Error - Send Failed",
MB_OK | MB_ICONERROR | MB_SYSTEMMODAL);
return 1;
}
::FreeLibrary(hMail);
return 0;
}
=======================================================================
// EmailTo.h
//---------------------------------------------------------------------------
#include <iomanip>
#ifndef EmailToH
#define EmailToH
//---------------------------------------------------------------------------
using namespace std;
int EMailTo(TCustomForm *MainForm, HWND hWndParent, string strAddress, string StrName, string strAttachmentFileName,
string strSubject, string strBody);
#endif
|
|
|
|
|
Hi All,
I tried to send an e-mail and attachment out via Outlook 2003. The e-mail dialog comes up with the right recipient and I 'Send' it. My backend mailer is Exchange Server.
However, a couple of seconds later, I get an undeliverable message error.
Your message did not reach some or all of the intended recipients.
Subject: Error report for xxx.exe
Sent: 15-Mar-06 12:21
The following recipient(s) could not be reached:
'Software Support' on 15-Mar-06 12:21
None of your e-mail accounts could send to this recipient.
When I looked at the OutBox copy of the message there is no 'Sender' and when I dbl-click on the 'To' address, I see a questionable E-mail type - it's the recipient e-mail address. I would have expected it to be 'SMTP', shouldn't I?
If anyone has an idea what is going on here, please let me know.
Thanks in Advance!
Corey.
-- modified at 14:23 Wednesday 15th March, 2006
|
|
|
|
|
Sounds like a malformed string somewhere that causing Exchange Server to barf. Send me the code (fragement) and I'll have a look.
|
|
|
|
|
Thanks David,
here's my snippet.
ULONG nError = lpfnSendMail(0, (ULONG)hWnd, &message, MAPI_LOGON_UI | MAPI_DIALOG, 0);
'message' is (from VC debugger). Let me know if you can't read this:
- &message 0x0011d868 {ulReserved=0 lpszSubject=0x0011d658 "Error report for XXXXX.exe" lpszNoteText=0x00000000 <Bad Ptr> ...} MapiMessage *
ulReserved 0 unsigned long
+ lpszSubject 0x0011d658 "Error report for XXXXX.exe" char *
+ lpszNoteText 0x00000000 <Bad Ptr> char *
+ lpszMessageType 0x00000000 <Bad Ptr> char *
+ lpszDateReceived 0x00000000 <Bad Ptr> char *
+ lpszConversationID 0x00000000 <Bad Ptr> char *
flFlags 0 unsigned long
+ lpOriginator 0x00000000 {ulReserved=??? ulRecipClass=??? lpszName=??? ...} MapiRecipDesc *
nRecipCount 1 unsigned long
- lpRecips 0x0011dcc0 {ulReserved=0 ulRecipClass=1 lpszName=0x0011dab0 "dumps@wirun.homelinux.net" ...} MapiRecipDesc *
ulReserved 0 unsigned long
ulRecipClass 1 unsigned long
+ lpszName 0x0011dab0 "XXXXX@XXXXX.net" char *
+ lpszAddress 0x0011d8a0 "XXXXX@XXXXX.net" char *
ulEIDSize 0 unsigned long
lpEntryID 0x00000000 void *
nFileCount 1 unsigned long
- lpFiles 0x0011e100 {ulReserved=0 flFlags=0 nPosition=4294967295 ...} MapiFileDesc *
ulReserved 0 unsigned long
flFlags 0 unsigned long
nPosition 4294967295 unsigned long
+ lpszPathName 0x0011def0 "e:\04-Apr-06.zip" char *
+ lpszFileName 0x0011dce0 "04-Apr-06.zip" char *
lpFileType 0x00000000 void *
+ hWnd 0x000d0958 {unused=0 } HWND__ *
lpfnSendMail 0x61e074ac unsigned long (unsigned long, unsigned long, MapiMessage *, unsigned long, unsigned long)*
nError 3435973836 unsigned long
|
|
|
|
|
I'm trying to use this in my mfc app. I'm getting a -11 error on the SendMail call.
Any ideas?
Thanks in advance.
|
|
|
|
|
Error 11 is MAPI_E_ATTACHMENT_NOT_FOUND.
Sounds like maybe you've passed in an invalid or non-exist path to the file to be attached. Also, check you've correctly escaped the slashes in the pathname if it's a literal or some combination of literal and parameter.
"c:\wibble\wobble.htm" is not valid
"c:\\wibble\\wobble.htm" is !!
|
|
|
|
|
You need double qoutes
""c:\wibble\wobble.htm""
Greatings,
Preky
|
|
|
|
|
Even i removed the attachment code. but it is not opening mail client with subject.
I am getting error: #define MAPI_E_FAILURE 2.
i am using MFC. and changed szSubject from TCHAR to char.
Thanks
Avinash
|
|
|
|
|
Is there a way to add a MailTo address? I would like a complete message including mailto/cc/bcc/subject/body/attachments. I could live without cc/bcc, but just curious if it's possible.
I haven't found a method that ties it all together.
Much appreciated!
Matt
|
|
|
|
|
Found it. It was there all the time. Thank you Brooks for posting the original message.
TCHAR szFileName[_MAX_PATH];
TCHAR szPath[_MAX_PATH];
TCHAR szSubject[_MAX_PATH];
TCHAR szBody[_MAX_PATH];
TCHAR szAddress[_MAX_PATH];
TCHAR szSupport[_MAX_PATH];
strcpy(szFileName, (LPCTSTR)strAttachmentFileName);
strcpy(szPath, (LPCTSTR)strAttachmentFileName);
strcpy(szSubject, (LPCTSTR)strSubject);
strcpy(szBody, (LPCTSTR)strSubject);
strcpy(szAddress, _T("SMTP:support@generalcadd.com"));
strcpy(szSupport, _T("Support"));
MapiFileDesc fileDesc;
ZeroMemory(&fileDesc, sizeof(fileDesc));
fileDesc.nPosition = (ULONG)-1;
fileDesc.lpszPathName = szPath;
fileDesc.lpszFileName = szFileName;
MapiRecipDesc recipDesc;
ZeroMemory(&recipDesc, sizeof(recipDesc));
recipDesc.lpszName = szSupport;
recipDesc.ulRecipClass = MAPI_TO;
recipDesc.lpszAddress = szAddress;
MapiMessage message;
ZeroMemory(&message, sizeof(message));
message.nRecipCount = 1;
message.lpRecips = &recipDesc;
message.lpszSubject = szSubject;
message.lpszNoteText = szBody;
message.nFileCount = 1;
message.lpFiles = &fileDesc;
int nError = SendMail(0, m_hWndParent, &message, MAPI_LOGON_UI|MAPI_DIALOG, 0);
|
|
|
|
|
Hi Matt,
Thanks for your code. I had tried this code and perfectly working in MS Outlook; however, in Lotus Notes, value given in "lpszAddress" doesn't appear. Hence, I am not able to send the mail.
Anything else need to be performed specifically for Lotus Notes?
Regards,
Vinoth
|
|
|
|
|
Can any one help in this regard?
|
|
|
|
|
The code works correctly with Mozilla Thunderbird running on Windows XP.
|
|
|
|
|
But the window is not modal.
|
|
|
|
|
Needed to change a number of things to get this puppy to work.
But, it worked fine after that.
Here is the code with the changes:
#include "stdafx.h"
#include <<mapi.h>
class CSendFileTo
{
HWND m_hWndParent;
enum Error {SUCCESS, NO_ATTACHMENT=0x1001, NOT_A_WINDOW, LIB_LOAD_FAILED, GETPROC_FAILED};
public:
CSendFileTo(HWND hWnd)
{
m_hWndParent = hWnd;
}
unsigned int SendMail( CString const &strAttachmentFileName,
CString const &strSubject=_T(""))
{
if (strAttachmentFileName.IsEmpty())
return NO_ATTACHMENT;
if (!m_hWndParent || !::IsWindow(m_hWndParent))
return NOT_A_WINDOW;
HINSTANCE hMAPI = ::LoadLibraryA(_T("MAPI32.DLL"));
if (!hMAPI)
return LIB_LOAD_FAILED;
ULONG (PASCAL *SendMail)(ULONG, HWND, MapiMessage*, FLAGS, ULONG);
(FARPROC&)SendMail = GetProcAddress(hMAPI, _T("MAPISendMail"));
if (!SendMail)
return GETPROC_FAILED;
TCHAR szFileName[_MAX_PATH];
TCHAR szPath[_MAX_PATH];
TCHAR szSubject[_MAX_PATH];
strcpy(szFileName, (LPCTSTR)strAttachmentFileName);
strcpy(szPath, (LPCTSTR)strAttachmentFileName);
strcpy(szSubject, (LPCTSTR)strSubject);
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, m_hWndParent, &message, MAPI_LOGON_UI|MAPI_DIALOG, 0);
if (nError != SUCCESS_SUCCESS && nError != MAPI_USER_ABORT && nError != MAPI_E_LOGIN_FAILURE)
return nError;
return SUCCESS;
}
void ShowError(UINT errCod)
{
if (errCod)
{
CString str;
str.Format("Error: %X", errCod);
AfxMessageBox(str);
}
}
};
#endif
|
|
|
|
|
Thanks for these changes - I'm glad it's useful. I've sort of ignored MFC since discovering the wonders of WTL. I'd be interested to know if it works with other non-Microsoft mail client.
|
|
|
|
|
When I compile have this error..
CSendFileTo sendTo;
error C2512: 'CSendFileTo' : no appropriate default constructor available
Could you Help me? thanks
|
|
|
|
|
Sorry, I solver this problem. CSendFileTo sendTo( m_hWnd ) Thanks
|
|
|
|
|
I tried your code for MFC, but i get following errors:
error C2664:'LoadLibraryA' : cannot convert parameter 1 from 'const wchar_t [11]' to 'LPCSTR'
error C2664: 'GetProcAddress' : cannot convert parameter 2 from 'const wchar_t [13]' to 'LPCSTR'
error C2664: 'strcpy' : cannot convert parameter 2 from 'LPCTSTR' to 'const char *'
Any idea? why this happened? how to slove it?
Thanks
avinash
|
|
|
|
|