Send Outlook attachments from Visual C++

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();
 
W

Wei-Dong Xu [MSFT]

Hi Mark,

Thanks for posting in the community!

From my understanding to this issue, you are going to add attachment(s)
into one outlook mail item through the Outlook automation in C++.

You can first obtain the Attachments collection from the MailItem object
and then use Add method to create one attachment. In the parameters of Add
method, you can specify the attachment location, type etc. You will obtain
more information for the Add method of Attachments from MSDN web site:
Add Method (Attachments Collection)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/office97/ht
ml/output/F1/D5/S5B05F.asp

Furthremore, I write one sample code for you.
//Code begin --------------------------------------------------------
...
CString oStr("G:\\OfficeTest\\3723.doc");
COleVariant oAttachLocation(oStr);
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
CAttachments olAths = olMail.get_Attachments();
CAttachment olNewAth = olAths.Add(oAttachLocation,
covOptional,
covOptional,
covOptional);
...
//Code end ----------------------------------------------------------

Please feel free to let me know if you have any further questions. I am
standing by to be of assistance.

Does this answer your question? Thank you for using Microsoft NewsGroup!

Wei-Dong Xu
Microsoft Product Support Services
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Mark Malyj

Thank you, Wei-Dong Xu! That works well. I touched up your code just a
little to make it work:

//Code begin --------------------------------------------------------
...
CString oStr("G:\\OfficeTest\\3723.doc");
COleVariant oAttachLocation(oStr);
COleVariant covOption((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
Attachments olAths = olMail.GetAttachments();
Attachment olNewAth = olAths.Add(oAttachLocation,
covOption,
covOption,
covOption);

...
//Code end ----------------------------------------------------------

- Mark Malyj
 
W

Wei-Dong Xu [MSFT]

Hi Mark,

Cool!

It is my pleasure to be of service for you!

Thank you once more for using Microsoft NewsGroup!

Wei-Dong Xu
Microsoft Product Support Services
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top