winsocket高手请来(200分)

  • 主题发起人 主题发起人 FAQ007
  • 开始时间 开始时间
F

FAQ007

Unregistered / Unconfirmed
GUEST, unregistred user!
为什么不能发邮件!!!

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,winsock, StdCtrls;
const
crlf=#13#10;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
err:integer;
wsadata:twsadata;
fsocket,fport,step:integer;
sockaddrin:tsockaddrin;
fhost,s1:string;
sbuf:array[0..1024] of char;
implementation

{$R *.dfm}
procedure mailsend;
begin
err:=recv(fsocket,sbuf,400,0);
showmessage(sbuf);
s1:=strpas(sbuf);
inc(step);
case step of
1:s1:='helo smtp.sina.com.cn'+crlf;
2:s1:='mail from: <lanxix825@sina.com>'+crlf;
3:s1:='rcpt to: <lanxix825@sina.com>'+crlf;
4:s1:='data'+crlf;
5:s1:='from:"lanxix825"<www.sina.com.cn>'+crlf
+'to:"lanxix825"<www.sina.com.cn>'+crlf
+'subject:come on now.'+crlf+crlf
+'hello!'+crlf
+'.'+crlf;
6:s1:='quit'+crlf;
else
step:=0;
end;
strcopy(sbuf,pchar(s1));
err:=send(fsocket,sbuf,strlen(sbuf),msg_dontroute);
end;

procedure sendpass;
begin
err:=wsastartup($0101,wsadata);
showmessage(inttostr(err));
fsocket := socket(pf_inet, sock_stream,ipproto_ip);
fhost:='stmp.sina.com.cn';
fport:=25;
sockaddrin.sin_addr.s_addr:=inet_addr(pchar(fhost));
sockaddrin.sin_family := pf_inet;
sockaddrin.sin_port :=htons(fport);
err:=connect(fsocket,sockaddrin, sizeof(sockaddrin));
step:=0;
repeat
mailsend;
until step=0;
err:=closesocket(fsocket);
err:=wsacleanup;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
sendpass;
end;

end.
 
为什么不用TNMSMTP?
 
你的好像没有用服务器验证
连登录协议都没有用
 
TNMSMTP的BUG很多,对了,电脑爱好者杂志上有一期有一个详细的例子,而且还支持认证发
信,找来看看,或者干脆用INDY的构件,D6就有
 
我不想控件,只想用winsocket
 
没想到这里还有一个类似的问题。

>inet_addr(PChar(host));
此函数中host必须是IP地址,也就是'192.168.0.1'的形式。如果是'smtp.21cn.com '的形式
应先用GestHostByName()解析得IP地址才行。

将'stmp.sina.com.cn'改成他的IP地址即可。
 
不行呀。我改成了解析后的IP地址,还是说系统忙呀。
 
“系统忙”是什么意思?
 
为什么不对返回信息进行处理:err:=send(fsocket,sbuf,strlen(sbuf),msg_dontroute);

你这样一个接一个执行,也不知道前一步成功没有。

CAsyncSocket::Send
virtual int Send( const void* lpBuf, int nBufLen, int nFlags = 0 );

Return Value

If no error occurs, Send returns the total number of characters sent. (Note that this can be less than the number indicated by nBufLen.) Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling GetLastError. The following errors apply to this member function:

WSANOTINITIALISED A successful AfxSocketInit must occur before using this API.


WSAENETDOWN The Windows Sockets implementation detected that the network subsystem failed.


WSAEACCES The requested address is a broadcast address, but the appropriate flag was not set.


WSAEINPROGRESS A blocking Windows Sockets operation is in progress.


WSAEFAULT The lpBuf argument is not in a valid part of the user address space.


WSAENETRESET The connection must be reset because the Windows Sockets implementation dropped it.


WSAENOBUFS The Windows Sockets implementation reports a buffer deadlock.


WSAENOTCONN The socket is not connected.


WSAENOTSOCK The descriptor is not a socket.


WSAEOPNOTSUPP MSG_OOB was specified, but the socket is not of type SOCK_STREAM.


WSAESHUTDOWN The socket has been shut down; it is not possible to call Send on a socket after ShutDown has been invoked with nHow set to 1 or 2.


WSAEWOULDBLOCK The socket is marked as nonblocking and the requested operation would block.


WSAEMSGSIZE The socket is of type SOCK_DGRAM, and the datagram is larger than the maximum supported by the Windows Sockets implementation.


WSAEINVAL The socket has not been bound with Bind.


WSAECONNABORTED The virtual circuit was aborted due to timeout or other failure.


WSAECONNRESET The virtual circuit was reset by the remote side.
Parameters

lpBuf

A buffer containing the data to be transmitted.

nBufLen

The length of the data in lpBuf in bytes.

nFlags

Specifies the way in which the call is made. The semantics of this function are determined by the socket options and the nFlags parameter. The latter is constructed by combining any of the following values with the C++ OR operator:

MSG_DONTROUTE Specifies that the data should not be subject to routing. A Windows Sockets supplier can choose to ignore this flag; see also the discussion of the SO_DONTROUTE option inWindows Sockets Programming Considerations in the Win32 SDK documentation.


MSG_OOB Send out-of-band data (SOCK_STREAM only).
Remarks

Call this member function to send data on a connected socket. Send is used to write outgoing data on connected stream or datagram sockets. For datagram sockets, care must be taken not to exceed the maximum IP packet size of the underlying subnets, which is given by the iMaxUdpDg element in the WSADATA structure returned by AfxSocketInit. If the data is too long to pass atomically through the underlying protocol, the error WSAEMSGSIZE is returned via GetLastError, and no data is transmitted.

Note that for a datagram socket the successful completion of a Send does not indicate that the data was successfully delivered.

On CAsyncSocket objects of type SOCK_STREAM, the number of bytes written can be between 1 and the requested length, depending on buffer availability on both the local and foreign hosts.

 
后退
顶部