[Demo] Sending HTML email. Saving MIME-encoded files.

Demos, code samples. Only questions related to the existing topics are allowed here.
wray
Posts: 38
Joined: Thu Feb 14, 2008 8:36 pm
Location: Romania

No background

Post by wray »

Hi,

first of all, from what i am seeing here, this might be the best support i saw ever. I am still using the demo version of RichView (there were 2 PayPals rejection, i still wonder why since they were PayPal2000, anyway i will use credit card and this is with with paypal :))

Now the issue is like this:

I have to send a HTML email AND i have to write some text over a picture. To do this i figured out i have to set up a background and just andter the text where should be.

The problem is, in Email no background is shown. Everything else is ok, but no background, if i add a picture with AddPictureEx, everything is ok. If i add with BackgroundBitmap, no background is shown in email. The bmp is sent, since the mail had 500k length.

I tried to load a file in Microsoft Word and to play with Wrapping in Format Picture, and i made the picture floating with the text over it. But when i load it in RichView is not floating, is on his own paragraph.

Please advise...

Code is following: (BTW Indy Mime encode works perfectly)
Platform: Borland C++ Builder 5.0, Indy 10

TForm1 *Form1;

TJPEGImage *jp;
TJPEGImage *jp2;
Graphics::TBitmap *bmp;
Graphics::TBitmap *bmp2;

