在程序中发送邮件有很多种方法,总结如下:
1,HINSTANCE hinst=ShellExecute(NULL,NULL,_T("mailto:fyzh84@126.com?subject=Contact I
Like It&cc=&bcc=&body="),NULL,NULL,SW_SHOWNORMAL);
这种方法使用的是shell,没有指定任何程序。
2,HINSTANCE hinst=ShellExecute(NULL,_T("C://Program Files//Outlook
Express//msimn.exe"),_T("/mailurl:fyzh84@126.com"),NULL,SW_SHOWNORMAL);
3,如果你想要添加附件之类的高级功能,那么建议你使用MAPI开头的windowsAPI来实现。例子代
码:
void __fastcall TForm_MainForm::ToolButton_SendToClick(TObject *Sender)
{
MapiRecipDesc *OriginatorRecipDesc=new MapiRecipDesc;
MapiRecipDesc *recipientRecipDesc=new MapiRecipDesc;
// MapiFileDesc *FileDesc=new MapiFileDesc;
MapiMessage aMapiMessage;
//接收人信息:
recipientRecipDesc-> ulReserved=0;
recipientRecipDesc-> ulRecipClass=MAPI_TO;
recipientRecipDesc-> lpszName=NULL;// "xthmpro_cn ";
recipientRecipDesc-> lpszAddress= "SMTP:xthmpro_cn@sina.com ";//Name
or Address can set noly one;
recipientRecipDesc-> ulEIDSize=0;
recipientRecipDesc-> lpEntryID=NULL;
//发送人信息:
OriginatorRecipDesc-> ulReserved=0;
OriginatorRecipDesc-> ulRecipClass=MAPI_ORIG;
OriginatorRecipDesc-> lpszName=NULL;
OriginatorRecipDesc-> lpszAddress= "SMTP:xthmpro_cn@sina.com ";//Name
or Address can set noly one;
OriginatorRecipDesc-> ulEIDSize=0;
OriginatorRecipDesc-> lpEntryID=NULL;
//Email内容:
aMapiMessage.ulReserved=0;
aMapiMessage.lpszSubject= "Hello! ";
aMapiMessage.lpszNoteText= "OK! ";
aMapiMessage.lpszMessageType=NULL;
aMapiMessage.lpszDateReceived= "2002/9/6/ 22:50 ";
aMapiMessage.lpszConversationID=NULL;
aMapiMessage.flFlags=MAPI_RECEIPT_REQUESTED;
aMapiMessage.lpOriginator=OriginatorRecipDesc;
aMapiMessage.nRecipCount=1;
aMapiMessage.lpRecips=recipientRecipDesc;
aMapiMessage.nFileCount=0;// 1
aMapiMessage.lpFiles=NULL;//FileDesc;
aMessage.Handle=Handle;
aMessage.aMapiMessage=aMapiMessage;
DWORD SendMailThreadID;
HANDLE Hthread=CreateThread
(0,0,SendMailThreadFunc,&aMessage,0,&SendMailThreadID);//参数一定要使用全局变量!
if(Hthread==NULL)
Application-> MessageBox( "Error ",NULL, MB_OK);
}
//---------------------------------------------------------------------------
DWORD CALLBACK SendMailThreadFunc(void *aMessage)
{
AMessage *aaMessage=(AMessage *)aMessage;
FLAGS flFlags=MAPI_DIALOG | MAPI_NEW_SESSION |
MAPI_LOGON_UI;
return MapiSendMail(NULL,(unsigned)aaMessage-> Handle,aaMessage->
aMapiMessage,flFlags,0);
}