Attaching files to mail programmatically...

  • Thread starter Sukhvinder Lamba via OfficeKB.com
  • Start date
S

Sukhvinder Lamba via OfficeKB.com

Hi,
I'm writing an Outlook Addin using ATL/C++. I create an mail object and
would like to add attachments to mail before i send it. Below is the code I
wrote but it does not work as expected. Can you please suggesct what
changes are required.:

//Creating mail object
_MailItemPtr mail(ptrIDispatch);

mail->put_Subject((BSTR)showFCNDialog.m_strSubject);
mail->put_Body((BSTR)showFCNDialog.m_strMessage);
mail->put_To((BSTR)showFCNDialog.m_strRecipient);
//Display file open dialog to select file to be displayed.
OPENFILENAME ofn;
char szFileName[MAX_PATH] = "";
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn); // SEE NOTE BELOW
ofn.hwndOwner = (HWND)mail->get_Parent(&ptrIDispatch);
ofn.lpstrFilter = _T("Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0");
ofn.lpstrFile = (LPWSTR)szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrDefExt = _T("txt");
GetOpenFileName(&ofn);
//Attach file recived from file open dialog
CComPtr<Outlook::Attachments> spAttachments;
mail->get_Attachments(&spAttachments);
CComPtr<Outlook::Attachment> spAttach;
CComVariant vSource(ofn.lpstrFile);
CComVariant vType(olByValue);
CComVariant vPos(0);
CComVariant vDispName(ofn.lpstrFileTitle);
HRESULT hr = spAttachments->Add(vSource, vType, vPos, vDispName, &spAttach);
if(FAILED(hr)) return;
if(spAttach == NULL) return;
mail->Save();

It would really help if you suggest me the changes I need to make to attach
files to the mail.

Thanks,
Lamba.
 

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