AnsiString GetImageContentType(TGraphic* gr)
{
if (gr->InheritsFrom(__classid(TJPEGImage)))
return "image/jpeg";

else if (gr->InheritsFrom(__classid(Graphics::TBitmap)))
return "image/bmp";

else if (gr->InheritsFrom(__classid(TIcon)))
return "image/x-icon";

else if (gr->InheritsFrom(__classid(TMetafile)))
return "application/octet-stream";

/*
else if (gr->InheritsFrom(__classid(TPNGObject)))
return "image/png";

else if (gr->InheritsFrom(__classid(TGifImage)))
return "image/gif";
*/

else
throw Exception("Unknown graphic format");
}
//---------------------------------------------------------------------------
// THTMLImageItem
//---------------------------------------------------------------------------
__fastcall THTMLImageItem::THTMLImageItem(TCollection* Collection)
: TCollectionItem(Collection)
{
Stream = new TMemoryStream;
}
//---------------------------------------------------------------------------
__fastcall THTMLImageItem::~THTMLImageItem()
{
delete Stream;
}
//---------------------------------------------------------------------------
// THTMLImagesCollection
//---------------------------------------------------------------------------
__fastcall THTMLImagesCollection::THTMLImagesCollection()
: TCollection(__classid(THTMLImageItem))
{
}
//---------------------------------------------------------------------------
THTMLImageItem* __fastcall THTMLImagesCollection::GetItem(int Index)
{
return (THTMLImageItem*)(TCollection::GetItem(Index));
}
//---------------------------------------------------------------------------
void __fastcall THTMLImagesCollection::SetItem(int Index, const THTMLImageItem* Value)
{
TCollection::SetItem(Index, (TCollectionItem*)Value);
}

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::RichViewEdit1SaveImage2(TCustomRichView *Sender,
TGraphic *Graphic, TRVSaveFormat SaveFormat, const AnsiString Path,
const AnsiString ImagePrefix, int &ImageSaveNo, AnsiString &Location,
bool &DoDefault)
{
if (SaveFormat!=rvsfHTML)
return;

bmp2 = new Graphics::TBitmap;
try
{
bmp2->Assign(Graphic);
}
catch(Exception &E)
{
bmp2->Width = Graphic->Width;
bmp2->Height = Graphic->Height;
bmp2->Canvas->Draw(0,0,Graphic);
}

jp2 = new TJPEGImage();
jp2->Assign(bmp2);
delete bmp2;

AnsiString Ext = GraphicExtension((TGraphicClass)(Graphic->ClassType()));
Location = Format("image%d.%s", ARRAYOFCONST((ImageSaveNo, Ext)));
ImageSaveNo++;
THTMLImageItem* Item = (THTMLImageItem*)(HTMLImages->Add());
Graphic->SaveToStream(Item->Stream);
Item->Name = Location;
Item->ContentType = GetImageContentType(Graphic);
Location = "cid:"+Location;
DoDefault = false;

if(jp2 != Graphic) delete jp2;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
jp = new TJPEGImage();
bmp = new Graphics::TBitmap;
HTMLImages = new THTMLImagesCollection;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Gen_actClick(TObject *Sender)
{

AnsiString Target = "www.xxxxxxxxx.com";

bmp->Assign(Image2->Picture->Graphic);

RichViewEdit1->BackgroundBitmap = bmp;
RichViewEdit1->BackgroundStyle = bsCentered;

RichViewEdit1->AddBreakEx(4, rvbsLine, clBlue);
RichViewEdit1->AddNL("Activation code", 1, 1);
RichViewEdit1->AddNL("", 1, 1);

RichViewEdit1->AddNL("",0,0);
RichViewEdit1->AddNL("",0,0);
RichViewEdit1->AddNL("",0,0);
RichViewEdit1->AddNL("",0,0);
RichViewEdit1->AddNL("",0,0);
RichViewEdit1->AddNL("",0,0);
RichViewEdit1->AddNL("",0,0);
RichViewEdit1->AddNL("",0,0);
RichViewEdit1->AddNL("",0,0);
RichViewEdit1->AddNL("",0,0);
RichViewEdit1->AddNL("",0,0);
RichViewEdit1->AddNL("",4,0);
RichViewEdit1->AddNL(" ", 0, 0);
// RichViewEdit1->AddBulletEx("", 0, ImageList1, -1);
RichViewEdit1->Add(" Your activation code is: ", 2);
RichViewEdit1->AddNL(" ", 0, 0);
RichViewEdit1->Add("12345678",0);

RichViewEdit1->AddNL("",0,0);
RichViewEdit1->AddNL("",0,0);

RichViewEdit1->AddBreakEx(4, rvbsLine, clBlue);
// RichViewEdit1->AddBulletEx("", 1, ImageList1, -1);
RichViewEdit1->AddNLTag(Target,3,1, (int)StrNew(Target.c_str()));

RichViewEdit1->Format();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::SendClick(TObject *Sender)
{

AnsiString Result;
AnsiString s,s2;
WideString ws;

HTMLImages->Clear();

//saving text
TMemoryStream* Stream2 = new TMemoryStream();
RichViewEdit1->SaveTextToStreamW("", Stream2, 80, false, false);
Stream2->Seek(0,0);
ws.SetLength(Stream2->Size /2);
if(Stream2->Size != 0)
Stream2->ReadBuffer(ws,Stream2->Size);
s2 = IdEncoderMIME1->EncodeString(IdEncoderMIME1->ClassType(),Utf8Encode(ws));
delete Stream2;


//saving HTML
TStringStream* Stream = new TStringStream("");
RichViewEdit1->SaveHTMLToStreamEx(Stream, "", "Web Archive Demo", "", "", "", "",TRVSaveOptions() << rvsoUseCheckpointsNames << rvsoUTF8);
s = IdEncoderMIME1->EncodeString(IdEncoderMIME1->ClassType(),Stream->DataString); //MimeEncodeString(Stream->DataString);
delete Stream;


// now s contains HTML file without images, base64-encoded. saving it in MIME
AnsiString BOUNDARY_TEXT_AND_HTML = "----=_TEXT_AND_HTML_BOUNDARY_";
AnsiString BOUNDARY_MESSAGE_AND_PICTURES = "----=_MESSAGE_AND_PICS_BOUNDARY_";
AnsiString BOUNDARY_MAIN_AND_ATTACHMENTS = "----=_MAIN_AND_ATTACHMENTS_BOUNDARY_";



Result = "This is a multi-part message in MIME format.\n\n";

if(HTMLImages->Count > 0)
{
Result += "--" + BOUNDARY_MESSAGE_AND_PICTURES+"\n";
Result += "Content-Type: multipart/alternative;\n\tboundary=\"" + BOUNDARY_TEXT_AND_HTML +"\"\n\n--" +BOUNDARY_TEXT_AND_HTML + "\n";
}
else
Result += "--" + BOUNDARY_TEXT_AND_HTML + "\n";

Result += "Content-Type: text/plain;\n\tcharset=\"utf-8\"\nContent-Transfer-Encoding: base64\n\n";
Result += s2;
Result += "\n\n--" + BOUNDARY_TEXT_AND_HTML + "\n";
Result += "Content-Type: text/html;\n\tcharset=\"utf-8\"\nContent-Transfer-Encoding: base64\n\n";
Result += s;
Result += "\n\n--" + BOUNDARY_TEXT_AND_HTML +"--\n";


// saving images
for (int i=0; i<HTMLImages->Count; i++)
{
s2 = "\n--" + BOUNDARY_MESSAGE_AND_PICTURES + "\n";
s.SetLength(HTMLImages->Items->Stream->Size);
HTMLImages->Items->Stream->Position = 0;
HTMLImages->Items->Stream->ReadBuffer(s.c_str(), s.Length());
s = IdEncoderMIME1->EncodeString(IdEncoderMIME1->ClassType(),s);
s2 += "Content-Type: "+HTMLImages->Items->ContentType +";\n\tname=\"" + HTMLImages->Items->Name +"\"\nContent-Transfer-Encoding: base64\nContent-ID: <" + HTMLImages->Items->Name +">\n\n" + s + "\n";
Result += s2;
}

Result += "\n--" + BOUNDARY_MESSAGE_AND_PICTURES + "--\n";


IdMessage1->Clear();
IdMessage1->Date = Now();
IdMessage1->Subject = "[xxxxxxxxxxx] Activation Code !";
IdMessage1->Recipients->Add();
IdMessage1->Recipients->Items[0]->Address = "[email protected]";
IdMessage1->Sender->Address = "[email protected]";
IdMessage1->From->Name = "xxxxxxx Server";
IdMessage1->From->Address = "[email protected]";
IdMessage1->Body->Text = Result;

IdMessage1->ExtraHeaders->Add("MIME-Version: 1.0");


if(HTMLImages->Count > 0)
{
IdMessage1->ExtraHeaders->Add("Content-Type: multipart/related;");
IdMessage1->ExtraHeaders->Add("\ttype=\"multipart/alternative\";");
IdMessage1->ExtraHeaders->Add("\tboundary=\"" + BOUNDARY_MESSAGE_AND_PICTURES +"\"");
}
else
{
IdMessage1->ExtraHeaders->Add("Content-Type: multipart/alternative;");
IdMessage1->ExtraHeaders->Add("\tboundary=\"" + BOUNDARY_TEXT_AND_HTML + "\"");
}

Form1->IdSMTP1->Host = "xxxxxx.ro";
Form1->IdSMTP1->Port = 25;
Form1->IdSMTP1->Username = "[email protected]";
Form1->IdSMTP1->Password = "xxxxxxx";

Form1->IdSMTP1->ConnectTimeout = 2000;
Form1->IdSMTP1->Connect();
Form1->IdSMTP1->Authenticate();

Form1->IdSMTP1->Send(IdMessage1);

Form1->IdSMTP1->Disconnect(true);

HTMLImages->Clear();

}
//---------------------------------------------------------------------------


Thank you in advance
Vlad
Sergey Tkachenko
Site Admin
Posts: 17564
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Please send me a project reproducing the problem. I'll try to see what's wrong.
wray
Posts: 38
Joined: Thu Feb 14, 2008 8:36 pm
Location: Romania

Post by wray »

Ok, i will send a PM.

You can test with your posted code, but translate it from delphi to c++, for me the result was the same, no background in mail.

Please test with a YAHOO mail, this is what i used, not outlook or other pop3 software.

Regards
Sergey Tkachenko
Site Admin
Posts: 17564
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

May be yahoo web interface does not support background images in e-mails?
wray
Posts: 38
Joined: Thu Feb 14, 2008 8:36 pm
Location: Romania

Post by wray »

Well, yes is very possible...

I just started using RichView and i do not know the advanced stuff, and i do not know how the HTML is formated, but if it used CSS styles, then i am almost positive the web interfaces blocks the backgroud...

http://www.xavierfrenette.com/articles/ ... -hotmail-1

Here are some details about what is allowed and what is not allowed on wide used web mails like Gmail, Yahoo and Hotmail

One thing is sure, on Gmail, the background is attached as picture but not displayed as background... Maybe is the case for Yahoo too.

---------------

Tell me if there is any other possibility to display text over a picture ?

If not, i will use headers and footers and hell with background :)

Regards
Vlad
Sergey Tkachenko
Site Admin
Posts: 17564
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

You can use SaveHTMLToStream instead of SaveHTMLToStreamEx to save HTML without CSS.
And you can insert text in table with background picture (background picture can be assigned to the table itself or to table cell)
wray
Posts: 38
Joined: Thu Feb 14, 2008 8:36 pm
Location: Romania

Post by wray »

I tried that too, it just seems webmails like yahoo HATE backgrounds...

Tried cell background and table background - same result :(

BUT after final try i listened to you and saved HTML without CSS and IT WORKS !!!

THANKS - verified solution - cell background and saved without CSS

Payment is ok now, i just wait for key now, see you soon on members forum ;)

Regards
Vlad
dmitry_oz
Posts: 6
Joined: Wed Apr 30, 2008 2:33 am

load email in MIME format into TichViewEdit

Post by dmitry_oz »

Hi Sergey,

do you have DEMO for this?
Sergey Tkachenko
Site Admin
Posts: 17564
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Demo for that?
http://www.trichview.com/support/files/mime.zip contains demos:
- saving EML/MHT file (HTML + images), for Delphi and for C++
- sending HTML email using Indy components (plain text + HTML + images + attached files), for Delphi
dmitry_oz
Posts: 6
Joined: Wed Apr 30, 2008 2:33 am

Post by dmitry_oz »

Sorry, I was not clear enough.

I was asking for DEMO on loading email into RichView.
My understanding is if email has bundled rtf/txt/html it should be possible to do so

Cheers,
Dmitry
Sergey Tkachenko
Site Admin
Posts: 17564
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Sorry, no.
You should use any component that can disassemble mime file into separate parts-files, then use RvHtmlImporter to load HTML.

Note that HTML email has a nested structure like this:
<(text {HTML, image1, image2, ...}) file1, file2...>
If there are no files, the level () does not present.
If there are no images, the level {} does not present.
nikivanov
Posts: 2
Joined: Thu Feb 18, 2010 6:37 pm
Location: Russia
Contact:

Post by nikivanov »

Sergey, could you create a similar example for Clever Internet Suite 7? I would not want to use the The Delphi Inspiration's DIMime unit. Perhaps it can be replaced by Mail Message or Encoder component from CIS 7?
Sergey Tkachenko
Site Admin
Posts: 17564
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

nikivanov, a demo is coming soon
nikivanov
Posts: 2
Joined: Thu Feb 18, 2010 6:37 pm
Location: Russia
Contact:

Post by nikivanov »

Sergey, thank you very much, I'll wait ...
vario_sts
Posts: 2
Joined: Tue Sep 28, 2010 2:20 pm

Hyperlinks

Post by vario_sts »

Hi, i´ve downloaded that indy demo and i´ve put some hyperlinks into the richviewedit and than i´ve send it via html mail, but the hyperlink does not work, it´s only blue and underlined...
What must I change, to use it correctly?
Post Reply