Request # 30
How to send HTML message with embedded image, encoding, code tables?
A simple example of how to send using Delphi and MAPI HTML formatted messages.
Code snip:
function TWebBrowserProxy.ConstructMSG(const MAPIMessage: IMessage): Boolean;
var
Document: IHTMLDocument2;
Images: THTMLElements;
iCount: Integer;
ImgPath, FileName: string;
FileStream: TStream;
AttachName: String;
Stream: TStream;
Charset: String;
SmartNoAttach: Boolean;
HTML: WideString;
const
imgfile = 'file:///';
LOCALE_ALLOW_NEUTRAL_NAMES = $08000000;
begin
// Result := False;
if not Assigned(FWebBrowser.Document) then
CheckDocument;
if not Supports(FWebBrowser.Document, IHTMLDocument2, Document) then
raise Exception.Create('Invalid HTML document');
SmartNoAttach := False;
try
Images := GetImgElements;
for iCount := 0 to Length(Images) - 1 do
begin
ImgPath := {$IF not Declared(UTF8ToWideString)}
UTF8Decode{$ELSE}
UTF8ToWideString{$IFEND}
(HTTPDecode(AnsiString(String(Images[iCount].getAttribute('SRC', 0)))));
if AnsiStartsText(imgfile, ImgPath) then
begin
Try
FileName := Copy(ImgPath, Length(imgfile) + 1, Length(ImgPath));
FileStream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
AttachName := AddMsgEmbImgAttachment(MAPIMessage, FileStream, 'image' + IntToStr(iCount) + ExtractFileExt(FileName));
AttachName := 'cid:' + AttachName;
Images[iCount].setAttribute('SRC', AttachName, 0);
SmartNoAttach := True;
Finally
FreeAndNil(FileStream)
End;
end;
end;
// Set SmartNoAttach
if SmartNoAttach then
SetNamedPropBoolean(MAPIMessage, PSETID_Common, $8514, True);
Stream := TMemoryStream.Create;
HTML := GetHTML;
Stream.Position := 0;
if HTML <> '' then
Stream.WriteBuffer(HTML[1], Length(HTML) * SizeOf(HTML[1]));
Charset := UpperCase(Document.Charset);
try
NormalizeHTMLStream(Stream, MAPIMessage, GetInternetCodepage(Charset));
SetMsgBodyHtml(MAPIMessage, Stream);
finally
Stream.Free();
end;
Result := True;
except
Result := False;
end;
end;
Download Request #30 as Compiled Application
Download Project (DELPHI 10.4) ZIP file
Source Code: In package

