Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sending email through Vb 1

Status
Not open for further replies.

oops4me

Programmer
Jul 7, 2003
50
IN
hello,
i got one code for sending mails through vb. th code is as follows

Option Explicit


Private nConnected As Boolean
'Private WithEvents Winsock1 As Winsock
Private ptRetCode As Boolean
Public Event Status(txtStatus As String)


Sub SendBrainDead(txtServer As String, szEmailFrom As String, szNameFrom As String, szEmailTo As String, szNameTo As String, szSubject As String, szMsg As String)


'txtServer smtp.mailserver.com
'szEmailFromyourname@bus.com the persons email address thats send
' ing the email
'szNameFromJohn Doe Your real name or even your alias
'szEmailTo recieving~party@their~pop.net Person your sending an e
' -mail to
'szNameTo Annie Doe reciever's name.
'szSubject This will show as the subject in the message.
'szMsg This is ofcourse the place you put you message.
'NOTE: I do not do any MIME or BASE64 in this code if someone cre
' ates an OCX for this
'i'd love a copy.
'-- This routine sends an email message via an SMTP gateway.


If Not nConnected Then 'Check to see if its a used control
InitIt txtServer
End If

Dim szCRLF As String
Dim szCompleteMsg As String
'-- All lines end with a CR/LF Pair
szCRLF = Chr$(13) & Chr$(10)
'-- Construct the data as a 4-line header (Date, From,
'To, and Subject) followed by the message text
szCompleteMsg = "DATE: " & Format$(Now, "dd mmm yy ttttt") & szCRLF
szCompleteMsg = szCompleteMsg & "FROM: " & szNameFrom & szCRLF
szCompleteMsg = szCompleteMsg & "TO: " & szNameTo & szCRLF
szCompleteMsg = szCompleteMsg & "SUBJECT: " & szSubject & szCRLF & szCRLF
szCompleteMsg = szCompleteMsg & szMsg & szCRLF
'-- Wait for a response,don't just send blindly
Winsock1.SendData "HELO " & Winsock1.LocalHostName & szCRLF
WaitLoop
Winsock1.SendData "MAIL FROM: <" & szEmailFrom & ">" & szCRLF
WaitLoop 'This will allow the SMTP server time to process your request
Winsock1.SendData "RCPT TO: <" & szEmailTo & ">" & szCRLF
WaitLoop 'This will allow the SMTP server time to process your request
Winsock1.SendData "DATA" & szCRLF
WaitLoop 'This will allow the SMTP server time to process your request
Winsock1.SendData szCompleteMsg & szCRLF
WaitLoop 'This will allow the SMTP server time to process your request
Winsock1.SendData "." & szCRLF
WaitLoop 'This will allow the SMTP server time to process your request
'Clean up the control
Winsock1.SendData "QUIT" & szCRLF
Winsock1.Close
Winsock1.RemotePort = 0
Winsock1.LocalPort = 0
'Set Winsock1 = Nothing
nConnected = False
End Sub



Private Sub Form_Load()
'SendBrainDead(txtServer As String, szEmailFrom As String, szNameFrom As String, szEmailTo As String, szNameTo As String, szSubject As String, szMsg As String)
Call InitIt("server ip address")
Call SendBrainDead("server ip address", "abc@xyz.com", "Ganu", "xyz@abc.com", "ajay", "hi test", "test msg with ")

End Sub

Private Sub Winsock1_Connect()

nConnected = True
End Sub



Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)

Dim strRet As String
Winsock1.GetData strRet, vbString, bytesTotal
RaiseEvent Status(strRet)
ptRetCode = True
End Sub



Private Sub InitIt(txtServer As String)

'-- Temporarily disable the button
'Set Winsock1 = Winsock1
Screen.MousePointer = vbHourglass
'btnSend.Enabled = False
'-- SMTP uses port 25
Winsock1.RemotePort = 25
Winsock1.RemoteHost = txtServer
'-- Try to connect
nConnected = False
'On Error Resume Next
Winsock1.Connect


Do


DoEvents
Loop Until nConnected

Screen.MousePointer = vbNormal
End Sub



Private Sub WaitLoop()

'Wait for a return
'I DONT check return values
'You should do some error protection.


Do Until ptRetCode = True


DoEvents
Loop

ptRetCode = False
End Sub

this code runs without any error but i am not receiving any mails.

please tell me what is error.
Ajay
 
I second vbSendmail. You can search this forum for threads on vbSendmail for more info.

zemp
 
thanx to all of you for helping me.

