Bonjour,
Dans le cadre d'un projet pour ma société, je dois créer un petit exécutable qui récupère des e-mails sur un serveur pop, les écrire dans différents fichiers puis rediriger les fichiers dans certains dossiers. Jusque là, ça paraît plutôt simple.
Afin de réaliser ce projet, j'ai effectué des recherches sur Google et il m'a semblé que l'utilisation d'un streamreader pour l'envoi, en binaire, des commandes POP était le plus simple à mettre en oeuvre.
L'aboutissement de mes recherches donne ce petit bout de code :
Ce code fonctionne, c'est à dire qu'il me récupère bien le mail de numéro NumMessage mais par contre il est trés long, trés trés long (parfois plus de 2 mins).
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 '//////////////////// Déclarations qui vont bien 'Initialisation du flux Strm = Sckt.GetStream 'Initialisation du StreamReader StrmRdr = New StreamReader(Strm) Dim line As String = StrmRdr.ReadLine() If line.Substring(0, 3) = "+OK" Then CommandeUser = "USER " & strLogin & vbCr & vbLf 'Envoi du login au serveur bCommande = System.Text.Encoding.UTF8.GetBytes(CommandeUser) Strm.Write(bCommande, 0, bCommande.Length) line = StrmRdr.ReadLine If line.Substring(0, 3) = "+OK" Then CommandePass = "PASS " & strPass & vbCr & vbLf 'Envoi du mdp au serveur bCommande = System.Text.Encoding.UTF8.GetBytes(CommandePass) Strm.Write(bCommande, 0, bCommande.Length) line = StrmRdr.ReadLine If line.Substring(0, 3) = "+OK" Then CommandeList = "RETR " & NumMessage & vbCr & vbLf 'Envoi de la commande de récupération du mail bCommande = System.Text.Encoding.UTF8.GetBytes(CommandeList) Strm.Write(bCommande, 0, bCommande.Length) 'Création d'un fichier texte Dim SW As New StreamWriter(CheminBAL & "\cmessage" & NumMessage & ".txt") line = StrmRdr.ReadLine txtDEBUG.Text = line If line <> Nothing Then If line.Substring(0, 4) = "-ERR" Then SW.WriteLine(line) Else 'Parcourt et enregistrement de l'email dans le fichier '/////////////////////////////////////////// Lorsqu'il arrive à la dernière ligne d'un email, la lecture de la propriété EndOfStream est trés trés longue. Do Until StrmRdr.EndOfStream SW.WriteLine(line) line = StrmRdr.ReadLine Loop End If End If SW.Dispose() txtPatience.Text = "Reception terminée" End If End If End If StrmRdr.Dispose() Strm.Dispose() Sckt.Close()
En debug, je me suis rendu compte que c'était la lecture de la propriété EndOfStream qui, lorsqu'il arrive à la dernière ligne du mail, est trés trés longue.
Quelqu'un aurait une explication, une piste pour une méthode plus rapide ?
Merci d'avance.
Partager