Apr 18th, 2005, 08:44 PM
#1
Simple VB emailer
Here is a class you can use to send email from VB. I wrote it years ago, but have seen a few requests for info on emailing.
Here is an example of how to use the class
Edit: Before using the class you'll have to add a refernce to MSWINSCK.OCX
Goto Project\References Browse to system32\MSWINSOCK.OCX
VB Code:
Option Explicit
Private WithEvents SMTP As clsSMTP
Private Sub Command1_Click()
With SMTP
'who is the email from?
.Sender = "YoMama"
'who is it going to?
'your SMTP server
.SMTPhost = "smtp.myserver.com"
'optional subject line
.Subject = "SMTP test"
'make connection to the server
.OpenMail
'did connection succeed?
If Not .Connected Then Exit Sub
'you can send as many lines as you want here
.Send "Bill,"
.Send "This is a test of your wonderful SMTP client!"
'make sure you close or nothing will get sent
.CloseMail
End With
End Sub
Private Sub Form_Load()
Set SMTP = New clsSMTP
End Sub
Private Sub SMTP_Error(Code As Integer, Message As String)
Debug.Print "SMTP Error "; Code, Message
End Sub
Private Sub SMTP_ServerResponse(Reply As Integer, Message As String)
Debug.Print Reply, Message
End Sub
Pretty simple, Now here is the class
VB Code:
Option Explicit
Private mvarConnected As Boolean
Private mvarRecipient As String
Private mvarSender As String
Private mvarSMTPhost As String
Private mvarSubject As String
Private WithEvents Winsock As MSWinsockLib.Winsock
Const OK = 250
Const Connection = 220
Const ReadyforData = 354
Const UserUnknown = 550
Dim Reply As Integer
Dim Message As String
Dim MyName As String
Dim ErrMSg As String
Event Error(Code As Integer, Message As String)
Event ServerResponse(Reply As Integer, Message As String)
Private Function WaitFor(R As Integer) As Boolean
Dim A
A = Timer + 3
While Not Reply = R And A > Timer
DoEvents
Wend
If A < Timer Then
WaitFor = True
Else
WaitFor = False
End If
Reply = 0
End Function
Public Property Let Subject(ByVal vData As String)
mvarSubject = vData
End Property
Public Property Get Subject() As String
Subject = mvarSubject
End Property
Public Property Let SMTPhost(ByVal vData As String)
mvarSMTPhost = vData
End Property
Public Property Get SMTPhost() As String
SMTPhost = mvarSMTPhost
End Property
Public Sub Send(Message As String)
Winsock.SendData Message & vbCrLf
End Sub
Public Property Let Sender(ByVal vData As String)
mvarSender = vData
End Property
Public Property Get Sender() As String
Sender = mvarSender
End Property
Public Property Let Recipient(ByVal vData As String)
mvarRecipient = vData
End Property
Public Property Get Recipient() As String
Recipient = mvarRecipient
End Property
Public Property Get Connected() As Boolean
Connected = mvarConnected
End Property
Public Sub OpenMail()
Reply = 0
'connect to SMTP server
Winsock.Connect mvarSMTPhost, 25
If WaitFor(Connection) Then
GiveError
Exit Sub
End If
'send hello and wait for OK
Winsock.SendData "helo " & mvarSMTPhost & vbCrLf
If WaitFor(OK) Then
GiveError
Winsock.Close
Exit Sub
End If
'send sender's name
MyName = WhoAmI
If InStr(mvarSender, "@") = 0 Then
Winsock.SendData "Mail From:<" & mvarSender & "@" & MyName & ">" & vbCrLf
Else
Winsock.SendData "Mail From:<" & mvarSender & ">" & vbCrLf
End If
If WaitFor(OK) Then
GiveError
Winsock.Close
Exit Sub
End If
'send recipient
Winsock.SendData "RCPT TO:<" & mvarRecipient & ">" & vbCrLf
If WaitFor(OK) Then
GiveError
Winsock.Close
Exit Sub
End If
'make ready for data
Winsock.SendData "Data" & vbCrLf
If WaitFor(ReadyforData) Then
GiveError
Winsock.Close
Exit Sub
End If
'Send Date:
Winsock.SendData "Date: " & Format(Date, " dd mmm yy ") & Time & vbCrLf
'To:
Winsock.SendData "TO:<" & mvarRecipient & ">" & vbCrLf
'From:
Winsock.SendData "From: <" & mvarSender & "@" & MyName & ">" & vbCrLf
'Subject:
If Not mvarSubject = "" Then Winsock.SendData "Subject:" & mvarSubject & vbCrLf
mvarConnected = True
End Sub
Private Function WhoAmI() As String
Dim X As Integer, Y As Integer
X = InStr(Message, "Hello")
Y = InStr(Message, "[")
If X = 0 Or Y = 0 Then
WhoAmI = Winsock.LocalIP
Else
X = X + 6
Y = Y - 1 - X
WhoAmI = Mid$(Message, X, Y)
End If
End Function
Public Sub CloseMail()
Winsock.SendData "." & vbCrLf
If WaitFor(OK) Then
GiveError
Exit Sub
End If
Winsock.SendData "QUIT"
Winsock.Close
End Sub
Private Sub GiveError()
Dim Message As String
Select Case Reply
Case Is = 10061
Message = "Can't Connect to " & mvarSMTPhost
Case Is = 11001
Message = mvarSMTPhost & " is not a valid name or address."
Case Is = 550
Message = "User " & mvarRecipient & " not known to server."
Case Is = 0
Message = "Timeout waiting for server response"
Case Is = 500
Message = "Server does not recognize a command sent."
Case Is = 553
Message = "Invalid recipient name"
Case Is = 1
Message = "You must set Host, Recipient and Sender before connecting"
Case Else
Message = "Mail tool error."
End Select
RaiseEvent Error(Reply, Message)
End Sub
Private Sub Class_Initialize()
Set Winsock = New MSWinsockLib.Winsock
End Sub
Private Sub Winsock_DataArrival(ByVal bytesTotal As Long)
Dim X As String
Winsock.GetData X
Reply = Val(X)
X = Trim$(Mid(X, 4))
RaiseEvent ServerResponse(Reply, X)
Message = X
End Sub
Private Sub Winsock_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)
Reply = Number
End Sub
Last edited by moeur; Jul 29th, 2005 at 08:11 PM .
Apr 18th, 2005, 08:45 PM
#2
Apr 19th, 2005, 06:10 AM
#3
Re: Simple VB emailer
Very nice. Another useful thing to add onto this class is to allow one to make an MIME encoded email enabling you to attach additional files to the message.
Apr 20th, 2005, 03:31 PM
#4
Jun 2nd, 2005, 10:29 PM
#5
Re: Simple VB emailer
Why am I getting an error here? I have the clsSMTP class named right, with a winsock control named Winsock on a form, with a button named Commannd1, so what is missing?
VB Code:
Private Sub Class_Initialize()
Set Winsock = New MSWinsockLib.Winsock
End Sub
Invalid use of NEW keyword
Thanks.
[Simple Send Email project]
Jun 3rd, 2005, 12:24 AM
#6
Re: Simple VB emailer
Before using the class you'll have to add a refernce to MSWINSCK.OCX
Goto Project\References Browse to system32\MSWINSOCK.OCX
Jun 3rd, 2005, 02:09 AM
#7
Re: Simple VB emailer
I had it loaded as a component. Thanks.
Now, though I'm getting a SMTP timeout error.
I'll try it again tomorrow.
Thanks.
Jun 3rd, 2005, 05:53 AM
#8
Re: Simple VB emailer
One quick question: In the WaitFor function, you have A declared as a Variant (because you didn't explicitly declare it as anything else)
Was that intentional? Is a Variant required here?
Jun 3rd, 2005, 09:56 AM
#9
Re: Simple VB emailer
A could be single if you wanted.
Aug 7th, 2005, 05:14 PM
#10
Addicted Member
Re: Simple VB emailer
Just what the doctor ordered and can be used as is. However, I found I am having problem with the return address. If I use "YoMamma" or something else made up, it blows up in my face. However, it I use my "real" address, it works perfectly.
BUT:
It adds "@127.0.0.1" after the address. if the person tries to respond to it, they get an error message "553 Invalid address syntax". Any way of getting rid of that last portion?
Aug 7th, 2005, 05:17 PM
#11
Re: Simple VB emailer
That is your ISP trying to prevent spam. I have the same thing, even when I'm not using their smtp server.
Aug 7th, 2005, 06:08 PM
#12
Re: Simple VB emailer
It adds "@127.0.0.1" after the address. if the person tries to respond to it, they get an error message "553 Invalid address syntax". Any way of getting rid of that last portion?
I changed the code in the OpenMail routine a little bit on 7/29.
VB Code:
'send sender's name
MyName = WhoAmI
If InStr(mvarSender, "@") = 0 Then
Winsock.SendData "Mail From:<" & mvarSender & "@" & MyName & ">" & vbCrLf
Else
Winsock.SendData "Mail From:<" & mvarSender & ">" & vbCrLf
End If
See if your code reflects this change.
Aug 9th, 2005, 10:47 PM
#13
Addicted Member
Re: Simple VB emailer
See if your code reflects this change.
Unfortunately it does. I only pulled the code down a couple of days ago and it is using this latter section.
I can "live" with it, just prefer to have it cleaner.
Aug 10th, 2005, 12:12 AM
#14
Re: Simple VB emailer
I assume this is the .Sender address you're talking about.
put some Debug.prints in there somewhere to see what's going on.
what is mvarsender equal to and which line does it send?
Aug 10th, 2005, 12:15 AM
#15
Re: Simple VB emailer
How could I use Yahoo in your class?
Aug 10th, 2005, 12:20 AM
#16
Re: Simple VB emailer
How could I use Yahoo in your class?
if Yahoo requires username and password authentification, then it won't work unless we add something to handle that. I could do that, but I'd need a server to test it on and mine doesn't require such things.
Aug 10th, 2005, 02:53 AM
#17
Hyperactive Member
Re: Simple VB emailer
Why i am getting error "Name conflicts with existing module, project or object library" when adding mswinsck.ocx file from prefrences.
Aug 10th, 2005, 09:56 AM
#18
Re: Simple VB emailer
Don't load the Winsock control into the toolbox.
Aug 11th, 2005, 09:57 PM
#19
Addicted Member
Re: Simple VB emailer
Don't load the Winsock control into the toolbox.
I don't see where I have the Winsock control in the toolbox. I went to components and references and don't see the Winsock. Other than that, where would I check?
Aug 12th, 2005, 01:19 AM
#20
Re: Simple VB emailer
This error happens if you try to load MSWINSCK.OCX twice either in references or components.
Aug 13th, 2005, 12:03 AM
#21
Addicted Member
Re: Simple VB emailer
This error happens if you try to load MSWINSCK.OCX twice either in references or components
Here is what I have loaded. Please take a look at them. I don't see where I have MSWINSCK.OCX loaded twice
References
VB for applications
VB runtime objects & procedures
VBobjects and procedures
MS DAO 3.6 object library
MS ActiveX Data Objectrs 2.5 library
MS Data binding collection
Components
MS ADO Data Control 6.0 (OLEDB)
MS Calendar Control 9.0
MS Comm Controll 6.0
MS Common dialog control 6.0 (SP3)
MS Data Grid Control 6.0 (OLEDB)
MS Flex grid control 6.0
MS Masked edit control 6.0
MS Multimedia control 6.0
MS Rich textbox control 6.0 (SP4)
MS Windows common controls 5.0 (SP2)
MS Windows common controls-2 (SP4)
Designers
Data Environment
Data Report
DHTML Page
Webclass
Insertable Objects
MS Calendar Control 9.0
I really appreciate the help
Aug 13th, 2005, 04:48 AM
#22
New Member
Re: Simple VB emailer
Uhh, i get a strange errror
Userdefined type not defined
and vb selects: Set Winsock = New mswinsocklib.Winsock
Last edited by impClaw; Aug 13th, 2005 at 04:57 AM .
Aug 13th, 2005, 09:55 AM
#23
Re: Simple VB emailer
I would start a new project, add the reference, and cut and past the code.
Aug 13th, 2005, 12:56 PM
#24
New Member
Re: Simple VB emailer
well, i have to do it in this project, is that possible?
I could send the project...
Also what shall the winsock class be named?
EDIT: clsSMPT probable, im such a noob
EDIT: ok, i use vb 5.0, that might be a problem.... since i have to add the winsock as a object not a referance....
Last edited by impClaw; Aug 13th, 2005 at 01:05 PM .
Aug 13th, 2005, 03:51 PM
#25
Re: Simple VB emailer
It's been a long time since I used VB5. Try this prpject to see if it works. Then you can incorporate it into your project.
Attached Files
Aug 14th, 2005, 03:48 AM
#26
New Member
Re: Simple VB emailer
Everything works! exept... i get this error:
[SMTP Error 0 Timeout waiting for server response]
And sometimes i get the [Wrong protecol or connection state for the requested transaction or request] error...
also, what shall the [ .SMTPhost ] be?
Last edited by impClaw; Aug 14th, 2005 at 03:59 AM .
Aug 14th, 2005, 10:35 AM
#27
Re: Simple VB emailer
smtp host has to be a vaild smtp server. People usually use their own ISP. Often the ISP will require a valis return address too.
If you don't have an ISP you could try .SMTPhost = mail.flash.net
Aug 14th, 2005, 12:47 PM
#28
New Member
Re: Simple VB emailer
still getting the same error
it breaks at the line
mvarWinsock.SendData "helo " & mvarSMTPhost & vbCrLf
Aug 14th, 2005, 01:42 PM
#29
Re: Simple VB emailer
You need an SMTP server that is going to cooperate. Can't help you with that I just use my ISP.
Aug 15th, 2005, 05:36 AM
#30
New Member
Re: Simple VB emailer
I searched for free SMTP stuff but found nothing
do you know how do use your ISP, since ive no idea what isp is and how to use my isp:s adress, if it has any..
Aug 15th, 2005, 05:49 AM
#31
Re: Simple VB emailer
ISP = Internet Service Provider.
You posted a question on this subject, which was the correct thing to do. However, questions do not belong in the CodeBank, so, I moved your question to where is should be, and where it will receive the most attention, and that is the ClassicVB section.
Aug 15th, 2005, 12:09 PM
#32
New Member
Re: Simple VB emailer
Hmm, i fixed a SMTP server (smtp.gmail.com), still getting the same old error.
here is the info i got about the SMTP:
smtp.gmail.com (use authentication)
Use Authentication: Yes
Use STARTTLS: Yes (some clients call this SSL)
Port: 465 or 587
Last edited by impClaw; Aug 15th, 2005 at 12:42 PM .
Aug 16th, 2005, 03:31 AM
#33
Addicted Member
Re: Simple VB emailer
great program .... , but why it cannot be run twice
Aug 16th, 2005, 10:44 AM
#34
Re: Simple VB emailer
I have no problem running it twice, what error are you getting?
Aug 17th, 2005, 09:10 PM
#35
Addicted Member
Re: Simple VB emailer
the program not responding.
try to do this (in vb ide):
1. run the program once (F5)
2. send a message
3. close the log form
4. close the mail form
5. run the program again (F5)
6. send another message
7. it's not responding
and i don't know why this happen?
Aug 17th, 2005, 09:39 PM
#36
Re: Simple VB emailer
What is the log form?
If you have altered the code I'll have to see it.
Aug 17th, 2005, 10:03 PM
#37
Addicted Member
Re: Simple VB emailer
no , i did not altered it at all.
i will explain to you again , i open your project with vb6 , then i run it by pressing F5 , frmmail show up (mail form) , then i click the "Send" command after all configuration needed all sets , then frmLog show up (log form) , after that i click the "Close" command, back to the mail form then i close the form and back to the IDE, and 1 cycle is completed.
then i do the same things again , but not responding after i click the "Send" command.
hope you understand what i mean this time.
Aug 17th, 2005, 10:29 PM
#38
Re: Simple VB emailer
what project are you opening that has a frmLog? One of mine?
Aug 18th, 2005, 08:35 PM
#39
Addicted Member
Re: Simple VB emailer
sorry , look like i on wrong forum , sorry moeur.
after i check the code again , i think it is not yours.
Aug 19th, 2005, 01:54 PM
#40
New Member
Re: Simple VB emailer
moeur, could you show how to make SMTP acces if you need login and Password, since i cannot find any SMTP without password request, and my ISP dont provide that...
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width