Certo che lo puoi fare ma in ASP.NET
Non dovrei postarlo qui ... ma datosi che ho faticato molto per capirne il funzionamento ... ti do la pappa pronta.
Ti do qui il sorgente. Inserisci i tuoi dati nell'area modificabile.
FindMail.aspx
codice:
<?xml version="1.0" encoding="iso-8859-1"?>
<%@ Page Language="vb" Debug="True" %>
<%@ import Namespace="System.Net" %>
<%@ import Namespace="System.Net.Sockets" %>
<script runat="server">
Dim tcpC as New TcpClient()
Sub Page_load()
' inizio area modificabile
POP3URL.Text = "pop3.mysite.it"
MailAddress.Text = "mymail@domail.com"
Password.Text = "mypassword"
' fine area modificabile
lblMessaggi.Text = 0
ReadMail(POP3URL.text, MailAddress.text, Password.text)
End Sub
' Ricerca specifica informazione dall'intestazione della mail
Function SeeInfo(FindValue,MyStr)
dim valore as string
dim posizione as integer
posizione=InStr(MyStr,FindValue)
if posizione>0 then
posizione=posizione+Len(FindValue)
Valore=Mid(MyStr,posizione)
end if
Return Valore
End Function
' 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)
lblMessaggi.Text = NumMess
if cint(NumMess) > 0 then 'controllo se ci sono messaggi
'Scrivo il numero dei messaggi
'lblMessaggi.Text += "Ci sono " & NumMess & " messaggi per un totale di " & tmpArray(2) & " bytes
"
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>
<body>
<asp:Label id="lblMessaggi" Visible="false" runat="server"></asp:Label>
<script language="JavaScript">
if (<%=lblMessaggi.Text%>>0) {
ConfCFG = alert("Ci sono <%=lblMessaggi.Text%> messaggi");
}
</script>
</body>
</html>