M
Mark Malyj
I am trying to send emails with attachments from Visual C++ via Outlook.
I have successfully implemented MS Knowledge base article 220600 "HOWTO:
Automate Outlook Using Visual C++/MFC", but can find know example
ANYWHERE how to add attachments with this technique. Below is some
220600 code. I was hoping that the _Mailitem would have a member
function to Add an attachment to an email item - but there's nothing
like a _MailItem.SetAttach!! What to do?
//// KB220600
#include "msoutl85.h" // for Outlook 2000 use msoutl9.h
// for Outlook 2002 & Outlook 2003 use msoutl.h
// Ole-initialization class.
class OleInitClass {
public:
OleInitClass() {
OleInitialize(NULL);
}
~OleInitClass() {
OleUninitialize();
}
};
// This global class calls OleInitialize() at
// application startup, and calls OleUninitialize()
// at application exit...
OleInitClass g_OleInitClass;
//// Now the implementation code:
// Start Outlook.
// If it is already running, you'll use the same instance...
_Application olApp;
COleException e;
if(!olApp.CreateDispatch("Outlook.Application", &e)) {
CString str;
str.Format("CreateDispatch() failed w/error 0x%08lx", e.m_sc);
AfxMessageBox(str, MB_SETFOREGROUND);
return;
}
// Logon. Doesn't hurt if you are already running and logged on...
_NameSpace olNs(olApp.GetNamespace("MAPI"));
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
olNs.Logon(covOptional, covOptional, covOptional, covOptional);
// Prepare a new mail message
_MailItem olMail(olApp.CreateItem(0));
olMail.SetTo("(e-mail address removed)");
olMail.SetSubject("About our meeting...");
olMail.SetBody("Hi James");
// Send the message!
olMail.Send();
AfxMessageBox("All done.", MB_SETFOREGROUND);
olNs.Logoff();
I have successfully implemented MS Knowledge base article 220600 "HOWTO:
Automate Outlook Using Visual C++/MFC", but can find know example
ANYWHERE how to add attachments with this technique. Below is some
220600 code. I was hoping that the _Mailitem would have a member
function to Add an attachment to an email item - but there's nothing
like a _MailItem.SetAttach!! What to do?
//// KB220600
#include "msoutl85.h" // for Outlook 2000 use msoutl9.h
// for Outlook 2002 & Outlook 2003 use msoutl.h
// Ole-initialization class.
class OleInitClass {
public:
OleInitClass() {
OleInitialize(NULL);
}
~OleInitClass() {
OleUninitialize();
}
};
// This global class calls OleInitialize() at
// application startup, and calls OleUninitialize()
// at application exit...
OleInitClass g_OleInitClass;
//// Now the implementation code:
// Start Outlook.
// If it is already running, you'll use the same instance...
_Application olApp;
COleException e;
if(!olApp.CreateDispatch("Outlook.Application", &e)) {
CString str;
str.Format("CreateDispatch() failed w/error 0x%08lx", e.m_sc);
AfxMessageBox(str, MB_SETFOREGROUND);
return;
}
// Logon. Doesn't hurt if you are already running and logged on...
_NameSpace olNs(olApp.GetNamespace("MAPI"));
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
olNs.Logon(covOptional, covOptional, covOptional, covOptional);
// Prepare a new mail message
_MailItem olMail(olApp.CreateItem(0));
olMail.SetTo("(e-mail address removed)");
olMail.SetSubject("About our meeting...");
olMail.SetBody("Hi James");
// Send the message!
olMail.Send();
AfxMessageBox("All done.", MB_SETFOREGROUND);
olNs.Logoff();