I am wondering if there is a way to send an email (SMTP) through VB6. I have an application that just needs to send a simple email when the user is done to let a group know that
I found this 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
You should hopefully have the CDOSYS libraries installed on your machine:
CDO Messaging - MSDN
Creating and Sending a Message - MSDN
Sending email using CDOSYS ( THE REAL DEAL )
ASP Sending e-mail with CDOSYS
If you don't have that library (and aren't able to install it) then there's always CDONTS to fall back on but it is deprecated:
Using the CDONTS component to send email from ASP pages.
Yep - depends on which version of windows you're using. Assuming one of the later versions - CDO.Message works great.
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
Dave has a good solution if you really need to send email from the client's PC. However, sometimes you get into trouble with firewalls and such. In the case where you are connecting to a SQL Server, I have found it is simpler and easier to manage if you proxy your mail through SQL Server (either by queuing it into an outgoing mail table, or calling the xp_sendmail
stored proc itself).
Here is a tutorial on how to get SQL Mail setup and working on the server, and at the end it shows how to use a stored procedure to send an email.
I found this solution to be advantageous because: