在VB6中写的一个发送简单邮件的类

在VB6中写的一个发送简单邮件的类


Option Explicit
Private cdoMessage As CDO.Message
Private Const cdoSendUsingMethod = "http://schemas.microsoft.com/cdo/configuration/sendusing"
Private Const cdoSMTPServer = "http://schemas.microsoft.com/cdo/configuration/smtpserver"
Private Const cdoSMTPServerPort = "http://schemas.microsoft.com/cdo/configuration/smtpserverport"
Private Const cdoSMTPConnectionTimeout = "http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"
Private Const cdoSMTPAuthenticate = "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"
Private Const cdoSendUserName = "http://schemas.microsoft.com/cdo/configuration/sendusername"
Private Const cdoSendPassword = "http://schemas.microsoft.com/cdo/configuration/sendpassword"
Private Const SMTPConnectionTimeout = 60
Private E_SendUsingMethod As Byte       '邮件发送选项
Private E_SendSMTPAuthenticate As Byte  'SMTP验证选项
Private E_SMTPServer As String          'SMTP服务器
Private E_SMTPServerPort As Integer     'SMTP服务器端口
Private E_SendUserName As String        '用户名
Private E_SendPassword As String        '密码

Private E_EmailTo As String
Private E_EmailFrom As String
Private E_EmailSubject As String
Private E_EmailTextBody As String
Public Property Get SendUsingPort() As Byte
SendUsingPort = E_SendUsingMethod
End Property
Public Property Let SendUsingPort(SUPort As Byte)
E_SendUsingMethod = SUPort
End Property
Public Property Get SMTPAuthenticate() As Byte
SMTPAuthenticate = E_SendSMTPAuthenticate
End Property
Public Property Let SMTPAuthenticate(SMTPType As Byte)
E_SendSMTPAuthenticate = SMTPType
End Property
Public Property Get SMTPServer() As String
SMTPServer = E_SMTPServer
End Property
Public Property Let SMTPServer(sServerName As String)
E_SMTPServer = sServerName
End Property
Public Property Get SMTPServerPort() As Integer
SMTPServerPort = E_SMTPServerPort
End Property
Public Property Let SMTPServerPort(ServerPort As Integer)
E_SMTPServerPort = ServerPort
End Property
Public Property Get SendUserName() As String
SendUserName = E_SendUserName
End Property
Public Property Let SendUserName(ServerLoginUser As String)
E_SendUserName = ServerLoginUser
End Property
Public Property Get SendPassword() As String
SendPassword = E_SendPassword
End Property
Public Property Let SendPassword(Pwd As String)
E_SendPassword = Pwd
End Property
Public Property Get EmailTo() As String
EmailTo = E_EmailTo
End Property
Public Property Let EmailTo(strEmail As String)
E_EmailTo = strEmail
End Property
Public Property Get EmailFrom() As String
EmailFrom = E_EmailFrom
End Property
Public Property Let EmailFrom(strEmail As String)
E_EmailFrom = strEmail
End Property
Public Property Get EmailSubject() As String
EmailSubject = E_EmailSubject
End Property
Public Property Let EmailSubject(strSubject As String)
E_EmailSubject = strSubject
End Property
Public Property Get EmailTextBody() As String
EmailTextBody = E_EmailTextBody
End Property
Public Property Let EmailTextBody(strTextBody As String)
E_EmailTextBody = strTextBody
End Property
'Error sub
Private Sub ErrorSub()
MsgBox "Error " & Err.Number & " " & Err.Description, vbInformation + vbOKOnly, "Error Information"
End Sub
'Send Email
Public Function SendEmail() As Boolean
On Error GoTo Err_SendEmail
'Configuration
    With cdoMessage.Configuration.Fields
.Item(cdoSendUsingMethod) = E_SendUsingMethod
.Item(cdoSMTPServer) = E_SMTPServer
.Item(cdoSMTPServerPort) = E_SMTPServerPort
.Item(cdoSMTPConnectionTimeout) = SMTPConnectionTimeout
.Item(cdoSMTPAuthenticate) = E_SendSMTPAuthenticate
.Item(cdoSendUserName) = E_SendUserName
.Item(cdoSendPassword) = E_SendPassword
.Update
End With
'Message
    With cdoMessage
.To = E_EmailTo
.From = E_EmailFrom
.Subject = E_EmailSubject
.TextBody = E_EmailTextBody
.Send
End With
SendEmail = True
Exit Function
Err_SendEmail:
ErrorSub
End Function
'Verify Data
Private Function VerifyData() As Boolean
Dim StrMsg As String
If E_SMTPServer = "" Then
StrMsg = "SMTP服务器名没有填写|"
        GoTo ErrorInput
End If
If E_SMTPServerPort <= 0 Then
StrMsg = "SMTP 端口没有填写|"
        GoTo ErrorInput
End If
If E_SendUserName = "" Then
StrMsg = "用户名没有填写|"
        GoTo ErrorInput
End If
If E_SendPassword = "" Then
StrMsg = "密码没有填写|"
        GoTo ErrorInput
End If
VerifyData = True
Exit Function
ErrorInput:
MsgBox GetLanguageStr(StrMsg), vbInformation + vbOKOnly, GetLanguageStr("信息提示|")
End Function
'Save messages of configuration to database
Public Function SaveConfigInfo(Optional ByVal intUpdateTyp As Integer = 1) As Boolean
Dim objDBB As Object
Dim strSQL As String
On Error GoTo Err_SaveConfigInfo
If Not VerifyData Then Exit Function
'代码略
    SaveConfigInfo = True
