Partendo da un esempio su aspitalia e dopo essermi studiato il protocollo Post Office ho iniziato a continuare il codice dell'autore.. per ora sono arrivato qui.
codice:
<%@ Page Language="vb" %>
<%@ import Namespace="System.Net" %>
<%@ import Namespace="System.Net.Sockets" %>
<script runat="server">
'Starts from www.aspitalia.com
'Alessio Marziali
Dim tcpC as New TcpClient()
Sub Page_load()
if ispostback() then
lblMessaggi.Text = ""
ReadMail(host.selecteditem.value, utente.text, pwd.text)
end if
End Sub
' Manda il comando e restituisce la risposta
Function SendCommand(byRef NetStream as NetworkStream, byVal sToSend as String)
Dim bData() as Byte = Encoding.ASCII.GetBytes(sToSend.ToCharArray)
NetStream.Write(bData,0,bData.Length())
Return GetResponse(NetStream)
End Function
' Controlla se c'è una risposta e la restituisce
Function GetResponse(byRef NetStream as NetworkStream)
Dim bytes(tcpC.ReceiveBufferSize) As Byte
NetStream.Read(bytes, 0, bytes.length)
'Restituisce i dati ricevuti
Dim ReturnData As String = Encoding.ASCII.GetString(bytes)
Return ReturnData
End Function
Function ReadMail(host as string, user as string, pass as string)
Dim NetStream as NetworkStream, MyResponse as string
' apre una connessione con il server di posta sulla porta 110
try
tcpC.Connect(host,110)
catch MyEx as Exception
'in caso di errore restituisce un messaggio
lblMessaggi.Text += "Errore nella connessione all'host: " & host & " (porta 110)
" & _
"L'errore riportato è: " & MyEx.message & "
Controlla e riprova
"
end try
' Recupera la risposta
try
NetStream = tcpC.GetStream()
MyResponse = GetResponse(netstream)
catch MyEx as Exception
lblMessaggi.Text += "Si è verificato un errore!"
end try
'Invia il nome dell'utente (account sul server)
MyResponse = SendCommand(netstream,"user " & user & vbCrLF)
'Invia la password
MyResponse = SendCommand(netstream,"pass " & pass & vbCrLf)
'Controlla se il collegamento è andato a buon fine
if left(MyResponse,4)="-ERR" then
lblMessaggi.Text += "Errore nel collegamento dell'utente; controlla i dati e riprova
"
lblMessaggi.Text += MyResponse & "
"
MyResponse=SendCommand(netstream,"QUIT" & vbCrLF)
tcpC.close
else
'Indica che il collegamento ha avuto successo
lblMessaggi.Text += "Utente correttamente collegato
"
'Richiede le statistiche dell'intera casella
MyResponse=SendCommand(netstream,"stat" & vbCrLf)
dim tmpArray() as string
'nel primo elemento (indice 0) c'è '+OK' nel secondo il numero dei messaggi, nel terzo la dimensione dei messaggi in bytes
tmpArray = split(MyResponse," ")
dim thisMess as integer
'quindi qui trovo il numero dei messaggi
dim NumMess as string = tmpArray(1)
if cint(NumMess) > 0 then 'controllo se ci sono messaggi
'Scrivo il numero dei messaggi
lblMessaggi.Text += "La tua mailbox contiene " & NumMess & " messaggi per un totale di " & tmpArray(2) & " bytes
"
'per ogni messaggio della casella recupero la dimensione
for thisMess = 1 to cint(numMess)
MyResponse = SendCommand(netstream,"list " & thisMess & vbCrLf)
tmpArray = split(MyResponse," ")
MyResponse = "Mail n° " & thisMess & ": dimensione " & tmpArray(2) & " bytes"
lblMessaggi.Text += MyResponse & "
"
next
else
lblMessaggi.Text += "La casella è vuota"
end if
end if
' chiudo la connessione con il server
MyResponse=SendCommand(netstream,"QUIT" & vbCrLF)
' chiudo la connessione TCP
tcpC.close
End Function
</script>
<html>
<head>
<title>Statistiche casella mail</title>
<style type="text/css">
<!--
.style1 {
font-family: Arial, Helvetica, sans-serif;
font-weight: normal;
font-size: 12px;
}
.input {
font-family: Arial, Helvetica, sans-serif;
font-weight: normal;
color: #000000;
background-color: #D7F5FF;
}
.emails {
font-family: "Courier New", Courier, mono;
font-size: 12px;
color: #666666;
}
-->
</style>
</head>
<body>
[img]mailcheck.gif[/img]
<form id="calc" method="post" runat="server">
<table width="60%" border="1" align="center" cellpadding="0" cellspacing="0" bordercolorlight="#006699">
<tr>
<td class="style1">Servizio Posta Elettronica</td>
<td><asp:DropDownList Width="100%" ID="host" CssClass="input" runat="server">
<asp:listitem Value="popmail.email.it">Email.it</asp:listitem>
<asp:listitem Value="popmail.libero.it">Libero</asp:listitem>
<asp:listitem value="pop.katamail.com">Katamail</asp:listitem>
<asp:listitem Value="mail.tin.it">Tin.it</asp:listitem>
<asp:listitem Value="popmail.virgilio.it">Virgilio</asp:listitem>
</asp:DropDownList></td>
</tr>
<tr>
<td class="style1">Username</td>
<td><asp:TextBox Width="100%" id="utente" CssClass="input" runat="server" text="marziali.alessio"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style1">Password di Accesso</td>
<td><asp:TextBox Width="100%" id="pwd" CssClass="input" runat="server" TextMode="Password"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2" class="style1"> <asp:Button Width="100%" id="Button1" runat="server" Text="Controlla Casella"></asp:Button></td>
</td>
</tr>
<tr>
<td colspan="2"><asp:Label Width="100%" CssClass="emails" id="lblMessaggi" runat="server"></asp:Label>
</td>
</tr>
</table>
</form>
</body>
</html>
ciao e fanne buon uso