473,385 Members | 1,397 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Socket Programming in VBA

126 Expert 100+
Can anyone point me in the right direction here?
As part of a database I'm designing, it has to be able to connect to an SMTP server. I know how to go about the protocol once connected, I know the hostname and port, I just don't know how to connect in the first place.

All I need to know is:
1. What object type allows me to make this connection? And what references do I need to be able to do this?
2. How do I open a connection, send to the server, read from the server, and close?

Thank-you in advance.
Sep 7 '07 #1
4 5231
Jim Doherty
897 Expert 512MB
Can anyone point me in the right direction here?
As part of a database I'm designing, it has to be able to connect to an SMTP server. I know how to go about the protocol once connected, I know the hostname and port, I just don't know how to connect in the first place. In addition I store the name of the SMTP server in a one line configtable in the frontend database in case the smtp servername changes and I don't want to revisit my code meely for this

All I need to know is:
1. What object type allows me to make this connection? And what references do I need to be able to do this?
2. How do I open a connection, send to the server, read from the server, and close?

Thank-you in advance.

Set a reference to Microsoft CDO for Windows 2000 Library


Here is a fully tested Function that I use for one of my production systems where parameters are passed in which includes a concatenated list of file attachments from a pop up dialog Access form resembling the usual outlook form. The form itself uses the usual automation techniques in order to retrieve outlook folders,users email addresses etc but which DOESNT use outlook to send the mail message instead favouring simple SMTP direct server access for sending the message but with a 'copy' returning back into outlook (their standard mail client for proof of sending... they didnt want to get into trying to byepass the security SP2 annoying popup message and didnt want to use ClickYes PRO small third party app either :)

Just remove the lines where I am calling in the function PLEASEWAIT This is merely a floating pop I use to relay information to the user as to the status of action in the send process so that they know whats going on (you might want to keep something like that in yours? thats why I left the lines in... whatever.) I don't know whether you have your own method for that but if you want the function let me know.

I also lookup the string value of the SMTP server name from a one line config table in case the server name changes in future and I don't want to revisit my code just for that. The value of which is editable from the usual dialog setup screens I tend to supply for variable system config stuff.


Expand|Select|Wrap|Line Numbers
  1. Public Function SendCDOMail(ByVal strFrom As String, ByVal strTo As String, intPerID, ByVal strSubject As String, ByVal strBody As String, strAttachmentPath As String) As Boolean
  2. On Error GoTo Err_SendCDOMail
  3.    If Trim$(strFrom) = "" Then
  4.         MsgBox "No email for the sender portion has been specified", vbExclamation, "Send Mail"
  5.         SendCDOMail = False
  6.         Exit Function
  7.     End If
  8.     If Trim$(strTo) = "" Then
  9.         MsgBox "No recipient email address has been specified", vbExclamation, "Send Mail"
  10.         SendCDOMail = False
  11.         Exit Function
  12.     End If
  13.    If Trim$(strSubject) = "" Then
  14.         MsgBox "No subject heading for the email was retrieved!", vbExclamation, "Send Mail"
  15.         SendCDOMail = False
  16.         Exit Function
  17.     End If
  18.    If Trim$(strBody) = "" Then
  19.         MsgBox "No text body for the email was retrieved!", vbExclamation, "Send Mail"
  20.         SendCDOMail = False
  21.         Exit Function
  22.     End If
  23.  
  24. Dim iCfg As CDO.Configuration
  25. Dim iMsg As CDO.Message
  26. Dim mailserver As String
  27. mailserver = DLookup("[SMTPServerName]", "Usys_tblConfigLocal")
  28.  
  29. Set iCfg = New CDO.Configuration
  30. PleaseWait ("Setting up email connection using C.D.O protocol...")
  31. With iCfg
  32. .Fields(cdoSMTPServer) = mailserver
  33. .Fields(cdoSMTPServerPort) = 25 ' typically
  34. .Fields(cdoSendUsingMethod) = cdoSendUsingPort
  35. .Fields(cdoSMTPConnectionTimeout) = 200
  36. .Fields.Update
  37. End With
  38.  
  39. Set iMsg = New CDO.Message
  40. With iMsg
  41. Set .Configuration = iCfg
  42. .From = ""
  43. .Sender = strFrom
  44. .ReplyTo = strFrom
  45. .Subject = strSubject
  46. .TextBody = strBody
  47. .To = strTo
  48. .CC = strFrom
  49. If strAttachmentPath <> "" Then
  50.     PleaseWait ("Gathering file attachments...")
  51.                         Dim Count As Integer, f() As String, i As Integer, FileName As String, myfiles As String
  52.                         Erase f
  53.                         FileName = Dir(strAttachmentPath, vbNormal + vbHidden + vbSystem)    ' Get first file name.
  54.                         'objEmail.Attachments.Add FileName
  55.                         'Iterate through PATH, caching all files in F()
  56.                         Do While FileName <> ""
  57.  
  58.                             If FileName <> "." And FileName <> ".." Then
  59.                                 If (GetAttr(strAttachmentPath + FileName) And vbDirectory) <> vbDirectory Then
  60.                                     If err <> 53 And err <> 76 Then
  61.                                         If (Count Mod 10) = 0 Then
  62.                                             ReDim Preserve f(Count + 10)    ' Resize the array.
  63.                                         End If
  64.                                         Count = Count + 1    ' Increment counter.
  65.                                         f(Count) = FileName
  66.                                         PleaseWait ("Retrieving filenames..." & FileName)
  67.                                     End If
  68.                                 End If
  69.                             End If
  70.  
  71.  
  72.                             .AddAttachment strAttachmentPath + FileName
  73.                             FileName = Dir    ' Get another file name.
  74.                         Loop
  75. End If
  76. PleaseWait ("Sending " & strSubject & " email please be patient. I am communicating with the server. You will be informed of the result...")
  77. .Send
  78. End With
  79.     msg = "Message Sent!" & vbCrLf & vbCrLf
  80.     msg = msg & "A copy of this email has been sent (CC'd)" & vbCrlf
  81.     msg = msg & "to your email inbox as proof of sending"
  82.     MsgBox msg, vbInformation, "Application Name Email Message"
  83. On Error Resume Next
  84.  
  85.  
  86. SendCDOMail = True
  87. Set iMsg = Nothing
  88. Set iCfg = Nothing
  89. PleaseWait ("")
  90. Exit_SendCDOMail:
  91.     Exit Function
  92.  
  93. Err_SendCDOMail:
  94. PleaseWait ("")
  95. Set iMsg = Nothing
  96. Set iCfg = Nothing
  97. SendCDOMail = False
  98.     MsgBox err.Description, vbInformation, "SendCDOMail Function error Command Cancelled"
  99.     Resume Exit_SendCDOMail
  100.  
  101. End Function

