Hi,
I am new to this forum & asp.net.
Iam trying to write a web-based application to read my emails through webpage(pop account). I had done this in ASP using CDO session.
Now what i thought is we can do this much easily in ASP.NET(System.Web.mail), however this turned false. I tried system.net through which i could get the no of emails etc. But i cannot see or display the messages in the browser or can say cannot extract the body of the message.So can anybody help me on that.I do not want to use any commercial components.
THE CODE I AM USING GOT THROUGH SOME WEBSITE works well
Code:
Imports System
Imports System.Text
Imports System.Net
Public Class email
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private tcpC As New Sockets.TcpClient
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ReadMail("mail.calorisplanitia.com", "[email protected]", "password")
End Sub
' send command strings and return the response
Function SendCommand(ByRef netstream As System.Net.Sockets.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
' check if there is a response to get and return it
Function GetResponse(ByRef netstream As System.Net.Sockets.NetworkStream)
Dim bytes(tcpC.ReceiveBufferSize) As Byte
Dim ret As Integer = netstream.Read(bytes, 0, bytes.Length)
' Returns the data received
Dim returndata As String = Encoding.ASCII.GetString(bytes)
Return returndata
End Function
Function ReadMail(ByVal host As String, ByVal user As String, ByVal pass As String)
' initialise objects
Dim netstream As System.Net.Sockets.NetworkStream
Dim thisResponse As String
' open connection to server
Try
tcpC.Connect(host, 110)
Catch ex As Exception
Response.Write("Error connecting to host: " & ex.Message & " - Please check your details and try again")
Response.End()
End Try
' get response
netstream = tcpC.GetStream()
thisResponse = GetResponse(netstream)
' enter user name
thisResponse = SendCommand(netstream, "user " & user & vbCrLf)
' enter password
thisResponse = SendCommand(netstream, "pass " & pass & vbCrLf)
' check if logged in ok
If Not Left(thisResponse, 4) = "-ERR" Then
Response.Write("Logged in OK <BR>")
Else
Response.Write("Error logging in, check your user details and try again<BR>")
Response.Write("<P>" & thisResponse & "</p>")
Response.End()
End If
thisResponse = SendCommand(netstream, "stat" & vbCrLf)
Dim tmpArray() As String
tmpArray = Split(thisResponse, " ")
Dim thisMess As Integer
Dim numMess As String = tmpArray(1)
Response.Write("")
thisResponse = ""
If CInt(numMess) > 0 Then
Response.Write("Messages: " & numMess & "<br>")
For thisMess = 1 To CInt(numMess)
thisResponse += Replace(SendCommand(netstream, "top " & thisMess & " 10" & vbCrLf), vbCrLf, "<br>")
Next
Else
Response.Write("Messages: None" & "<br>")
End If
thisResponse += Replace(SendCommand(netstream, "stat" & vbCrLf), vbCrLf, "<br>")
tmpArray = Split(thisResponse, "+OK")
Response.Write(thisResponse & "<br>--------------------------------------------<br>")
Dim msg As Integer
For msg = 1 To tmpArray.Length - 1
Response.Write("<h3>#" & msg & "</h3><pre>" & tmpArray(msg) & "</pre>")
Next
' close connection
thisResponse = SendCommand(netstream, "QUIT" & vbCrLf)
tcpC.Close()
End Function
End Class