Function SendMail_CDO() ' Reference to Microsoft ActiveX Data Objects 2.5 Library ' Reference to Microsoft CDO for Windows 2000 Library ' 引用 Microsoft ActiveX Data Objects 2.5 Library ' 引用 Microsoft CDO for Windows 2000 Library Dim iConf As New CDO.Configuration Dim Flds As ADODB.Fields Set Flds = iConf.Fields ' Set the configuration for Network Send ' 设定配置信息 Flds(CDO.cdoLanguageCode) = "gb2312" Flds(CDO.cdoTimeZoneIDURN) = CDO.cdoBeijing Flds(CDO.cdoSendUsingMethod) = CDO.cdoSendUsingPort Flds(cdoSMTPServer) = "test.com.cn" Flds(cdoSMTPServerPort) = 25 Flds(cdoSMTPConnectionTimeout) = 30 Flds(cdoSMTPAccountName) = "My Name" Flds(cdoSendUserReplyEmailAddress) = """User"" <example@example.com>" Flds(cdoSendEmailAddress) = """User"" <example@example.com>" Flds(cdoSMTPAuthenticate) = cdoBasic ' IMPORTANT: Storing user names and passwords inside source code ' can lead to security vulnerabilities in your software. Do not ' store user names and passwords in your production code. ' 注意,用户名和密码不要写在程序里,否则会对你的软件安全性构成威胁 Flds(cdoSendUserName) = "name" Flds(cdoSendPassword) = "password" ' 以下为代理服务器设置,如果你要使用类似 IMessage.AddAttachment 和 IMessage.CreateMHTMLBody ,你可能需要设置以下内容 'Flds(cdoURLProxyServer) = "proxyserver:80" 'Flds(cdoURLProxyBypass) = "<local>" 'Flds(cdoURLGetLatestVersion) = True 'Flds(cdoHTTPCookies) = "cookies here" '设置邮件头 With Flds .Item("urn:schemas:httpmail:to") = "example@access911.net" .Item("urn:schemas:httpmail:from") = "another@access911.net" 'BCC 是密件抄送 'CC 是抄送 .Item("urn:schemas:httpmail:cc") = "exampleuser3@access911.net" .Item("urn:schemas:httpmail:sender") = "example@access911.net" '.Item("urn:schemas:mailheader:myhdr") = "multipart/alternative" '.Item("urn:schemas:mailheader:x-hdr") = "less value" End With Flds.Update Dim iMsg As New CDO.Message Set iMsg.Configuration = iConf With iMsg '以下属性根据字面理解都应该没问题的。 .From = "access911@etang.com" .To = "example@example.com" '.Newsgroups = "area.example.newsgroup1" .Subject = "Test Title" .TextBody = "Please switch to HTML view ... " & _ vbCrLf & "请切换到 HTML 视图。。。" .HTMLBody = "<html><body><table ><tr><td><font color=red>5545</font></td>" & _ "<td><font size=5>44545</font></td></tr></table></body></html>" '添加附件,下面多种格式可以选择 '.AddAttachment "http://server.access911.net/logo.gif" '.AddAttachment "file://c:/temp/test.doc" .AddAttachment "C:\temp\readme.html" .BodyPart.Fields("urn:schemas:mailheader:content-type") = _ "multipart/alternative; charset=""gb2312""" ' content-type 可以选择 "text/plain" or "text/html" or "multipart/alternative" .BodyPart.Charset = "gb2312" .Send End With Set iMsg = Nothing Set Flds = Nothing Set iConf = Nothing End Function 用 CDO 发送邮件 注意:CDO(Collaboration Data Objects) 分多个版本,比如 CDO 1.2 ; CDO for Windows 2000 ; CDO for SPS ; CDO for Exchange 2000 Server 在这里我们使用的应该是 Microsoft CDO for Windows 2000 Library CDO 方法适用所有的 OFFICE 组件。 优点:WINDOWS 2000 以及以上内置该类库,无需额外安装 缺点:需要 ADO 2.5 及以上版本支持,只有在 WINDOWS 2000 及以上版本才无需安装 |