# Object - SMTP 邮件发送 ## `SMTP` 声明:class SMTP 内置对象 ### `create(host)` 声明:function 创建 SMTP 实例 可见性:public | 参数 | 类型 | 说明 | | --- | --- | --- | | `host` | string | 可选。字符串类型。如果指定了Host参数,会自动从SMTP配置文件里获取其他相应参数,并自动连接 | ### `connect()` 声明:function 连接服务器。需要设置host属性。并且不能多次connect,否则第二次之后的会报错 可见性:public 返回:any #### 示例 ```tsl obj:= CreateObject("smtp") ; obj.Host := 'smtp.tinysoft.com.cn' ; obj.connect(); return 1; ``` ### `disconnect()` 声明:function 断开连接 可见性:public 返回:any #### 示例 ```tsl obj:= CreateObject("smtp") ; obj.Host := 'smtp.tinysoft.com.cn' ; obj.connect(); obj.disconnect(); return 1; ``` ### `disconnectNotifyPeer()` 声明:function 断开并且通知服务器 可见性:public 返回:any #### 示例 ```tsl obj:= CreateObject("smtp") ; obj.Host := 'smtp.tinysoft.com.cn' ; obj.connect(); obj.DisconnectNotifyPeer(); return 1; ``` ### `expand(username)` 声明:function 展开用户名,例如用于邮件列表。需要服务器配置,暂不支持 可见性:public | 参数 | 类型 | 说明 | | --- | --- | --- | | `username` | string | 字符串类型,展开的用户名 | 返回:string ### `login()` 声明:function 登录,暂不支持。但是,在设置了Username和Password属性时,使用connect()方法就包括连接服务器和登陆功能。如需验证用户名密码可使用Authenticate()方法 可见性:public 返回:any ### `quickSend(a_host, a_subject, a_to, a_from, a_text)` 声明:function 发送邮件。暂时不支持 可见性:public | 参数 | 类型 | 说明 | | --- | --- | --- | | `a_host` | string | 字符串类型,服务器主机名或者地址 | | `a_subject` | string | 字符串类型,标题 | | `a_to` | string | 接受者邮件地址 | | `a_from` | string | 来源邮件地址 | | `a_text` | string | 发送的邮件的内容 | 返回:any #### 示例 ```tsl obj := CreateObject("smtp"); obj.UserName := 'username'; //邮箱账号 obj.Password := 'password'; //邮箱密码 obj.Host := 'smtp.tinysoft.com.cn'; //smtp地址 obj.port := 25; //端口 obj.connect(); try mailHost:='smtp.tinysoft.com.cn'; //邮件服务器地址 mailSubject:="SendMailTest"; //邮件标题 mailBody := "test"; //邮件内容 mailSender := "username@tinysoft.com.cn"; //邮件发送人邮箱 mailRecipients := " Recipients@qq.com"; //邮件接收人邮箱 obj.QuickSend(mailHost,mailSubject,mailRecipients,mailSender,mailBody); except echo '\r\n邮件错误信息',obj.LastCmdResult(); return echo '\r\n邮件发送失败:',ExceptObject.errinfo; end; return 1; ``` ### `send(message)` 声明:function 发送邮件 可见性:public | 参数 | 类型 | 说明 | | --- | --- | --- | | `message` | mailmsg | MailMsg对象,需要发送的邮件内容 | 返回:any #### 示例 ```tsl obj:= CreateObject("smtp") ; obj.UserName := 'username' ; //邮箱账号 obj.Password := 'password'; //邮箱密码 obj.Host := 'smtp.tinysoft.com.cn' ; //smtp地址 obj.port:=25; //端口 obj.connect(); //设置mailmsg对象 msg := CreateObject("MailMsg"); msg.subject := "SendMailTest"; //邮件标题 msg.body := "test"; //邮件内容 msg.Sender := "username@tinysoft.com.cn"; //邮件发送人邮箱 msg.Recipients :=" Recipients @qq.com";//邮件接收人邮箱 try obj.send(msg); except echo '\r\n邮件错误信息',obj.LastCmdResult(); return echo '\r\n邮件发送失败:',ExceptObject.errinfo; end; return 1; ``` ### `sendCmd(cmd, cmd_result, ret_code)` 声明:function 发送命令 可见性:public | 参数 | 类型 | 说明 | | --- | --- | --- | | `cmd` | string | 字符串类型,发送的命令 | | `cmd_result` | string | 可选。返回的结果串 | | `ret_code` | string | 可选。返回的代码串 | 返回:boolean #### 示例 ```tsl //发送HELO命令。 obj:= CreateObject("smtp") ; obj.Host := 'smtp.tinysoft.com.cn' ; obj.connect(); obj.sendcmd("HELO smtp.tinysoft.com.cn\r\n",r,c); return r; //结果:665518 Hello smtp.tinysoft.com.cn [ip], pleased to meet you. ``` ### `verify(username)` 声明:function 验证用户名。验证用户名。用户名是否存在当前服务器中,注:smtp服务器可禁止此功能 可见性:public | 参数 | 类型 | 说明 | | --- | --- | --- | | `username` | string | 字符串类型,验证的用户名 | 返回:string #### 示例 ```tsl obj:= CreateObject("smtp","test") ; obj.Host := 'smtp.tinysoft.com.cn' ; obj.port:=25; try username:="username@tinysoft.com.cn"; return obj.verify(username); except return echo ExceptObject.errinfo; end; //结果:username < username@tinysoft.com.cn> ``` ### `Authenticate` 声明:property 服务器验证。验证用户名和密码是否能登陆smtp服务器。使用之前需要先使用connect()方法 可见性:public 类型:boolean 访问:read/write ### `AuthType` 声明:property 认证类型,0:明文用户密码,1:MD5,2:SASL认证,-1:自定义认证 可见性:public 类型:integer 访问:read/write ### `AuthTypeString` 声明:property 自定义认证类型,仅当 AuthType 设置为-1时有效;设置为"AUTH=*":自动选择服务器支持的认证类型;多个用分号连接 可见性:public 类型:string 访问:read/write ### `DidAuthenticate` 声明:property 是否已经验证。需要先执行Authenticate()方法验证 可见性:public 类型:boolean 访问:read ### `Host` 声明:property 主机名称或地址 可见性:public 类型:string 访问:read/write ### `LastCmdResult` 声明:property 最后的命令执行结果 可见性:public 类型:string 访问:read ### `LastCmdResultCode` 声明:property 最后的命令执行的返回码 可见性:public 类型:string 访问:read ### `Password` 声明:property 密码 可见性:public 类型:string 访问:read/write ### `Port` 声明:property 主机端口 可见性:public 类型:integer 访问:read/write ### `Username` 声明:property 用户名 可见性:public 类型:string 访问:read/write ### `UseSTARTTLS` 声明:property 是否使用显式 TLS(即 STARTTLS 加密协议) 可见性:public 类型:boolean 访问:read/write ### `UseTLS` 声明:property 是否采用SSL连接。采用ssl需要设置到服务器的对应端口 可见性:public 类型:boolean 访问:read/write