Click here to Skip to main content
16,006,006 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRise! (those controls) - vb.net 2003 Pin
omega2325-Jul-03 6:20
omega2325-Jul-03 6:20 
GeneralRe: Rise! (those controls) - vb.net 2003 Pin
J. Dunlap25-Jul-03 8:37
J. Dunlap25-Jul-03 8:37 
GeneralVB6 Question and problem. Pin
igor196025-Jul-03 6:18
igor196025-Jul-03 6:18 
GeneralOutlook Bar Pin
Emile Jacobs25-Jul-03 1:48
Emile Jacobs25-Jul-03 1:48 
GeneralRe: Outlook Bar Pin
J. Dunlap25-Jul-03 8:42
J. Dunlap25-Jul-03 8:42 
GeneralRe: Outlook Bar Pin
Grégory1-Aug-03 3:11
Grégory1-Aug-03 3:11 
GeneralRe: Outlook Bar Pin
Grégory1-Aug-03 3:11
Grégory1-Aug-03 3:11 
Generalvb and vba Pin
Cristian_Dior24-Jul-03 22:38
Cristian_Dior24-Jul-03 22:38 
does someone know if its possible to send email in vba using smtp and winsock?
i got a code which works in vb.Using the code in vba seems to be possible
and also the refereces are in vba available but i always get the same message
"objectvariable or with-blockvariable not set"
what does it mean?
here is my complete code,
hope someone can help


Option Explicit
' ----------------------------------------------------------------------------
'
' EVENTS
'
' ----------------------------------------------------------------------------

Public Event Connected(ByVal Host As String, ByVal Port As Long)
Public Event ReceivedData(ByVal Data As String)
Public Event SentData(ByVal Data As String)
Public Event MailCompleted()
Public Event Error(ByVal Error As String)

' ------------------------------------------------------------------------------
'
' PROPERTY VARIABLES
'
' ------------------------------------------------------------------------------
Dim m_Server As String ' mail server host
Dim m_Port As String ' mail server port
Dim m_MailFrom As String ' from address
Dim m_MailTo As String ' to address
Dim m_BCC As String ' blind carbon copy addresses
Dim m_CCC As String ' carbon copy addresses
Dim m_Subject As String ' email subject
Dim m_NameFrom As String ' from name
Dim m_NameTo As String ' to name
Dim m_Body As String ' email body
Dim m_Log As String ' log of transaction
Dim Sock As Winsock

' private state variables
Dim LastResponse As String

' ------------------------------------------------------------------------------
'
' PUBLIC PROPERTIES
'
' ------------------------------------------------------------------------------
Public Property Get Server() As String
Server = m_Server
End Property

Public Property Let Server(ByVal Data As String)
m_Server = Data
End Property

Public Property Get Port() As String
Port = m_Port
End Property

Public Property Let Port(ByVal Data As String)
m_Port = Data
End Property

Public Property Get MailFrom() As String
MailFrom = m_MailFrom
End Property

Public Property Let MailFrom(ByVal Data As String)
m_MailFrom = Data
End Property

Public Property Get MailTo() As String
MailTo = m_MailTo
End Property

Public Property Let MailTo(ByVal Data As String)
m_MailTo = Data
End Property

Public Property Get BCC() As String
BCC = m_BCC
End Property

Public Property Let BCC(ByVal Data As String)
m_BCC = Data
End Property

Public Property Get CCC() As String
CCC = m_CCC
End Property

Public Property Let CCC(ByVal Data As String)
m_CCC = Data
End Property

Public Property Get Subject() As String
Subject = m_Subject
End Property

Public Property Let Subject(ByVal Data As String)
m_Subject = Data
End Property

Public Property Get NameTo() As String
NameTo = m_NameTo
End Property

Public Property Let NameTo(ByVal Data As String)
m_NameTo = Data
End Property

Public Property Get NameFrom() As String
NameFrom = m_NameFrom
End Property

Public Property Let NameFrom(ByVal Data As String)
m_NameFrom = Data
End Property

Public Property Get Body() As String
Body = m_Body
End Property

Public Property Let Body(ByVal Data As String)
m_Body = Data
End Property

Public Property Get Log() As String
Log = m_Log
End Property

Public Property Let Log(ByVal Data As String)
m_Log = Data
End Property

' ------------------------------------------------------------------------------
'
' PUBLIC SUBS
'
' ------------------------------------------------------------------------------
Public Function SendMail() As Boolean
Dim SMTPCommands(0 To 10) As String
Dim SMTPResponses(0 To 10) As String
Dim Success As Boolean
Dim i As Integer

' construct an array of commands
SMTPCommands(0) = "HELO " & Me.Server
SMTPCommands(1) = "MAIL FROM:" & Me.MailFrom
SMTPCommands(2) = "RCPT TO:" & Me.MailTo
SMTPCommands(3) = "DATA"
SMTPCommands(4) = "BCC:" & Me.BCC
SMTPCommands(5) = "CCC:" & Me.CCC
SMTPCommands(6) = "SUBJECT:" & Me.Subject
SMTPCommands(7) = "TO:" & Me.NameTo
SMTPCommands(8) = "FROM:" & Me.NameFrom & vbCrLf ' extra vbCrLf
SMTPCommands(9) = Me.Body & vbCrLf & "."
SMTPCommands(10) = "QUIT"

SMTPResponses(0) = "250"
SMTPResponses(1) = "250"
SMTPResponses(2) = "250"
SMTPResponses(3) = "354"
SMTPResponses(4) = ""
SMTPResponses(5) = ""
SMTPResponses(6) = ""
SMTPResponses(7) = ""
SMTPResponses(8) = ""
SMTPResponses(9) = "250"
SMTPResponses(10) = "221"

