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?
.Recipient = "whomever@whereever.net"
'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)
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.
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?
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?
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.
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?
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.
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?
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
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.
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
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.
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...