如何编写SMTP邮件服务器 许多时候,我想不通为什么不能直接将信件送到对方(POP或IMAP)服务器上,为什么非 要通 过一个莫名其妙的SMTP邮件服务器转交一次,实在不服气,如果使用UNIX,这个问题就很好 解 决,用sendmail就能完成递送任务,但在Windows下呢?同样的,一定也有办法饶开SMTP Se rver 直接递送到对方的远程邮局服务器上,难得住别人,难得住我们程序员吗?分析相关协议 (RFC2645,RFC821, RFC1846,RFC1939,RFC1725,RFC1730-RFC1733 etc.),我们会知道要完 成直接递送其实相当简单。 首先我们看一下Email的递送过程:Email(Encode) -> a SMTP Relay Server -> Remote SMTP Server(远程邮局)。非常简单,邮件编码后被递送到一个SMTP转交服务器上,该服务 器对 信件分检(到同一邮局的被放在一起)后,根据优先级以及信件的先后次序被发送到远程邮 局的 SMTP服务器上。换句话说,只要我们知道了SMTP转交服务器是如何确定远程邮局SMTP服务器 的地 址的,就可以轻松地将饶开SMTP Relay Server直接递送到远程邮局服务器。 SMTP Relay Server是如何确定远程邮局服务器的地址的呢?如果你熟悉域名解析,就知 道是 怎么回事了,我们知道电子邮件的地址由两部分构成[email protected],邮箱(postbox )和 地址(address.com),给域名服务器发送指令查询“address.com”的远程邮局服务器的地 址即 可找到远程邮局SMTP服务器的IP 地址,该指令查询是被称作MX(Mail Exchange)邮件交换服 务器 的地址查询。远程邮局SMTP服务器的地址可能不止一个,这时,你可根据信件优先级的不同 ,将 对应优先级的信件发到对应地址的远程邮局SMTP服务器,当然,你也可以不管三七二十一, 随便 选一个SMTP服务器发送,见后附“域名解析结果样例”。简单吧。这下,自己编写一个SMTP Server不难了吧! 问题:头ID是个麻烦的事,有时会产生Invalid Head ID 5811的错误。 附:域名解析结果样例 Answer List Resource name is : sina.com Type is : MX Class is : IN MX Priority : 10 MX Server : mailcn.sina.com Resource name is : sina.com Type is : MX Class is : IN MX Priority : 15 MX Server : mail.sina.com.cn Authority List Resource name is : sina.com Type is : NS Class is : IN Domain name is : resolver.sina.com Resource name is : sina.com Type is : NS Class is : IN Domain name is : ns2.sina.com Additional Response List Resource name is : mailcn.sina.com Type is : A Class is : IN IP Address is : 202.106.184.233 Resource name is : mail.sina.com.cn Type is : A Class is : IN IP Address is : 202.106.187.150 Resource name is : resolver.sina.com Type is : A Class is : IN IP Address is : 206.204.114.135 Resource name is : ns2.sina.com Type is : A Class is : IN IP Address is : 209.133.24.135
并且有中文支持,支持SMTP,POP3,WEB等方式。
本公司也有自己的域名,但邮箱是有限的通过该方法可以无限拥有邮箱数目,且对内对外都是有效的。
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
{ Position has to be -1, please see the WinApi Help
for details. }
Attachments^.nPosition := Cardinal(-1);
Attachments^.lpFileType := nil;
Inc(Attachments);
end;
end;
lpFiles := PFiles;
end
else
begin
nFileCount := 0;
lpFiles := nil;
end;
end; { Send the Mail, silent or verbose:
Verbose means in Express a Mail is composed and shown as setup.
In non-Express versions we show the Login-Dialog for a new
session and after we have choosen the profile to use, the
composed email is shown before sending Silent does currently not work for non-Express version. We have
no Session, no Login Dialog so the system refuses to compose a
new email. In Express Versions the email is sent in the
background.
}
if FShowDialog then
MError := MapiSendMail(0, AppHandle, MapiMessage, MAPI_DIALOG or MAPI_LOGON_UI or MAPI_NEW_SESSION, 0)
else
MError := MapiSendMail(0, AppHandle, MapiMessage, 0, 0); { Now we have to process the error messages. There are some
defined in the MAPI unit please take a look at the unit to get
familiar with it.
I decided to handle USER_ABORT and SUCCESS as special and leave
the rest to fire the "new" error event defined at the top (as
generic error) Not treated as special:
MAPI_E_AMBIGUOUS_RECIPIENT,
MAPI_E_ATTACHMENT_NOT_FOUND,
MAPI_E_ATTACHMENT_OPEN_FAILURE,
MAPI_E_BAD_RECIPTYPE,
MAPI_E_FAILURE,
MAPI_E_INSUFFICIENT_MEMORY,
MAPI_E_LOGIN_FAILURE,
MAPI_E_TEXT_TOO_LARGE,
MAPI_E_TOO_MANY_FILES,
MAPI_E_TOO_MANY_RECIPIENTS,
MAPI_E_UNKNOWN_RECIPIENT:
} case MError of
MAPI_E_USER_ABORT:
begin
if Assigned(FOnUserAbort) then
FOnUserAbort(Self);
end;
SUCCESS_SUCCESS:
begin
if Assigned(FOnSuccess) then
FOnSuccess(Self);
end
else begin
if Assigned(FOnMapiError) then
FOnMapiError(Self, MError);
end; end;
finally
{ Finally we do the cleanups, the message should be on its way }
FreeMem(Recipients, MapiMessage.nRecipCount * sizeof(TMapiRecipDesc));
end;
end; {
Please treat this as free. If you improve the component
I would be glad to get a copy.
} end.
2、Web服务器
3、用SQL Server2000建数据库表请大家发表一下自己的看法
如多线程的应用,建库注意事项(每个用户一个表)等等