' connect to the server
If ConnectToServer = False Then
RaiseEvent Error("Couldn't connect to server")
Exit Function
Else
' wait for the welcome message to be received
WaitForResponse "220"
End If

' send each command, waiting for a response
For i = 0 To 10

' send the command
SMTPSend SMTPCommands(i)

' wait for the response
Success = WaitForResponse(SMTPResponses(i))

' check if we were successful
If Success = False Then
RaiseEvent Error("Failed at server side. Check the SMTP.Log property for more details")
Exit Function
End If

Next i

' finished
RaiseEvent MailCompleted
End Function

Private Function ConnectToServer() As Boolean

' connect to the host
Sock.RemoteHost = Me.Server
Sock.RemotePort = Me.Port
Sock.Connect

' wait for connection
Do While Sock.State <> sckConnected
DoEvents
If Sock.State = sckError Then
Exit Function
End If
Loop

' return true
ConnectToServer = True
End Function

Private Function WaitForResponse(ByVal Response As String) As Boolean

' if we're not waiting for a response then exit
If Response = "" Then
WaitForResponse = True
Exit Function
Else

' wait for the response property to change
Do While LastResponse = ""
DoEvents
Loop

' if it matches, return true, else return false
If Response = LastResponse Then
' return true
WaitForResponse = True

' check for errors
Else
WaitForResponse = False
End If

End If

' clear the static variable for next time
LastResponse = ""
End Function

Private Sub SMTPSend(ByVal Data As String)

' send the passed string with a vbCrLf
If Sock.State = sckConnected Then
Sock.SendData Data & vbCrLf
DoEvents
End If

' raise event
RaiseEvent SentData(Data)

' log
AppendLog Data & vbCrLf
End Sub

Public Sub AppendLog(ByVal Data As String)
' append the passed data to the log
Me.Log = Me.Log & Data
End Sub

' ------------------------------------------------------------------------------
'
' INTERNAL SUBS
'
' ------------------------------------------------------------------------------
Private Sub Sock_Connect()

' raise event
RaiseEvent Connected(Sock.RemoteHost, Sock.RemotePort)
End Sub

Private Sub Sock_DataArrival(ByVal bytesTotal As Long)
Dim Data As String

' grab the data and set the last response
Sock.GetData Data
LastResponse = Mid$(Data, 1, 3)

' raise event
RaiseEvent ReceivedData(Data)

' log
AppendLog Data
End Sub

Private Sub Sock_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)

' raise event
RaiseEvent Error(Description)
End Sub

Private Sub UserControl_Resize()

' fit the control to the image
With UserControl
.Height = picImage.Height
.Width = picImage.Width
End With
End Sub

Private Sub UserForm_Click()

' display the form
'Me.Show

' setup the mail control
With SMTP
.Server = "mail.hotmail.com"
.Port = 25
.MailFrom = "testing@winsockvb.com"
.MailTo = "youremailhere@hotmail.com"
.NameFrom = "Me"
.NameTo = "You"
.Subject = "Test mail from WinsockVB.com control"
.Body = "Hello"

' send the mail
.SendMail
End With
End Sub


Thanx
GeneralRe: vb and vba Pin
pxw16-Aug-03 7:09
pxw16-Aug-03 7:09 
GeneralData Structure Pin
iti24-Jul-03 16:35
iti24-Jul-03 16:35 
Generalvb6.0 Pin
cypresspalace24-Jul-03 13:54
cypresspalace24-Jul-03 13:54 
GeneralRe: vb6.0 Pin
Hesham Amin24-Jul-03 22:37
Hesham Amin24-Jul-03 22:37 
GeneralRTF convert to postscript Pin
jjpaquin24-Jul-03 9:28
jjpaquin24-Jul-03 9:28 
General(407) Proxy Authentication Required Pin
Bernhard Hofmann24-Jul-03 5:08
Bernhard Hofmann24-Jul-03 5:08 
GeneralPrinting Graphics Pin
S Guruprasad24-Jul-03 2:25
S Guruprasad24-Jul-03 2:25 
Generalvb resource file Pin
r i s h a b h s23-Jul-03 21:42
r i s h a b h s23-Jul-03 21:42 
Generalicon for vb non gui application Pin
r i s h a b h s23-Jul-03 23:04
r i s h a b h s23-Jul-03 23:04 
GeneralFinding .BMP's and .JPEG's after deployment Pin
Jm6k23-Jul-03 6:47
Jm6k23-Jul-03 6:47 
GeneralRe: Finding .BMP's and .JPEG's after deployment Pin
apferreira23-Jul-03 6:57
apferreira23-Jul-03 6:57 
GeneralRe: Finding .BMP's and .JPEG's after deployment Pin
Jm6k24-Jul-03 12:14
Jm6k24-Jul-03 12:14 
GeneralRe: Finding .BMP's and .JPEG's after deployment Pin
Jm6k24-Jul-03 12:14
Jm6k24-Jul-03 12:14 
GeneralRe: Finding .BMP's and .JPEG's after deployment Pin
apferreira25-Jul-03 1:17
apferreira25-Jul-03 1:17 
GeneralThanks Pin
Jm6k25-Jul-03 19:22
Jm6k25-Jul-03 19:22 
Generalrunning applications in VB 6.0 Pin
adnanmadi23-Jul-03 4:36
adnanmadi23-Jul-03 4:36 
GeneralRe: running applications in VB 6.0 Pin
Hesham Amin23-Jul-03 7:24
Hesham Amin23-Jul-03 7:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.