您的位置:首页 > 编程语言 > VB

VB-使用CDO连接Exchange Server发邮件

2013-04-25 17:08 85 查看
实现CDO控件通过exchange server发送邮件

邮箱环境 企业邮箱 Exchange Server(office 365)

1. 首先加载 Microsoft CDO for Windows 2000 library.

2. 创建CDO实例

Set cdoObj = CreateObject("cdo.message")

Set iConf = CreateObject("CDO.Configuration")

3. 选择使用SMTP

iFields.Item(nameSpace + "sendusing") = 2 ' SMTP

4.设置服务器IP地址

本例子使用Exchange Server作为地址

iFields.Item(nameSpace + "smtpserver") = smtpServer

5.如果服务器端设置允许不需要认证发送邮件

iFields.Item(nameSpace & "smtpauthenticate") = 0

如果需要认证

iFields.Item(nameSpace & "smtpauthenticate") = 1

并且输入

iFields.Item(nameSpace & "sendusername") = sendUserName

iFields.Item(nameSpace & "sendpassword") = sendPassword

本项目用的是域用户,在局域网内测试不需要认证

但如果用邮箱认证也是可以的

6.其余没什么特殊的了

7.可以用SMTP邮箱测试

可以用自己的163,或者qq邮箱进行测试

代码

Public Sub SendExchangeServerEmail()

On Error GoTo errHandler

Dim cdoObj As CDO.Message

Dim iConf As CDO.Configuration

Dim str As String

Dim nameSpace As String

nameSpace = "http://schemas.microsoft.com/cdo/configuration/"

'add reference ' Microsoft CDO Exchange 2000 Library

Dim newLine As String

newLine = Chr(13) + Chr(10)

'Code

Set cdoObj = CreateObject("cdo.message")

Set iConf = CreateObject("CDO.Configuration")

Set iFields = iConf.Fields

iFields.Item(nameSpace + "sendusing") = 2 ' SMTP

iFields.Item(nameSpace + "smtpserver") = smtpServer

If smtpServerPort <> "" Then iFields.Item(nameSpace & "smtpserverport") = smtpServerPort

If usePassword = True Then

iFields.Item(nameSpace & "smtpauthenticate") = 1

iFields.Item(nameSpace & "sendusername") = sendUserName

iFields.Item(nameSpace & "sendpassword") = sendPassword

Else

iFields.Item(nameSpace & "smtpauthenticate") = 0

'iFields.Item(nameSpace & "sendusername") = sendUserName

'iFields.Item(nameSpace & "sendpassword") = sendPassword

End If

iFields.Update

Set cdoObj.Configuration = iConf

str = vbNullString

cdoObj.Subject = emailSubject

str = str + " <html> " + emailContent

str = str + " </html>"

cdoObj.HTMLBody = str

cdoObj.To = sendTo

cdoObj.CC = ""

cdoObj.From = sendFrom ' type your mail id

cdoObj.Send

MsgBox "Send a email to " + sendTo

Exit_Handler:

Exit Sub

errHandler:

MsgBox Err.Description

Resume Exit_Handler

Endsub:

End Sub
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: