Html emails with attachments

T

Tomski

Hello!
I've been trying to implement sample code #1 from
http://www.outlookcode.com/d/code/htmlimg.htm and it doesn't work for me.
I use Delphi and Outlook 2000.

I'm successfully logged on. OLMain is a global object:

OLMain.OLS := CoApplication_.Create;
OLMain.NS := OLS.GetNamespace('MAPI');
OLMain.NS.Logon(sUser, sPass, true, true);
....
I add mail:

FEmail := MailItem(OLMain.Outbox.Items.Add(olMailItem));

Set some properties....
....
Now I'm trying to embed pictures:


oSession := MAPI_TLB._Session(CreateOLEObject('MAPI.Session'));
try
sPass := CRMEx.GetLoginData(sUser, sServer); // obtain logon
information
try
if FindFirst(FDir + '*.*', faAnyFile-faDirectory-faVolumeID, sr) =
0 then begin // get all images from certain dir
repeat
if ExtractFileExt(sr.Name) <> '.html' then begin
vAtts := FEmail.Attachments;
vAtt := vAtts.Add(OGetUniversalName(FDir + sr.Name),
olByValue, 1, ExtractFileName(FDir + sr.Name));
FEmail.Close(olSave);
eid := FEmail.EntryID;
sid := OLMain.WorkFolder.StoreID;
FEmail := nil;
vAtts := nil;
vAtt := nil;
try
oSession.Logon(sUser, sPass, true, true, 0, true,
EmptyParam); // I logon to the existing session
try
oMsg := Unassigned;
oMsg := oSession.GetMessage(eid, sid); // here comes an
exception:

// The file C:\Documents and Settings\Administrator\Local
Settings\Application Data\Microsoft\Outlook\mailbox.pst
// is in use and could not be accessed. Close any application that is using
this file, and then try again.
// [Personal Folders - [MAPI_E_EXTENDED_ERROR(80040119)]]

oAttachs := oMsg.Attachments;
oAttach := oAttachs.Item(oAttachs.Count);
colFields := oAttach.Fields;
oField := colFields.Add(CdoPR_ATTACH_MIME_TAG,
'image/jpeg');
oField := colFields.Add(CdoPR_ATTACH_CONTENT_ID,
EncodeBase64(sr.Name));
//HideAttachments

oMsg.Fields.Add('{0620080000000000C000000000000046}0x8514', vbBoolean,
True);
oMsg.Update;
sTresc := StringReplace(sTresc, sr.Name,
'cid:'+EncodeBase64(sr.Name), [rfIgnoreCase, rfReplaceAll]); //html body
except
on E: Exception do
ErrMsg(E.ClassName, E.Message);
end;
finally
FEmail := MailItem(OLMain.Namespace.GetItemFromID(eid,
sid));
oField := Unassigned;
colFields := Unassigned;
oMsg := Unassigned;
oSession.Logoff;
end;
end;
until FindNext(sr) <> 0;
FindClose(sr);
end;
FEmail.HtmlBody := sTresc;
finally
end;
finally
oSession := nil;
end;


Do I have to OLMain.NS.Logoff ?
 
D

Dmitry Streblechenko \(MVP\)

Do not call Session.Logon in CDO with the NewProfile parameter = true.
Request an existing session (already created by Outlook).

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
T

Tomski

Dmitry said:
Do not call Session.Logon in CDO with the NewProfile parameter = true.
Request an existing session (already created by Outlook).

Thanks Dmitry, but that doesn't work. I've tried both true and false with
same result...
 
D

Dmitry Streblechenko \(MVP\)

The trick is to pick up the existing session, so that the PST provider does
not get loaded twice. Did you try Logon with no parameters at all?

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
T

Tomski

Tomski said:
Thanks Dmitry, but that doesn't work. I've tried both true and false
with same result...


And here's one more question:
When I use Session.Logon method, it returns Null. What does it mean? What
shoud I expect?
 
T

Tomski

Dmitry said:
The trick is to pick up the existing session, so that the PST
provider does not get loaded twice. Did you try Logon with no
parameters at all?

Yes, I've tried ALL combinations. It doesn't work even if I set only
NewSession := false.
 
D

Dmitry Streblechenko \(MVP\)

Hmmm... The type library does say that Logon returns variant, but I have
never seen the returned value documented or used anywhere.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
T

Tomski

Dmitry said:
Hmmm... The type library does say that Logon returns variant, but I
have never seen the returned value documented or used anywhere.

Nor did I. Btw it returns Null in my case.
 

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