Mike Gagnon Canada Membre Simple # 0000000025 enregistré le 14/10/2004
Gagnon Mike Pointe Cla H9R 3K8 de la société Carver Technologies Inc. Fiche personnelle
Note des membres pas de note
Contributions > 05 - API et appels systèmes
Comment trouver le serveur SMTP, le port SMTP, l'authentification d'une adresse par defaut
# 0000000830
ajouté le 11/07/2012 12:40:32 et modifié le 11/07/2012
consulté 8840 fois
Niveau
initié
Version(s) Foxpro : VFP 9.0
Description
Le code qui suit a été testé avec VFP9 et Windows 7 32 bit ET 64 bit.
Il est possible que données de la base de registre soit à un endroit different.
Code source :
Private goApp Local loOutlook,ns Private lcDefaultEmail,lcFolder,lcSMTPServer DeclareInteger RegCloseKey In advapi32 Integer hKey DeclareInteger RegOpenKeyEx In advapi32; INTEGER hKey, String lpSubKey, Integer ulOptions,; INTEGER samDesired, Integer @phkResult DeclareInteger RegEnumValue In advapi32; INTEGER hKey, Integer dwIndex, String @lpValueName,; INTEGER @lpcValueName, Integer lpReserved, Integer @lpType,; STRING @lpData, Integer @lpcbData
loOutlook = Createobject('outlook.application') && Get the default e-mail address from Outlook
ns = loOutlook.GetNameSpace('MAPI')
lcDefaultEmail = ns.CurrentUser.Address && Now lets find out in which folder in the registry this e-mail address belongs to
goApp = Createobject('capp') For i = 1 To 10 && Change this in case there are more than 10 e-mail addresses
lcFolder = ''
lcFolder=getfolder(i,lcDefaultEmail) If lcFolder <> -1 Exit Endif Endfor
lcFolder=Padl(lcFolder,8,'0')
lcSubKey = 'Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\Outlook\9375CFF0413111d3B88A00104B2A6676\'+lcFolder
lcSMTPServer=goApp.ReadReg('HCU',lcSubKey,'SMTP Server')
lcSMTPPort=goApp.ReadReg('HCU',lcSubKey,'SMTP Port')
lcSMTPSecureconnection=goApp.ReadReg('HCU',lcSubKey,'SMTP Secure Connection')
lcSMTPUseAuth=goApp.ReadReg('HCU',lcSubKey,'SMTP Use Auth')
? 'Adresse courriel par defaut: ' +lcDefaultEmail
? 'Serveur SMTP : '+lcSMTPServer
? 'Port SMTP: '+lcSMTPPort
? 'Connection SMTP: '+lcSMTPSecureconnection
? 'Authorization: '+lcSMTPUseAuth ************************************************** * DefineClass capp AsContainer *-- Reads the windows Registry. Procedure ReadReg * Start of Code Lparameters tcKey,tcSubKey,tcValue IfPcount() = 2
tcValue = "" Endif
DoCase Case m.tcKey == "HCR"
nKey = HKEY_CLASSES_ROOT Case m.tcKey == "HLM"
nKey = HKEY_LOCAL_MACHINE Case m.tcKey = "HCU"
nKey = HKEY_CURRENT_USER Case m.tcKey = "HCR"
nKey = HKEY_CLASSES_ROOT Otherwise
nKey = m.tcKey Endcase
cSubKey = m.tcSubKey
cValue = m.tcValue
* here is where we will actually read the registry if tcValue = 'SMTP Server'
cValueRead = This.ReadREG_bin(nKey, cSubKey, cValue) endif if tcValue = 'SMTP Port'
cValueRead = This.ReadREG_dword(nKey, cSubKey, cValue) endif if tcValue = 'SMTP Secure Connection'
cValueRead = This.ReadREG_dword(nKey, cSubKey, cValue) endif if tcValue = 'SMTP Use Auth'
cValueRead = This.ReadREG_dword(nKey, cSubKey, cValue) endif If (Empty(cValueRead)) Then
cValueRead = "REGISTRY KEY NOT FOUND" * MESSAGEBOX("Function Not Successful.") && testing only Else * MESSAGEBOX("Function Successful. " + cValueRead) && testing only Endif Return cValueRead
Endproc
*-- API's used by the ReadReg method. Procedure ReadREG_bin * * This function reads a REG_BINARY value from the registry. If successful, * it will return the value read. If not successful, it will return an empty string.
Parameters nKey, cSubKey, cValue * nKey The root key to open. It can be any of the constants defined below. * #DEFINE HKEY_CLASSES_ROOT -2147483648 * #DEFINE HKEY_CURRENT_USER -2147483647 * #DEFINE HKEY_LOCAL_MACHINE -2147483646 * #DEFINE HKEY_USERS -2147483645 * cSubKey The SubKey to open. * cValue The value that is going to be read.
* Constants that are needed for Registry functions
#Define REG_BINARY 3
* WIN 32 API functions that are used DeclareInteger RegOpenKey In Win32API ; Integer nHKey, String @cSubKey, Integer @nResult DeclareInteger RegQueryValueEx In Win32API ; Integer nHKey, String lpszValueName, Integer dwReserved,; Integer @lpdwType, String @lpbData, Integer @lpcbData * Local variables used Local nErrCode && Error Code returned from Registry functions Local nKeyHandle && Handle to Key that is opened in the Registry Local lpdwValueType && Type of Value that we are looking for Local lpbValue && The data stored in the value Local lpcbValueSize && Size of the variable Local lpdwReserved && Reserved Must be 0
nErrCode = RegOpenKey(nKey, cSubKey, @nKeyHandle) * If the error code isn't 0, then the key doesn't exist or can't be opened. If (nErrCode # 0) Then Return"" Endif
lpcbValueSize = 1 * Get the size of the data in the value
nErrCode=RegQueryValueEx(nKeyHandle, cValue, lpdwReserved, @lpdwValueType, @lpbValue, @lpcbValueSize)
* Make the buffer big enough
lpbValue = Space(lpcbValueSize)
nErrCode=RegQueryValueEx(nKeyHandle, cValue, lpdwReserved, @lpdwValueType, @lpbValue, @lpcbValueSize)
=RegCloseKey(nKeyHandle) If (nErrCode # 0) Then Return"" Endif
lpbValue = Left(lpbValue, lpcbValueSize - 1)
lpbValue =u2a( lpbValue ) Return lpbValue Endproc Procedure ReadREG_dword * This function reads a REG_DWORD value from the registry. If successful, * it will return the value read. If not successful, it will return an empty string.
Parameters nKey, cSubKey, cValue * Constants that are needed for Registry functions
#DEFINE REG_DWORD 4 * WIN 32 API functions that are used DeclareInteger RegOpenKey In Win32API ; Integer nHKey, String @cSubKey, Integer @nResult DeclareInteger RegQueryValueEx In Win32API ; Integer nHKey, String lpszValueName, Integer dwReserved,; Integer @lpdwType, String @lpbData, Integer @lpcbData * Local variables used Local nErrCode && Error Code returned from Registry functions Local nKeyHandle && Handle to Key that is opened in the Registry Local lpdwValueType && Type of Value that we are looking for Local lpbValue && The data stored in the value Local lpcbValueSize && Size of the variable Local lpdwReserved && Reserved Must be 0
nErrCode = RegOpenKey(nKey, cSubKey, @nKeyHandle) * If the error code isn't 0, then the key doesn't exist or can't be opened. If (nErrCode # 0) Then Return"" Endif
lpcbValueSize = 1 * Get the size of the data in the value
nErrCode=RegQueryValueEx(nKeyHandle, cValue, lpdwReserved, @lpdwValueType, @lpbValue, @lpcbValueSize)
* Make the buffer big enough
lpbValue = Space(lpcbValueSize)
nErrCode=RegQueryValueEx(nKeyHandle, cValue, lpdwReserved, @lpdwValueType, @lpbValue, @lpcbValueSize)
lpbValue= alltrim(str(buf2dword(PADR(lpbValue,4,Chr(0)))))
=RegCloseKey(nKeyHandle) If (nErrCode # 0) Then Return"" Endif Return lpbValue Endproc