通过VB6发送电子邮件

时间:2011-04-01 18:18:10

标签: email vb6 email-integration

我想知道是否有办法通过VB6发送电子邮件(SMTP)。我有一个应用程序,只需要在用户完成后发送一个简单的电子邮件,让组知道应用程序已处理。有没有办法做到这一点?

5 个答案:

答案 0 :(得分:10)

是的 - 取决于您使用的是哪个版本的Windows。假设其中一个版本 - CDO.Message效果很好。

Sub SendMessage(MailFrom,MailTo,Subject,Message)
    Dim ObjSendMail
    Set ObjSendMail = CreateObject("CDO.Message")

    'This section provides the configuration information for the remote SMTP server.

    With ObjSendMail.Configuration.Fields
    .Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'Send the message using the network (SMTP over the network).
    .Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smpt server Address"
    .Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
    .Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False 'Use SSL for the connection (True or False)
    .Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60

    ' If your server requires outgoing authentication uncomment the lines below and use a valid email address and password.
'    .Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 'basic (clear-text) authentication
'    .Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = MailFrom
'    .Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = yourpassword

    .Update
    End With

    'End remote SMTP server configuration section==

    ObjSendMail.To = MailTo
    ObjSendMail.Subject = Subject
    ObjSendMail.From = MailFrom

    ' we are sending a html email.. simply switch the comments around to send a text email instead
    ObjSendMail.HTMLBody = Message
    'ObjSendMail.TextBody = Message

    ObjSendMail.Send

    Set ObjSendMail = Nothing
End Sub

答案 1 :(得分:3)

您应该希望在您的计算机上安装CDOSYS库:

  

CDO Messaging - MSDN
  Creating and Sending a Message - MSDN
  Sending email using CDOSYS ( THE REAL DEAL )
  ASP Sending e-mail with CDOSYS

如果您没有该库(并且无法安装它),那么总会有CDONTS重新开启,但它已被弃用:

  

Using the CDONTS component to send email from ASP pages.

答案 2 :(得分:3)

我发现了here

Dim UserName$, UserMail$, MailRecipiant$, MailBody$, SockData$

Private Sub Command1_Click()
UserName = "YourUserName_or_Addr"
UserMail = "Your Name <You@provider.com>"
MailRecipiant = UserMail
MailBody = "The message goes here"
Winsock1.LocalPort = 0
Winsock1.RemoteHost = "smtp-server"
Winsock1.RemotePort = 25
Winsock1.Connect
End Sub

Private Sub Winsock1_Connect()
Label1 = "Sending message..."
Winsock1.SendData "EHLO " & UserName & vbCrLf
If Not WaitFor("250") Then GoTo 100
Winsock1.SendData "MAIL FROM: " & UserMail & vbCrLf
If Not WaitFor("250") Then GoTo 100
Winsock1.SendData "RCPT TO: " & MailRecipiant & vbCrLf
If Not WaitFor("250") Then GoTo 100
Winsock1.SendData "DATA" & vbCrLf
If Not WaitFor("354") Then GoTo 100
Winsock1.SendData MailBody & vbCrLf & "." & vbCrLf
If Not WaitFor("250") Then GoTo 100
Winsock1.SendData "QUIT" & vbCrLf
If Not WaitFor("221") Then GoTo 100
Label1 = "Message sent"
GoTo 200
100
Label1 = SockData
200
Winsock1.Close
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Winsock1.GetData SockData
End Sub

Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
Label1 = "Error: " & Description
SockData = "Error"
Winsock1.Close
End Sub

Private Function WaitFor(SockResponse As String) As Boolean
Do While Left(SockData, 3) <> SockResponse And Left(SockData, 3) <> "220" And Left(SockData, 3) <> "250"
  DoEvents
  If Left(SockData, 3) > "400" Then Exit Function
Loop
WaitFor = 1
SockData = ""
End Function

答案 3 :(得分:0)

如果您真的需要从客户端的PC发送电子邮件,那么Dave有一个很好的解决方案。但是,有时您会遇到防火墙等问题。在您连接到SQL Server的情况下,如果您通过SQL Server代理邮件(通过将其排入传出邮件表或调用存储的xp_sendmail,我发现它更简单,更易于管理proc自己)。

这是关于如何在服务器上安装和设置SQL Mail的tutorial,最后介绍了如何使用存储过程发送电子邮件。

我发现这个解决方案是有利的,因为:

  • Windows 7计算机阻止所有出站SMTP
  • 实施所有重试,例如,向外发送电子邮件“正确”非常复杂
  • 使用SQL Server的队列方法,但实际上在我的开发或测试数据库上没有设置SQL Mail,除非我在生产服务器上运行,否则电子邮件会留在队列中

答案 4 :(得分:0)

我们之前使用过OstroSoft SMTP控制效果非常好。