can u give me the code. how to use cdo. but i don't want third party mailing s/w(like MS Outlook/ outlook express) to send my mails. i want a code through which i can directly send mails through vb.

also can any one give me code which will connect to smtp server & from there it will send the mail.
 
May look a little long, but about half of this is comments
Code:
[blue]Option Explicit
' Ensure you have a reference to Microsoft CDO for Exchange 2000
' or to Microsoft CDO for Windows 2000
 
Private Sub Command1_Click()
    Dim iMsg As CDO.Message
    Dim iConf As CDO.Configuration
    
    Set iMsg = New CDO.Message
    Set iConf = New CDO.Configuration

    'Apply settings to the configuration object
    With iConf.Fields
        ' Don't bother with the following 3 lines of code if the SMTP server you will be connecting to
        ' does not require authentication
        
        ' Specify the authentication mechanism to basic (clear-text) authentication.
        .Item(cdoSMTPAuthenticate) = cdoBasic
        ' The username for authenticating to an SMTP server
        .Item(cdoSendUserName) = "myusername"
        ' The password used to authenticate to an SMTP server
        .Item(cdoSendPassword) = "mypassword"
        
        ' How to send the mail
        .Item(cdoSendUsingMethod) = cdoSendUsingPort
        'Specify mail server
        .Item(cdoSMTPServer) = "target.smtpserver.com"
        'Specify the timeout in seconds
        .Item(cdoSMTPConnectionTimeout) = 10
        ' The port on which the SMTP service specified by the smtpserver field is listening for connections (typically 25)
        .Item(cdoSMTPServerPort) = 25
        
        ' Ensure configuration is up to date
        .Update
    End With

    Set iMsg.Configuration = iConf
    iMsg.To = "destination@email.com"
    iMsg.Subject = "Example"
    iMsg.TextBody = "Hello"
    iMsg.From = "source@home.com"
    
    iMsg.Send
    Set iMsg = Nothing
End Sub[/blue]
Just for fun, here's the short version (same reference still required):
Code:
[blue]Option Explicit
 
Private Sub Command1_Click()
    With New CDO.Message
        With .Configuration.Fields
            .Item(cdoSMTPAuthenticate) = cdoBasic
            .Item(cdoSendUserName) = "myusername"
            .Item(cdoSendPassword) = "mypassword"
            .Item(cdoSendUsingMethod) = cdoSendUsingPort
            .Item(cdoSMTPServer) = "target.smtpserver.com"
            .Item(cdoSMTPConnectionTimeout) = 10
            .Item(cdoSMTPServerPort) = 25
            .Update
        End With
        .To = "destination@email.com"
        .Subject = "Example"
        .TextBody = "Hello"
        .From = "source@home.com"
        .Send
    End With
End Sub[/blue]
 
vbsendmail will work on all Windows versions.
CDO only works on 2k and XP and 2003 server.

vbsendmail works alone.
CDO can work alone or through a client.

both are free, but you get the source for vbsendmail.

Regards

Frederico Fonseca
SysSoft Integrated Ltd
 
Oh, I stopped worrying about W9x/Me some time ago. 2k, XP and 2003 are the only current versions of Windows, so I question your use of the word 'only' ;-)

 
I can't ignore the 9x platforms entirely yet, so for me that could be an issue. In such cases something like w3 JMail or vbsendmail would probably be preferable to "rolling my own."

I'm pretty sure that the early forms of CDO that were paired with Outlook 97, 98, 2000 run on Win9x but I also suspect they can't use SMTP directly.
 
CDONT is the previous version of CDOSYS.
It required a Email client to be configured on each PC.
CDOSYS can still use an email client, but it can also use SMTP servers directly.

Major choice for this is --- do I need to keep a log of the emails sent on an email client --- If the answer is yes then vbsendmail is not for you.

Regards

Frederico Fonseca
SysSoft Integrated Ltd
 
CDO 1.2.1 (rather than CDONT) was the spiritual predecessor of CDO for Windows 2000, and did indeed require at least a MAPI subsystem to be installedon the client PCs.

CDONTS, which was CDO for NT Server, was a different, simpler beast that made sending emails from ASP really easy (and could be conned into a number of other purposes) and whose object model was similar to that of CD) 1.2.1 so that code could be relatively easily ported between the two.
 
CDONTS used the NT SMTP Service from the old Option Pack I believe.

So it seems that to avoid having multiple versions of an application some 3rd party component is probably the answer. Especially if one doesn't want to "ride atop" some mail client subsystem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top