Few days back I got an R&D from my friend, it is like, in Blogger, we can send mails to a specific mail address and we a can Post. He want the same implementation in asp.net, but the mail Id will be static. So I thoght of reading POP3 mailbox using .Net. And I didn’t done the same in ASP.Net, but I created a POC in VB.Net, which will help you to check you POP3 account.
Dim _tcpClient As New TcpClient
Dim _networkStream As NetworkStream
Dim _Msg As String
With _tcpClient
.Connect(Me.txtServerIp.Text, Integer.Parse(Me.txtPortNum.Text))
_networkStream = .GetStream
Dim sw As New StreamWriter(_networkStream)
Dim sr As New StreamReader(_networkStream)
If Not CheckError(sr.ReadLine()) Then
sw.WriteLine(String.Format("USER {0}", Me.txtUsername.Text))
sw.Flush()
End If
If Not CheckError(sr.ReadLine()) Then
sw.WriteLine(String.Format("PASS {0}", Me.txtPassword.Text))
sw.Flush()
End If
If Not CheckError(sr.ReadLine()) Then
sw.WriteLine("STAT ")
sw.Flush()
End If
_Msg = sr.ReadLine
Dim MsgCount As String = _Msg.Split(New String() {" "}, _
StringSplitOptions.RemoveEmptyEntries)(1)
If Integer.Parse(Me.lblMsgCount.Text) < Integer.Parse(MsgCount) Then
Me.lblMsgCount.Text = MsgCount
End If
sw.WriteLine("Quit ")
sw.Flush()
sw.Close()
sr.Close()
_networkStream.Close()
_tcpClient.Close()
End With
And the check error method will check for the ERR status and display a generic error messgae.
Private Function CheckError(ByVal Msg As String) As Boolean
Dim result As Boolean = False
If Msg.IndexOf("+OK") >= -1 AndAlso Msg.IndexOf("-ERR") >= 0 Then
MessageBox.Show(My.Resources.UI.Msg_GenericError, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning)
result = True
Me.tmr.Enabled = False
End If
Return result
End Function
I am planning to write a simple utility using this code, and to host it on codeplex, which will give the count of unread mails in your mailbox.