Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to create a MailTo TLabel?

The Web

How to create a MailTo TLabel?

by  BTecho  Posted    (Edited  )
Here is the code to a simple project, place a TLabel named Label1 on the Form and set these properties

Caption = "SomeEmail@net.com"
Cursor = crHandPoint

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
//OnMouseEnter event of Label1
void __fastcall TForm1::Label1MouseEnter(TObject *Sender)
{
Label1->Font->Color=clBlue;
}
//---------------------------------------------------------------------------
//OnMouseLeave event of Label1
void __fastcall TForm1::Label1MouseLeave(TObject *Sender)
{
Label1->Font->Color=clBlack;
}
//---------------------------------------------------------------------------
//OnClick event of Label1
void __fastcall TForm1::Label1Click(TObject *Sender)
{
//You can also use the Hint property to hold the Email address if you like so you would just change Label1->Caption to Label1->Hint

ShellExecute(Handle,
"open",
String("mailto:"+Label1->Caption).c_str(),
NULL,
NULL,
SW_SHOW);
/*
Syntax to also include subject

ShellExecute(Handle,
"open",
String("mailto:email@net.com?Subject=Test").c_str(),
NULL,
NULL,
SW_SHOW);

Include subject and body

ShellExecute(Handle,
"open",
String("mailto:email@net.com?Subject=Test&Body=Just testing.").c_str(),
NULL,
NULL,
SW_SHOW);


*/

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

Another snippet

AnsiString Body,Subject="Test Subject",Email="Test@net.com",FullStr;

Body = "Test1%0ATest2"; // %0A hexadecimal equivalent of Line feed "\r\n" will not work in this case

FullStr = "mailto:"+Email + "?Subject="+Subject + "&Body="+Body;

ShellExecute(Handle, NULL, FullStr.c_str(), NULL, NULL, SW_SHOWDEFAULT);


For more info on the parameters of ShellExecute and Outlook parameters

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/functions/shellexecute.asp

http://support.microsoft.com/default.aspx?scid=KB;en-us;q192341
Since the above link is specifically aimed toward command line execution we don't have to worry about the other

Hexadecimal equivalents such as

Space ( ) %20
Comma (,) %2C
Question mark (?) %3F
Period (.) %2E
Exclamation point (!) %21
Colon :)) %3A
Semicolon (;) %3B
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top