2011-04-01 348 views
6

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

+0

另请参阅http://stackoverflow.com/questions/3539242/sending-e-mail-via-smtp-using-vb6 http://stackoverflow.com/questions/5155611/send-emails-through-vb6-如果没有电子邮件客户端 – MarkJ 2011-04-02 08:12:18

回答

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 
+2

通过设置对库的引用,您还可以获得这些字段名称和幻数的命名常量。 – Bob77 2011-04-01 19:45:45

3

我发现这个here

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

Private Sub Command1_Click() 
UserName = "YourUserName_or_Addr" 
UserMail = "Your Name <[email protected]>" 
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 
+0

为什么击落投票? – SQLMason 2011-04-01 18:35:59

+0

我测试了它,它在VB 6.0中工作... – SQLMason 2011-04-01 18:45:24

+0

也许因为如果您需要加密身份验证或SSL/TLS传输,此方法会失败?这些都是相当普遍的要求了。附件和HTML邮件也为这种“自己动手”的方式增加了更多的麻烦。尽管如此,在有限的情况下仍然很棒 – Bob77 2011-04-01 19:48:35

0

戴夫有一个很好的解决方案,如果你真的需要从客户端的PC发送电子邮件。但是,有时您会遇到防火墙等问题。在连接到SQL Server的情况下,如果通过SQL Server代理邮件(通过将邮件排队到发送邮件表中,或者调用xp_sendmail存储过程本身),我发现它更简单,更容易管理。

这是关于如何获取SQL Mail设置和在服务器上工作的tutorial,并且最后它展示了如何使用存储过程发送电子邮件。

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

  • Windows 7的电脑被封锁所有出站SMTP
  • 实现所有的重试次数和这样做的出站电子邮件“右”是相当复杂
  • 使用与SQL Server,但在我的开发或测试数据库实际上没有设置SQL Mail的队列方法,该电子邮件停留在队列中,除非我在生产服务器上运行