Regards

Jim
Sep 7 '07 #2
Stwange
126 Expert 100+
That's brilliant mate, and a lot more than I expected!

Thank-you.
Sep 7 '07 #3
Scott Price
1,384 Expert 1GB
Ignore me, just subscribing :-)

Regards,
Scott
Sep 7 '07 #4
Jim Doherty
897 Expert 512MB
That's brilliant mate, and a lot more than I expected!

Thank-you.
You're welcome

Jim
Sep 9 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: pyguy2 | last post by:
Issues of socket programming can be wierd, so I'm looking for some comments. In my python books I find exclusive use of socket.close(). From my other readings, I know about a "partial close...
5
by: John Sheppard | last post by:
Hi all, I am not sure that I am posting this in the right group but here it goes anyway. I am new to socket programming and I have been searching on the internet to the questions I am about to pose...
1
by: John Sheppard | last post by:
Thanks to everyone that responded to my previous Socket Programming question. Now I have run into some behavior that I don't quite understand. Programming environment. VS.NET 2003, C#, Windows...
5
by: mscirri | last post by:
The code below is what I am using to asynchronously get data from a PocketPC device. The data comes in fine in blocks of 1024 bytes but even when I send no data from the PocketPC constant blocks of...
2
by: djc | last post by:
I read a network programming book (based on framework 1.1) which indicated that you should 'never' use the RecieveTimeout or the SendTimeout 'socket options' on TCP sockets or you may loose data. I...
10
by: Uma - Chellasoft | last post by:
Hai, I am new to VB.Net programming, directly doing socket programming. In C, I will be able to map the message arrived in a socket directly to a structure. Is this possible in VB.Net. Can...
11
by: atlaste | last post by:
Hi, In an attempt to create a full-blown webcrawler I've found myself writing a wrapper around the Socket class in an attempt to make it completely async, supporting timeouts and some scheduling...
0
by: shonen | last post by:
I'm currently attempting to connect to a shoutcast server pull down the information from here and then I'll parse it. I got this working with the httplib, which was great, the problem is I want...
8
by: =?Utf-8?B?Sm9obg==?= | last post by:
Hi all, I am new to .net technologies. ASP.NET supports socket programming like send/receive in c or c++? I am developing web-site application in asp.net and code behind is Visual C#. In...
3
by: Stuart | last post by:
I am in the process of teaching myself socket programming. I am "playing around" with some simple echo server-client programs for m the book TCP/IP Sockets in C. The Server program is: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.