通过VB6发送电子邮件

通过VB6发送电子邮件,vb6,email,Vb6,Email,我想知道是否有办法通过VB6发送电子邮件(SMTP)。我有一个应用程序,只需要在用户完成后发送一封简单的电子邮件,让一个组知道应用程序已经处理。有没有办法做到这一点?您最好在您的计算机上安装CDOSYS库: 如果您没有该库(并且无法安装该库),则总有CDont可供依赖,但不推荐使用: 我发现: Dim UserName$、UserMail$、MailRecipiant$、MailBody$、SockData$ 专用子命令1_Click() UserName=“YourUserName\

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

您最好在您的计算机上安装CDOSYS库:




如果您没有该库(并且无法安装该库),则总有CDont可供依赖,但不推荐使用:

我发现:

Dim UserName$、UserMail$、MailRecipiant$、MailBody$、SockData$
专用子命令1_Click()
UserName=“YourUserName\u或\u Addr”
UserMail=“您的姓名”
MailRecipiant=UserMail
MailBody=“消息在这里传递”
Winsock1.LocalPort=0
Winsock1.RemoteHost=“smtp服务器”
Winsock1.RemotePort=25
Winsock1.Connect
端接头
专用子Winsock1_Connect()
Label1=“正在发送消息…”
Winsock1.SendData“EHLO”&用户名和vbCrLf
如果没有等待(“250”),则转到100
Winsock1.SendData“邮件发件人:”&UserMail&vbCrLf
如果没有等待(“250”),则转到100
Winsock1.SendData“RCPT TO:”&MailRecipiant&vbCrLf
如果没有等待(“250”),则转到100
Winsock1.SendData“数据”和vbCrLf
如果没有等待(“354”),则转到100
Winsock1.SendData邮件体和vbCrLf&“&”和vbCrLf
如果没有等待(“250”),则转到100
Winsock1.SendData“退出”&vbCrLf
如果没有等待(“221”),则转到100
Label1=“已发送消息”
转到200
100
Label1=SockData
200
Winsock1。关闭
端接头
私有子Winsock1_数据到达(ByVal ByTestTotal长度)
Winsock1.GetData SockData
端接头
私有子Winsock1_错误(ByVal编号为整数,描述为字符串,ByVal Scode为长,ByVal源为字符串,ByVal HelpFile为字符串,ByVal HelpContext为长,CancelDisplay为布尔)
Label1=“错误:”&说明
SockData=“错误”
Winsock1。关闭
端接头
私有函数WaitFor(SockResponse作为字符串)作为布尔值
左(SockData,3)sockdesponse和左(SockData,3)“220”和左(SockData,3)“250”时执行
多芬特
如果左(SockData,3)>“400”,则退出该功能
环
等待=1
SockData=“”
端函数

是-取决于您使用的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

如果你真的需要从客户端的PC发送电子邮件,Dave有一个很好的解决方案。但是,有时你会遇到防火墙之类的问题。在连接到SQL Server的情况下,我发现,如果通过SQL Server代理邮件(将邮件排入传出邮件表,或调用
xp\u sendmail
存储过程本身),管理邮件会更简单、更容易

下面是一个关于如何设置SQL Mail并在服务器上工作的示例,最后展示了如何使用存储过程发送电子邮件

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

  • Windows 7计算机正在阻止所有出站SMTP
  • 执行所有重试等操作以“正确”发送出站电子邮件相当复杂
  • 在SQL Server中使用队列方法,但不是在我的开发或测试数据库上实际设置SQL Mail,除非我运行的是生产服务器,否则电子邮件将保留在队列中

通过设置对库的引用,您还可以获得这些字段名和幻数的命名常量。可能是因为如果您需要加密身份验证或SSL/TLS传输,这种方法会失败?这些都是相当常见的需求了。附件和HTML邮件也为这种“滚动你自己”的方法增加了更多的麻烦。但在有限的情况下,这很好。“当用户完成任务时,发送简单的电子邮件,让小组知道应用程序已处理”另请参阅
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