Richview and SMTP (import into a mail message)

General TRichView support forum. Please post your questions here
Post Reply
alogrep
Posts: 52
Joined: Fri Oct 27, 2006 5:25 pm

Richview and SMTP (import into a mail message)

Post by alogrep »

Hi
Is it possible to import a richview document with graphics and
tables etc... in the BODY of an email (not as an attachment)?
If so, what is the syntax, and what SMTP client component is required? (Would ICS be ok?).
Thanks
Enrico
Sergey Tkachenko
Site Admin
Posts: 17310
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

How to create formatted e-mail message:
http://www.trichview.com/forums/viewtopic.php?t=11

I think this message can be sent by any SMTP component (as text)
alogrep
Posts: 52
Joined: Fri Oct 27, 2006 5:25 pm

tried dmime .... unsuccessfully

Post by alogrep »

Hi Sergey

I tried the dmime example but it does not work, clearly b/c I am doing something wrong.
I have a Form with a TNMSMTP Ccomponent (D5) and a TrichviewEdit component. This is a hidden form, I want to read the content (with graphics) from the file 'letters' and load it
directlyu in the Body of teh email that is sent to the someone. I tried the code below, sending the email to myself, but i get a long 'text' of garbage, rather than the content in the 'letters' file. What I am doinig wrong? Also, how doI use RV_RegisterHTMLGraphicFormat to be able to have any kind of picture in the letters file? I did not find an example in demos

var
Stream:TStringStream;
s,s1: string;
.........

LoadRVFFromField(RichViewEdit1,dm3.letters,'content');
HTMLImages.Clear;
Stream := TStringStream.Create('');
try
RichViewEdit1.SaveHTMLToStreamEx(Stream, '', 'Web Archive Demo', '', '', '', '',
[rvsoUseCheckpointsNames, rvsoUTF8]);
s := MimeEncodeString(Stream.DataString);
finally
Stream.Free;
end;
for i := 0 to HTMLImages.Count-1 do begin
SetLength(s1, HTMLImages.Stream.Size);
HTMLImages.Stream.Position := 0;
HTMLImages.Stream.ReadBuffer(PChar(s1)^, Length(s1));
s1 := MimeEncodeString(s1);
s:=s+s1;
Finalize(s1);
end;
HTMLImages.Clear;
SMTP1.PostMessage.Body.text:=s;
Sergey Tkachenko
Site Admin
Posts: 17310
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

1) Message parts MUST look like unreadable garbage, because they are encoded as 'base64'.
Usually, mailers use 'base64' only for binary data (such as images); for the main text they use encodings like 'quoted-printable'.
But it is ok, e-mail clients must be able to decode 'base64'.

2) You cannot just concatenate mime-encoded strings. All other information is very important.
You can see the saved file in Notepad. The message consists of parts. The first part is for the main HTML document, other parts are for images. Each part is started from the header like:

Code: Select all

------=_BOUNDARY_LINE_
Content-Type: image/bmp
	Name="image0.bmp"
Content-Transfer-Encoding: base64
Content-ID: <image0.bmp>
And the whole message is started from

Code: Select all

MIME-Version: 1.0
Content-Type: multipart/alternative;
	boundary="----=_BOUNDARY_LINE_"
X-Unsent: 1

This is a multi-part message in MIME format.
and finished by

Code: Select all

------=_BOUNDARY_LINE_--
All line breaks and indents are very important.
At the very beginning of the message there are lines "from" and "subject", but I guess SMTP1 will add them itself.

3) This demo has wrong comment:
{ Actually, this demo converts all pictures to Jpegs, so only TJpegImage can
occur here. But you can use RV_RegisterHTMLGraphicFormat to allow other
graphic formats in HTML }

Actually, this demo saves all images in formats as they are. It uses OnSaveImage2 event, and images are passed to this event without conversion to jpegs. For the purposes of e-mail saving, you should convert images to jpegs (or png, or gifs). Change the code for this event to (and add CRVData in uses):

Code: Select all

procedure TForm1.RichViewEdit1SaveImage2(Sender: TCustomRichView;
  Graphic: TGraphic; SaveFormat: TRVSaveFormat; const Path,
  ImagePrefix: String; var ImageSaveNo: Integer; var Location: String;
  var DoDefault: Boolean);
var gr: TGraphic;
    bmp: TBitmap;
begin
  if SaveFormat<>rvsfHTML then
    exit;
  if not (Graphic is TJPEGImage) and not RV_IsHTMLGraphicFormat(Graphic) then begin
    bmp := TBitmap.Create;
    try
      bmp.Assign(Graphic);
    except
      bmp.Width := Graphic.Width;
      bmp.Height := Graphic.Height;
      bmp.Canvas.Draw(0,0, Graphic);
    end;
    gr := TJPEGImage.Create;
    gr.Assign(bmp);
    bmp.Free;
    end
  else
    gr := Graphic;

  Location := Format('image%d.%s', [ImageSaveNo, GraphicExtension(TGraphicClass(gr.ClassType))]);
  inc(ImageSaveNo);
  with HTMLImages.Add as THTMLImageItem do
  begin
    gr.SaveToStream(Stream);
    Name := Location;
    ContentType := GetImageContentType(gr);
  end;
  Location := 'cid:'+Location;
  DoDefault := False;
  if gr<>Graphic then
    gr.Free;
end;
Sergey Tkachenko
Site Admin
Posts: 17310
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Usually, mailers include one more MIME part in the message: plain text alternative of HTML content. It is used by e-mail client that cannot display formatted messages.
But it is not necessary.
alogrep
Posts: 52
Joined: Fri Oct 27, 2006 5:25 pm

Post by alogrep »

I have made the corrections. Now I can create a file as you do in your example, and it works. But if instead of saving that stream to disk I sent it to the Body of the smtp, I get just incomprehensible text in my emai.
Now I have seen there are Indy and ICS smtp copmponent that claim they can indorporate a FILE in the body of the Message, but the fact is I am using a Richview richedit, with a cust_name code so i cannot save to file first, then mail them unless I save one file for each customer.
Anybody knows of a way to send directly form inside the Richview richedit the content to the Body of the emai message?
Thanks
Sergey Tkachenko
Site Admin
Posts: 17310
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

When receiving this e-mail, compare its source with formatted e-mails sent by other mailers. Try to find the difference.
Send this e-mail to me, I'll try to find what's wrong.

That example already creates a single MIME document from multiple files - HTML and images. You should use either code in this example or Indy/ICS features for creating multipart documents, not both of them, they'd conflict with each other. From the point of view of SMTP components, this document must be a plain text.
alogrep
Posts: 52
Joined: Fri Oct 27, 2006 5:25 pm

Post by alogrep »

did you receive my email sample?
Sergey Tkachenko
Site Admin
Posts: 17310
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Yes, sorry for delay, I'll try to answer tomorrow.
alogrep
Posts: 52
Joined: Fri Oct 27, 2006 5:25 pm

Post by alogrep »

Sergey
Any news yet?
btw, I am not using Indy or CCS SMTP. I am using the standard SMTP that comes with Delphi (5, Prof).
Thanks
Post Reply