Exit Function
Err_SaveConfigInfo:
ErrorSub
End Function
'Read messages of configuration from database
Public Sub ReadConfigInfo()
Dim objDBB As Object
Dim objRst As ADODB.Recordset
On Error GoTo Err_ReadConfigInfo
'其中的代码略
    If Not objRst.EOF Then
E_SendUsingMethod = objRst!SendUsingMethod
E_SMTPServer = objRst!SMTPServer
E_SMTPServerPort = objRst!ServerPort
E_SendSMTPAuthenticate = objRst!Authenticate
E_SendUserName = objRst!SendUserName
E_SendPassword = objRst!SendPassword
E_EmailTo = objRst!EmailTo
End If
If objRst.State = adStateOpen Then objRst.Close
Set objRst = Nothing
Set objDBB = Nothing
Exit Sub
Err_ReadConfigInfo:
ErrorSub
End Sub
Private Sub Class_Initialize()
E_SendUsingMethod = 2
E_SendSMTPAuthenticate = 1
E_SMTPServerPort = 25
Set cdoMessage = New CDO.Message
End Sub

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 在VB.NET中,我们可以使用System.Net.Mail命名空间中的来收取邮件。具体步骤如下: 1. 引入命名空间: `Imports System.Net.Mail` 2. 创建一个SmtpClient对象,并配置SMTP服务器信息: ``` Dim client As New SmtpClient() client.Host = "邮件服务器地址" client.Port = 587 '邮件服务器端口号 client.EnableSsl = True '是否使用SSL加密连接 client.Credentials = New System.Net.NetworkCredential("用户名", "密码") '登录邮箱的用户名和密码 ``` 3. 创建一个MailMessage对象,并设置发件人、收件人、主题和正文: ``` Dim message As New MailMessage() message.From = New MailAddress("发件人邮箱地址") message.To.Add("收件人邮箱地址") message.Subject = "邮件主题" message.Body = "邮件正文" ``` 4. 发送邮件: ``` client.Send(message) ``` 通过以上步骤,我们就可以使用VB.NET来发送邮件了。 值得注意的是,在使用SmtpClient发送邮件之前,需要确保SMTP服务器的相关设置已经正确配置,包括邮件服务器地址、端口号、SSL加密等,同时还需要提供登录邮箱的用户名和密码用于身份验证。另外,具体的邮件内容设置和处理还可以根据实际需求进行进一步的定制和优化。 ### 回答2: 在VB.NET中,我们可以使用System.Net.Mail和System.Net命名空间中的来收取邮件。 首先,我们需要创建一个SmtpClient对象来连接到邮件服务器。需要提供邮件服务器的地址和端口号以及用户凭证(例如用户名和密码)。然后,使用SmtpClient的方法连接到服务器。 接下来,我们可以使用Pop3Client来接收邮件。与SmtpClient似,我们需要提供服务器的地址和端口号以及用户凭证。然后,使用Pop3Client的方法连接到服务器。 一旦连接成功,我们可以使用Pop3Client的方法获取邮件的总数。然后,我们可以使用Pop3Client的方法按照索引号获取每封邮件。将每封邮件保存为MailMessage对象,我们可以使用MailMessage对象的属性来获取邮件的信息,例如发件人、收件人、主题和正文。 最后,我们可以使用Pop3Client的方法删除已读取的邮件或将其标记为已读。 需要注意的是,邮件服务器可能会有不同的配置要求。因此,具体的实现细节可能会因使用的邮件服务器而有所不同。在实际应用中,我们还需要处理错误、异常以及安全性等问题。 综上所述,在VB.NET中收取邮件的过程可以简单总结为:连接到邮件服务器、获取邮件的总数、按照索引号获取每封邮件并获取其信息、删除或标记已读邮件。 ### 回答3: 在VB.NET中,我们可以使用System.Net命名空间中的POP3或IMAP来收取邮件。 对于POP3协议,我们可以使用System.Net.POP3命名空间中的POP3来实现收取邮件的功能。首先,我们需要建立一个POP3Client对象,并指定邮件服务器的主机名和端口号。然后,使用POP3Client对象的User方法和Pass方法来指定邮件服务器的用户名和密码。接下来,我们可以使用POP3Client对象的GetEmailCount方法获取邮件的数量,使用GetEmailSize方法获取特定邮件的大小,使用GetEmailHeader方法获取特定邮件的头部信息,使用GetEmailBody方法获取特定邮件的内容。最后,可以使用POP3Client对象的DeleteEmail方法来删除特定的邮件。 对于IMAP协议,我们可以使用System.Net.IMAP命名空间中的IMAPClient来实现收取邮件的功能。与POP3似,我们首先需要建立一个IMAPClient对象,并指定邮件服务器的主机名和端口号。然后,使用IMAPClient对象的User方法和Pass方法来指定邮件服务器的用户名和密码。接下来,我们可以使用IMAPClient对象的GetMailbox方法获取邮箱的信息,使用GetMessageCount方法获取邮件的数量,使用GetHeader方法获取特定邮件的头部信息,使用GetBodyText方法获取特定邮件的文本内容,使用GetBodyHTML方法获取特定邮件的HTML内容。最后,可以使用IMAPClient对象的DeleteMessage方法来删除特定的邮件。 无论使用POP3还是IMAP协议,我们都需要处理一些异常情况,例如无法连接到邮件服务器、用户名或密码错误等。因此,在实现收取邮件的功能时,还需要使用try-catch语句来捕获可能出现的异常,并进行相应的处理。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值