Re: Coding the Winsock control....

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Brad Pears (donotreply_at_notreal.com)
Date: 11/11/04


Date: Thu, 11 Nov 2004 12:24:42 -0500

Great, I'll give that a shot... Except, we do have some Win98 clients as
well...

CDO will not work for those clients?

"Ron Weiner" <weinNoSpam1@mindspring.com> wrote in message
news:%23$bstF2xEHA.2600@TK2MSFTNGP09.phx.gbl...
> Brad
>
> If your clients are Win2K or later you might want to consider sending mail
> Via Microsoft CDO. Here is some sample code to get you started.
>
>
> Public Sub testCDO()
> ' Purpose Send an Email with or without an attachment without using
> Outlook or other MAPI client
> ' Uses Late Binding - Does not need a reference to the Microsoft
> CDO For Windows library
> ' But the system CdoSys registered. CDOSys comes standard on
> Windows 2K and higher
> ' This code will likely fail on a Win 98 box
>
> Const cdoSendUsingPort = 2
> Const cdoBasic = 1
> Dim objCDOConfig As Object, objCDOMessage As Object
> Dim strSch As String
>
> strSch = "http://Schemas.microsoft.com/cdo/configuration/"
> Set objCDOConfig = CreateObject("CDO.Configuration")
> With objCDOConfig.Fields
> .Item(strSch & "sendusing") = cdoSendUsingPort
> .Item(strSch & "smtpserver") =
"SomeSecureMailServer.SomeDomain.COM"
> 'Use only when SMTP server requires Authentication - Otherwise Rem
> out
> .Item(strSch & "SMTPAuthenticate") = cdoBasic
> .Item(strSch & "SendUserName") = "SomeName@SomeDomain.com"
> .Item(strSch & "SendPassword") = "YourPassword"
> .Update
> End With
>
> Set objCDOMessage = CreateObject("CDO.Message")
> With objCDOMessage
> Set .Configuration = objCDOConfig
> .From = "Your Nice Name"
> .Sender = "You@YourDomain.com"
> .To = "Receiver@HerDomain.com"
> .Subject = "Sample CDO Message"
> '.TextBody = "This is a test for CDO.message"
> .HTMLBody = "This is a test for CDO.message. This is not Bold But
> <B>This is!</B>"
> '.AddAttachment "c:\Inv83595.pdf"
> '.MDNRequested = True
> .Send
> End With
> Set objCDOMessage = Nothing
> Set objCDOConfig = Nothing
> End Sub
>
> I have found that this code to be reliable in my customers environment.
> Since it uses late binding there are no references to worry about, BUT you
> need to add error handler for cases where CDO is not available (Win 98),
and
> when the mail server is not available, or authentication fails, or ...
> Well you get the idea.
>
> Ron W
> "Brad Pears" <donotreply@notreal.com> wrote in message
> news:uPm3Ed0xEHA.3416@TK2MSFTNGP09.phx.gbl...
> > I have some sample code on connecting to a mail server using the winsock
> > control to send an email automatically from an Access 2000 project...
> >
> > However, I am not getting a connect request back from the mail server at
> > all...There must be a missing step somewhere...
> >
> > Does someone have some sample code they could send my way that will
allow
> > the client computer to connect to an SMTP server over port 25 and send
an
> > email?
> >
> > The code I have seems to work but the .Connect request to the email
server
> > just sits there and never comes back. It seems as a connection is never
> > established.
> >
> > Here is the sample code....
> >
> > (Form declaration code)
> >
> > Option Compare Database
> > Dim winsock1 As Winsock
> >
> > (command button code)
> >
> > Private Sub cmdSendMail_Click()
> > Call SMTPSend("mydomain.com", "192.168.2.15", "bradp", "friggin", "This
is
> > the Subject", "This is the body")
> >
> > End Sub
> >
> > ' Routine to send email
> >
> > Sub SMTPSend(strMyDomain As String, _
> > strEmailServer As String, _
> > strEmailAddressWithoutDomain As String, _
> > strWhoToSayThisIsFrom As String, _
> > strSubject As String, _
> > strMessageBody As String)
> > Set winsock1 = Me!axWinsockServer.Object
> > winsock1.Protocol = sckTCPProtocol
> > winsock1.RemoteHost = strEmailServer
> > winsock1.RemotePort = 25
> > winsock1.Connect
> >
> > ' Wait for connection
> > WaitForIt
> >
> > Select Case Left$(strWSin, 3)
> > Case "220"
> > ' connected ok, send HELLO
> > winsock1.SendData "HELO " & _
> > strMyDomain & _
> > vbCrLf
> > WaitForIt
> > winsock1.SendData "MAIL FROM: " & _
> > strWhoToSayThisIsFrom & _
> > vbCrLf
> > WaitForIt
> > winsock1.SendData "RCPT TO: " & _
> > strWhoToSayThisIsFrom & _
> > vbCrLf
> > WaitForIt
> > winsock1.SendData "DATA" & vbCrLf & _
> > "DATE:" & _
> > Format$(Now(), "mm/dd/yyyy hh:mm:ss") & _
> > "FROM: " & _
> > strWhoToSayThisIsFrom & vbCrLf & _
> > "TO:" & _
> > Format$(Now(), "mm/dd/yyyy hh:mm:ss") & _
> > "DATE:" & _
> > Format$(Now(), "mm/dd/yyyy hh:mm:ss") & _
> > "SUBJECT: " & _
> > strSubject & _
> > vbCrLf & _
> > strMessageBody & _
> > vbCrLf & _
> > "." & vbCrLf
> > ' note: . & vbcrlf terminates the "send"
> > WaitForIt
> > ' parse and validate the return from the sever
> > End Select
> > ' tell the server you're done:
> > winsock1.SendData "QUIT" & vbCrLf
> > ' and that's it!
> > winsock1.Close
> > End Sub
> >
> >
> > Private Sub WaitForIt()
> > WaitingforData = True
> > While WaitingforData = True
> > DoEvents
> > Wend
> > End Sub
> >
> > ' This should run when a connection has been established - but it never
> > does....
> >
> > Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
> >
> > Dim Temp As String
> > Temp = String(bytesTotal, " ")
> > winsock1.GetData Temp, vbString
> > Do
> > If Right$(Temp, 1) = vbLf Then
> > Temp = Left$(Temp, Len(Temp) - 1)
> > End If
> > Loop While Right$(Temp, 1) = vbLf
> > strWSin = Temp
> > WaitingforData = False
> > End Sub
> >
> >
> > Any help with this would be greatly appreciated!!!
> >
> > Thanks,
> >
> > Brad
> >
> >
>
>



Relevant Pages

  • Re: Coding the Winsock control....
    ... CDO will not work for those clients? ... objCDOMessage As Object ... > End Sub ...
    (microsoft.public.access.forms)
  • Re: VB Code to Click Yes Outlook Security
    ... I am using CDO for a automated mailing of a report. ... With our local exchange server and Outlook I can give in Excel VBA SendMail ... >> Private Sub Form_Load ...
    (microsoft.public.excel.programming)
  • Re: Insert new date once.
    ... sub putdate() ... Lets say that you have a spreadsheet that you re-use for clients (Save as ... You can submit your question to MSDN newsgroup as they are best suited to ...
    (microsoft.public.excel.worksheet.functions)
  • Re: Singleton pattern in VB6?
    ... there are a lot of clients to this ActiveX Server. ... > threads are messy because the Sub Mainis fired for the creation of each ... I understand, but is there so much Init-Code inside the Sub Main, that is ... so the "lot of Clients" are running on the MiddleTier inside IIS on ...
    (microsoft.public.vb.general.discussion)
  • Re: Coding the Winsock control....
    ... Is there an alternative to using winsock in .Net? ... objCDOMessage As Object ... > End Sub ... > when the mail server is not available, or authentication fails, or ... ...
    (microsoft.public.access.formscoding)