1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
| Dim ConvertMail()
dim I
Const cdoAnonymous = 0 'Do not authenticate
Const cdoBasic = 1 'basic (clear-text) authentication
Const cdoNTLM = 2 'NTLM
const SSL=false
'Delivery Status Notifications
Const cdoDSNDefault = 0 'None
Const cdoDSNNever = 1 'None
Const cdoDSNFailure = 2 'Failure
Const cdoDSNSuccess = 4 'Success
Const cdoDSNDelay = 8 'Delay
Const cdoDSNSuccessFailOrDelay = 14 'Success, failure or delay
Dim Erreur
dim con
set con =new Ado
con.TYPEBASE =4
con.Fichier = "\\Automate_mail_RD\Mail.mdb"
dim Sql
dim RS
dim Expediteur
dim Server
dim Autentification
dim Utilisateur
dim PasWod
dim Ports
dim TimeAout
dim MySSL
adLockOptimistic=3
adOpenDynamic=2
con.OpenConnetion
Sql = "SELECT SMTP.* FROM SMTP;"
'FichierLog Hour(time) & ":" & Minute(time) & vbcrlf
Set Rs = con.OpenRecordSet(Sql)
While Rs.EOF = False
Expediteur= trim("" & Rs("Expediteur"))
Server = trim("" & Rs("Serveur"))
Autentification = Rs("Autentification")
Utilisateur = trim("" & Rs("Utilisateur"))
PasWod = trim("" & Rs("PasWod"))
Ports = Rs("Port")
TimeAout = Rs("TimeAout")
MySSL=Rs("SSL")
Rs.MoveNext
Wend
Sql="SELECT SubstituerAdresseMail.* FROM SubstituerAdresseMail;"
Set Rs = con.OpenRecordSet(Sql)
I=0
While Rs.EOF = False
redim preserve ConvertMail(I)
ConvertMail(I)=rs("Adress") & ";" & rs("AdressRemplace")
I=I+1
Rs.MoveNext
Wend
Sql="SELECT Job.* FROM Job;"
Set Rs = con.OpenRecordSet(Sql)
While Rs.EOF = False
If Rs("Envoyer") = False Then
Rs("Envoyer") = MailEnvoi( Server, Autentification,MySSL,Utilisateur,PasWod,Ports,TimeAout, RetournAddressMail(Expediteur),RetournAddressMail(rs("To")),RetournAddressMail(rs("CC")),RetournAddressMail(rs("CCB")),rs("Objet"),rs("Body"),rs("PJ"))
Rs.Update
End If
Rs.MoveNext
Wend
Set Rs = con.CloseRecordSet(Rs)
Sql = "DELETE Job.*, Job.Envoyer FROM Job WHERE Job.Envoyer=True;"
con.Execute Sql
con.CloseConnection
'msgbox "test:" & vbcrlf & Day(now) & "/" & Month(now) & "/" & Year(now) & " " & Hour(time) & ":" & Minute(time) & vbcrlf & A & " : " & B ,vbok
'**************************************************************************************************************************************************************************************************************
Function RetournAddressMail(V)
'Retourne une adresse Mail Valide s'il s'agit d'une mailing liste.
Dim T
RetournAddressMail=""
T= split(V & ";",";")
for I=0 to Ubound(T)
if trim("" & T(I))<>"" then
if instr(1, trim("" & t(I)) ,"@")=0 then T(I)= trim("" & T(I)) & "@ca-silca.fr"
RetournAddressMail =RetournAddressMail & trim("" & T(I)) & ";"
end if
next
for I=0 to Ubound(ConvertMail)
T= split(ConvertMail(I),";")
RetournAddressMail=Replace(RetournAddressMail, T(0), T(1), 1, -1)
next
RetournAddressMail=RetournAddressMail
' RetournAddressMail=LCase(RetournAddressMail)
end function
Public function MailEnvoi(Serveur, Identify , SSL, User, PassWord, Port, Delay, Expediteur, Dest, DestEnCopy,DestEnCah, Objet, Body, Pj)
on error resume next
MailEnvoi=true
dim Log
dim I
log= "Mail en date : " & Day(now) & "/" & Month(now) & "/" & Year(now) & " " & Hour(time) & ":" & Minute(time) & vbcrlf
log=log & "Serveur : " & Serveur & vbcrlf
log=log & "Identify : " & Identify & vbcrlf
log=log & "SSL : " & SSL & vbcrlf
log=log & "User : " & User & vbcrlf
log=log & "Port : " & Port & vbcrlf
log=log & "Delay : " & Delay & vbcrlf
log=log & "Expediteur : " & Expediteur & vbcrlf
log=log & "Destinataire : " & Dest & vbcrlf
log=log & "Destinataire en copie : " & DestEnCopy & vbcrlf
log=log & "Destinataire en copie caché : " & DestEnCah & vbcrlf
log=log & "Objet : " & Objet & vbcrlf
log=log & "Body : " & Body & vbcrlf
log=log & "Pièce jointe : " & Pj & vbcrlf
log=log & "----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------" & vbcrlf
FichierLog log
' sub pour envoyer les mails
Dim msg
Dim Conf
Dim Config
Dim ess
Dim splitPj
Dim IsplitPj
dim schema
const cdoBasic=1
dim Erreur
dim Sql
Set msg = CreateObject("CDO.Message") 'pour la configuration du message
Set Conf = CreateObject("CDO.Configuration") ' pour la configuration de l'envoi
Dim strHTML
Set Config = Conf.Fields
' Configuration des parametres d'envoi
'(SMTP - Identification - SSL - Password - Nom Utilisateur - Adresse messagerie)
schema = "http://schemas.microsoft.com/cdo/configuration/" 'smtpusessl
With Config
If Identify <> 0 Then
.Item(schema & "smtpusessl") =SSL
.Item(schema & "smtpusetls") = 1
.Item(schema & "smtpauthenticate") = Identify
.Item(schema & "sendusername") = User
.Item(schema & "sendpassword") = PassWord
end if
.Item(schema & "smtpserverport") = Port
.Item(schema & "sendusing") = 2
.Item(schema & "smtpserver") = Serveur
.Item(schema & "smtpconnectiontimeout") = Delay
.Item(schema & "enablessl") = 1
.Update
End With
'Configuration du message
'If E_mail.Sign.Value = Checked Then Convert ServeurFrm.SignTXT, ServeurFrm.Text1
With msg
Set .Configuration = Conf
.To = Dest
.cc = DestEnCopy
.bcc= Expediteur & DestEnCah
.FROM = Expediteur
.Subject = Objet
' .DSNOptions = cdoDSN
'
.htmlbody = Replace(Replace(Body, chr(13), "",1,-1), chr(10), "<br>",1,-1) '"<p align=""center""><font face=""Verdana"" size=""1"" color=""#9224FF""><b><br><font face=""Comic Sans MS"" size=""5"" color=""#FF0000""></b><i>" & body & "</i></font> " 'E_mail.ZThtml.Text
If Pj <> "" Then
splitPj = Split(Pj & ";", ";")
For IsplitPj = 0 To UBound(splitPj)
If Trim("" & splitPj(IsplitPj)) <> "" Then
.AddAttachment Trim("" & splitPj(IsplitPj))
if err<>0 then
Erreur = "ERR : " & vbcrlf & Err.Description & vbcrlf
FichierLog Erreur
MailEnvoi=false
Sql="INSERT INTO Job ( [To], Objet, Body )"
Sql=Sql & " values( '" & Expediteur & "', 'Automate_mail_RD : Erreur envoi Mail',' " & log & Erreur & "'); "
'FichierLog Sql
con.Execute Sql
exit function
end if
End If
Next
End If
.Send 'envoi du message
if err<>0 then
FichierLog "ERR : " & vbcrlf & Err.Description & vbcrlf
MailEnvoi=false
else
FichierLog "Fin du traitement avec succès." & vbcrlf
end if
on error goto 0
End With
FichierLog "**********************************************************************************************************************************************************************************" & vbcrlf & vbcrlf
' reinitialisation des variables
Set msg = Nothing
Set Conf = Nothing
Set Config = Nothing
End function
class Ado
Private Connexion
Public TYPEBASE
Public BASE
Public Server
Public Fichier
Public User
Public PassWord
Private Function GenereCSTRING()
'1 - ODBC
'2 - ORACLE
'3 - ACCESS 2012
'4 - ACCESS 2000
'5 - ACCESS 97
'6 - SQL SERVER
'7 - SQL SERVER 2005 Express
If Trim("" & Fichier) = "" Then Fichier = BASE
Select Case TYPEBASE
Case 1
GenereCSTRING = "Provider=MSDASQL.1;Password=" & PassWord & ";Persist Security Info=True;User ID=" & User & ";Data Source=" & BASE
Case 2
GenereCSTRING = "Provider=OraOLEDB.Oracle.1;Password=" & PassWord & ";Persist Security Info=True;User ID=" & User & ";Data Source=" & BASE
Case 3
GenereCSTRING = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Fichier & ";"
Case 4
GenereCSTRING = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Fichier & ";Persist Security Info=False"
Case 5
GenereCSTRING = "Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=" & Fichier
Case 6
GenereCSTRING = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Fichier
' GenereCSTRING = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Fichier & ";user=" & User & ";Passwors=" & PassWord
Case 7
GenereCSTRING = "Provider=SQLOLEDB.1;Password=" & PassWord & ";Persist Security Info=True;User ID=" & User & ";Initial Catalog=" & BASE & ";Data Source=" & Server
Case 8
GenereCSTRING = "Provider=SQLNCLI.1;Password=" & PassWord & ";Persist Security Info=True;User ID=" & User & ";Initial Catalog=" & BASE & ";Data Source=" & Server
Case Else
GenereCSTRING = "PAS ASSEZ DE PARAMETRES RENSEIGNES !!!"
End Select
'msgbox GenereCSTRING
'Response.End
End Function
Public Function OpenConnetion()
'Dim Fso As New Scripting.FileSystemObject
OpenConnetion = False
On Error Resume Next
Dim ConnecString
Dim NbErr
Set Connexion = CreateObject("ADODB.Connection")
Connexion.Open GenereCSTRING
'ConnecString
If Err = 0 Then
OpenConnetion = True
Connexion.CommandTimeout = 14400
Else
'MsgBox Err.Description
End If
' Debug.Print Err.Description
Err.Clear
On Error GoTo 0
End Function
Public Function CloseConnection()
CloseConnection = False
On Error Resume Next
Connexion.Close
Set Connexion = Nothing
If Err = 0 Then
CloseConnection = True
End If
Err.Clear
On Error GoTo 0
End Function
Public Function OpenRecordSet(Sql )
On Error Resume Next
Dim Rs
Dim NbErr
Err.Clear
If Connexion.State = 0 Then
OpenConnetion
End If
'Debug.Print Sql 'Replace(Sql, "%", "*")
Set OpenRecordSet = CreateObject("ADODB.Recordset")
' OpenRecordSet.LockType = adLockOptimistic
'MsgBox adLockOptimistic & vbcrlf & Err.Description
OpenRecordSet.Open Sql, Connexion, 1,3
If Err Then
' MsgBox Err.Description
NbErr = NbErr + 1
If NbErr < 11 Then
Set OpenRecordSet = Nothing
End If
End If
Err.Clear
End Function
Public Function RetournConnection()
Set RetournConnection = Connexion
End Function
Public Function OpenRecordSetParametre(Sql , Param)
Dim Commande
Dim Params
Set Commande = CreateObject("ADODB.Command")
Dim MyParameter
Set MyParameter = CreateObject("ADODB.Parameter")
Set Commande.ActiveConnection = Connexion
Commande.CommandText = "select Requête2.* from Requête2;"
Commande.CommandType = adCmdText
Set MyParameter = Commande.CreateParameter("[NumJob]", adNumeric)
MyParameter.Value = 10
Commande.Parameters.Append MyParameter
'aa.Parameters.Append("MyRef") = "243410M660"
Set Rs2 = Commande.Execute
End Function
Public Function CloseRecordSet(Rs)
On Error Resume Next
Rs.Close
Set CloseRecordSet = Nothing
End Function
Public Function Execute(Sql )
Execute = False
On Error Resume Next
Dim NbErr
Reprise:
If Connexion.State = 0 Then
OpenConnetion
End If
Debug.Print Sql
Connexion.Execute Sql
If Err = 0 Then
Execute = True
' Else
' MsgBox Err.Description
' Err.Clear
' NbErr = NbErr + 1
' If NbErr < 11 Then
'
' GoTo Reprise
' End If
Else
' MsgBox Err.Description
End If
Err.Clear
End Function
end class
Function AppendTxt(sFile, sText)
Dim Fso, NewFichier
Set Fso = CreateObject("Scripting.FileSystemObject")
Set NewFichier = Fso.OpenTextFile(sFile, 8)
NewFichier.Write sText
NewFichier.Close
Set NewFichier = Nothing
Set Fso = Nothing
End Function
Sub test()
FichierLog "TXT"
End Sub
Public Sub FichierLog(TXT)
Dim FichierLog, Fso
FichierLog = "\\Automate_mail_RD\LOG\Mail_" & Year(now) & "-" & Month(now) & "-" & Day(now) & ".LOG"
''CreerPath FichierLog
Set Fso = CreateObject("Scripting.FileSystemObject")
If Fso.FileExists(FichierLog) = False Then EnteteFichier FichierLog
AppendTxt FichierLog, TXT
Set Fso = Nothing
End Sub
Sub EnteteFichier(Fichier)
Dim TXT, Fso, NewFichier
TXT = "***********************************************************************************************************************************************************************************"
TXT = TXT & vbCrLf
TXT = TXT & " Fichier de LOG"
TXT = TXT & vbCrLf
TXT = TXT & " Date de création: " & Day(now) & "/" & Month(now) & "/" & Year(now) & " " & Hour(time) & ":" & Minute(time) & vbcrlf
TXT = TXT & vbCrLf
TXT = TXT & " " & Fichier
TXT = TXT & vbCrLf
TXT = TXT & "***********************************************************************************************************************************************************************************"
TXT = TXT & vbCrLf
TXT = TXT & vbCrLf
WScript.Echo Fichier
Set Fso = CreateObject("Scripting.FileSystemObject")
Set NewFichier = Fso.OpenTextFile(Fichier, 2, True)
NewFichier.Write TXT
NewFichier.Close
Set NewFichier = Nothing
Set Fso = Nothing
End Sub
Sub rrr()
OpenTextFile
End Sub |