Mailunit.pas
上传用户:wxp200602
上传日期:2018-04-17
资源大小:172k
文件大小:7k
源码类别:

WEB邮件程序

开发平台:

Delphi

  1. unit Mailunit;
  2. interface
  3. uses
  4.   ComObj, ActiveX, AspTlb, iNotes_TLB, StdVcl,Sysutils,NMSMTP;
  5. type
  6.   TMail = class(TASPObject, IMail)
  7.   protected
  8.     SMTPHost: String;
  9.     SMTPPort: Integer;
  10.     SMTPUser: String;
  11.     SMTPTime: Integer;
  12.     FromAddress: String;
  13.     FromName: String;
  14.     ToAddress: String;
  15.     ToCC: String;
  16.     ToBCC: String;
  17.     Subject: String;
  18.     Body: String;
  19.     Attachments: String;
  20.     MailMsg:String;
  21.     procedure OnEndPage; safecall;
  22.     procedure OnStartPage(const AScriptingContext: IUnknown); safecall;
  23.     function SendMail: OleVariant; safecall;
  24.     function Get_FromAddress: OleVariant; safecall;
  25.     procedure Set_FromAddress(Value: OleVariant); safecall;
  26.     function Get_FromName: OleVariant; safecall;
  27.     procedure Set_FromName(Value: OleVariant); safecall;
  28.     function Get_ToAddress: OleVariant; safecall;
  29.     procedure Set_ToAddress(Value: OleVariant); safecall;
  30.     function Get_ToCC: OleVariant; safecall;
  31.     procedure Set_ToCC(Value: OleVariant); safecall;
  32.     function Get_ToBCC: OleVariant; safecall;
  33.     procedure Set_ToBCC(Value: OleVariant); safecall;
  34.     function Get_Attachments: OleVariant; safecall;
  35.     procedure Set_Attachments(Value: OleVariant); safecall;
  36.     function Get_Body: OleVariant; safecall;
  37.     procedure Set_Body(Value: OleVariant); safecall;
  38.     function Get_Subject: OleVariant; safecall;
  39.     procedure Set_Subject(Value: OleVariant); safecall;
  40.     function Get_SMTPHost: OleVariant; safecall;
  41.     function Get_SMTPPort: SYSINT; safecall;
  42.     procedure Set_SMTPHost(Value: OleVariant); safecall;
  43.     procedure Set_SMTPPort(Value: SYSINT); safecall;
  44.     function Get_SMTPUser: OleVariant; safecall;
  45.     procedure Set_SMTPUser(Value: OleVariant); safecall;
  46.     function Get_SMTPTime: SYSINT; safecall;
  47.     procedure Set_SMTPTime(Value: SYSINT); safecall;
  48.     function CheckAddress(Eaddr:String):Boolean;safecall;
  49.     function CheckMail:Boolean; safecall;
  50.     function Get_Message: OleVariant; safecall;
  51.   end;
  52. implementation
  53. uses ComServ;
  54. procedure TMail.OnEndPage;
  55. begin
  56.   inherited OnEndPage;
  57. end;
  58. procedure TMail.OnStartPage(const AScriptingContext: IUnknown);
  59. begin
  60.   inherited OnStartPage(AScriptingContext);
  61. end;
  62. function TMail.SendMail: OleVariant;
  63. var
  64.   NMSMTP:TNMSMTP;
  65. begin
  66.   if CheckMail then begin
  67.     NMSMTP:=TNMSMTP.Create(nil);
  68.     NMSMTP.Host:=SMTPHost;
  69.     NMSMTP.Port:=SMTPPort;
  70.     NMSMTP.UserID:=SMTPUser;
  71.     NMSMTP.TimeOut:=SMTPTime;
  72.     NMSMTP.PostMessage.FromAddress:=FromAddress;
  73.     NMSMTP.PostMessage.FromName:=FromName;
  74.     NMSMTP.PostMessage.ToAddress.Add(ToAddress);
  75.     NMSMTP.PostMessage.ToCarbonCopy.Add(ToCC);
  76.     NMSMTP.PostMessage.ToBlindCarbonCopy.Add(ToBCC);
  77.     NMSMTP.PostMessage.Subject:=Subject;
  78.     NMSMTP.PostMessage.Body.Text:=Body;
  79.     NMSMTP.PostMessage.Attachments.Text:=Attachments;   
  80.     try
  81.       NMSMTP.Connect ;
  82.       NMSMTP.SendMail;
  83.       MailMsg:='发送邮件成功!';
  84.       Result:=True;
  85.     except
  86.       MailMsg:='发送邮件错误!';
  87.       Result:=False;
  88.     end;
  89.     NMSMTP.Disconnect;
  90.     NMSMTP.Free;
  91.   end
  92.   else begin
  93.     Result:=False;
  94.   end;
  95. end;
  96. function TMail.Get_FromAddress: OleVariant;
  97. begin
  98.   Result:=FromAddress;
  99. end;
  100. procedure TMail.Set_FromAddress(Value: OleVariant);
  101. begin
  102.   FromAddress:=Value;
  103. end;
  104. function TMail.Get_FromName: OleVariant;
  105. begin
  106.   Result:=FromName;
  107. end;
  108. procedure TMail.Set_FromName(Value: OleVariant);
  109. begin
  110.   FromName:=Value;
  111. end;
  112. function TMail.Get_ToAddress: OleVariant;
  113. begin
  114.   Result:=ToAddress;
  115. end;
  116. procedure TMail.Set_ToAddress(Value: OleVariant);
  117. begin
  118.    ToAddress:=Value;
  119. end;
  120. function TMail.Get_ToCC: OleVariant;
  121. begin
  122.    Result:=ToCC;
  123. end;
  124. procedure TMail.Set_ToCC(Value: OleVariant);
  125. begin
  126.    ToCC:=Value;
  127. end;
  128. function TMail.Get_ToBCC: OleVariant;
  129. begin
  130.    Result:=ToBCC;
  131. end;
  132. procedure TMail.Set_ToBCC(Value: OleVariant);
  133. begin
  134.    ToBCC:=Value;
  135. end;
  136. function TMail.Get_Attachments: OleVariant;
  137. begin
  138.    Result:=Attachments;
  139. end;
  140. procedure TMail.Set_Attachments(Value: OleVariant);
  141. begin
  142.   Attachments:=Value;
  143. end;
  144. function TMail.Get_Body: OleVariant;
  145. begin
  146.   Result:=Body;
  147. end;
  148. procedure TMail.Set_Body(Value: OleVariant);
  149. begin
  150.   Body:=Value;
  151. end;
  152. function TMail.Get_Subject: OleVariant;
  153. begin
  154.   Result:=Subject;
  155. end;
  156. procedure TMail.Set_Subject(Value: OleVariant);
  157. begin
  158.   Subject:=Value;
  159. end;
  160. function TMail.Get_SMTPHost: OleVariant;
  161. begin
  162.   Result:=SMTPHost;
  163. end;
  164. function TMail.Get_SMTPPort: SYSINT;
  165. begin
  166.   Result:=SMTPPort;
  167. end;
  168. procedure TMail.Set_SMTPHost(Value: OleVariant);
  169. begin
  170.   SMTPHost:=Value;
  171. end;
  172. procedure TMail.Set_SMTPPort(Value: SYSINT);
  173. begin
  174.   SMTPPort:=Value;
  175. end;
  176. function TMail.Get_SMTPUser: OleVariant;
  177. begin
  178.   Result:=SMTPUser;
  179. end;
  180. procedure TMail.Set_SMTPUser(Value: OleVariant);
  181. begin
  182.   SMTPUser:=Value;
  183. end;
  184. function TMail.Get_SMTPTime: SYSINT;
  185. begin
  186.   Result:=SMTPTime;
  187. end;
  188. procedure TMail.Set_SMTPTime(Value: SYSINT);
  189. begin
  190.   SMTPTime:=Value;
  191. end;
  192. function TMail.CheckMail:Boolean;
  193. begin
  194.   if SMTPHost='' then
  195.   begin
  196.      MailMsg:='发送邮件服务器(SMTP)没找到!';
  197.      Result:=False;
  198.      exit;
  199.   end;
  200.   if FromAddress<>'' then begin
  201.     if not CheckAddress(FromAddress) then begin
  202.        MailMsg:='您的电子邮件地址有错误!';
  203.        Result:=False;
  204.        exit;
  205.     end;
  206.   end
  207.   else begin
  208.       FromAddress:='Unknown';
  209.   end;
  210.   if not CheckAddress(ToAddress) then begin
  211.      MailMsg:='您输入的邮件接收人电子邮件地址有错误!';
  212.      Result:=False;
  213.      exit;
  214.   end;
  215.   if ToCC<>'' then begin
  216.     if not CheckAddress(ToCC) then begin
  217.        MailMsg:='您输入的转送接收人电子邮件地址有错误!';
  218.        Result:=False;
  219.        exit;
  220.     end;
  221.   end;
  222.   if ToBCC<>'' then begin
  223.     if not CheckAddress(ToBCC) then begin
  224.        MailMsg:='您输入的密送接收人电子邮件地址有错误!';
  225.        Result:=False;
  226.        exit;
  227.     end;
  228.   end;
  229.   if Trim(Subject) ='' then Subject:='无主题';
  230.   if Trim(Body) ='' then Body :='无内容';
  231.   Body :=Body+#13#10+#13#10;
  232.   Body :=Body+#13#10+'================================================';
  233.   Body :=Body+#13#10+'          网际备忘中心  世纪倾情奉献            ';
  234.   Body :=Body+#13#10+'            Http://www.ebcall.com               ';
  235.   Body :=Body+#13#10+'================================================';
  236.   Result:=True;
  237. end;
  238. function TMail.CheckAddress(EAddr:String):Boolean;
  239. var
  240.    AtNum:Integer;    AtEnd:Integer;    AtCur:Integer;    CurChr:PChar; begin   {Check Address}    Result:=False;    AtEnd:=Length(EAddr);    if AtEnd<=5 then exit;    for AtCur:=1 to Atend do begin      CurChr:=PChar(Copy(EAddr,AtCur,1));      if CurChr[0] in ['~','`','!','#','$','%','^','&','*','(',')','+','=','|',         '','}','{',']','[','"','''',':',';','<','>',',','?','/'] then        Exit;    end;    AtNum:=Pos('@',EAddr);    if AtNum in [0,1,AtEnd] then Exit;    AtNum:=Pos('.',EAddr);    if AtNum in [0,1,AtEnd] then Exit;    AtNum:=Pos('@.',EAddr);    if AtNum <>0 then Exit;    AtNum:=Pos('.@',EAddr);    if AtNum <>0 then Exit;    Result:=True; end;
  241. function TMail.Get_Message: OleVariant;
  242. begin
  243.   Result:=MailMsg;
  244. end;
  245. initialization
  246.   TAutoObjectFactory.Create(ComServer, TMail, Class_Mail,
  247.     ciMultiInstance, tmApartment);
  248. end.