PDA

View Full Version : مشکل در جدا سازی Subject , Body در POP3 Class



m.yazdian
یک شنبه 12 آبان 1387, 08:43 صبح
من یه مشکلی دارم تو پروژه ام که البته این پروژه با vb.net نوشته شده نه delphi اونم اینه که من وقتی به سرور pop3 وصل میشو و بعد از ارسال نام کاربری و کلمه عبور و فرمان RERE 1 سرور در ج.اب متن نامه من را برای من برمب گرداند . (ولی مشکل اینجاست که من نمی دونم بخش های Body , Attachment , Subject , From , Date را چجوری می تونم خارج کنم ؟؟؟؟) همه این اطلاعات بصورت یه stream در یک خط از سرور دریافت میشه واگر من بخوام اونو نمایش بدم کلی اطلاعات دیگه هم باهاش نمایش داده میشه .

اگر ممکنه نحوه Parse کردن این stream رو برای من توضیح بدین.
من تو codeproject مقاله های زیادی دیدم فکر میکنم که با توجه به Mime Type ها باید این کار رو کرد ولی من موفق به انجام این کار نشدم.

ممنون میشم اگر کمکی کنید .





Public


Class GMail

Dim TCP As Net.Sockets.TcpClient

Dim POP3Stream As System.IO.Stream
Dim inStream As System.IO.StreamReader
Dim strDataIn, strNumMains(2) AsString
Dim intNoEmails AsInteger
PublicFunction Connection(ByVal strServer AsString, ByVal strUserName AsString, ByVal strPassword AsString) AsString
'connect to the pop3 server over port 110
Try
TCP =




New Net.Sockets.TcpClient

TCP.Connect(strServer, 110)

'create stream into the ip
POP3Stream = TCP.GetStream
inStream = New System.IO.StreamReader(POP3Stream)
'Make sure we get the ok back from the server
If WaitFor("+OK") Then SendData("USER " & strUserName)
'send the email down
If WaitFor("+OK") Then SendData("PASS " & strPassword)
WaitFor("+OK")
Catch ex As Exception
Return ex.Message
ExitFunction
EndTry
EndFunction
Function GetMailStat() AsInteger
'send the stat command and make sure it returns as expected
Try
SendData(




"STAT")

If WaitFor("+OK") = FalseThen

GetMailStat =




"0"

ExitFunction

Else
'split up the response. It should be +OK Num of emails size of emails
strNumMains = Split(strDataIn,




" ")

GetMailStat = strNumMains(1)

intNoEmails = strNumMains(1)
EndIf
Catch ex As Exception
GetMailStat = 0
intNoEmails = 0
EndTry
EndFunction
PublicFunction SendData(ByVal strCommand AsString)
Dim outBuff AsByte()
outBuff = ConvertStringToByteArray(strCommand & vbCrLf)
POP3Stream.Write(outBuff, 0, strCommand.Length + 2)
EndFunction






PublicFunction WaitFor(ByVal strTarget AsString) AsBoolean

strDataIn = inStream.ReadLine
If InStr(strDataIn, strTarget) Then
WaitFor =




True

Else

WaitFor =




False

EndIf

EndFunction
PublicSharedFunction ConvertStringToByteArray(ByVal stringToConvert AsString) AsByte()
Return (New System.Text.ASCIIEncoding).GetBytes(stringToConver t)
EndFunction
PublicFunction GetMailMessage(ByVal intNum AsInteger) AsString
Dim strTemp AsString
Dim strEmailMess AsString = ""
Try
'send the command to the server to return that email back. Command is RETR and the email no ie RETR 1
SendData(




"RETR " & Str(intNum))

'make sure we get a proper response back

If WaitFor("+OK") = FalseThen
GetMailMessage = (




"No Email was Retrived")

ExitFunction

EndIf
'Should be ok at this point to read in the tcp stream. We read it in until the end of the email
'whitch is signified by a line containing only a fullpoint(chr46)
strTemp = inStream.ReadLine
While (strTemp <> ".")
strEmailMess = strEmailMess & strTemp & vbCrLf
strTemp = inStream.ReadLine
EndWhile
GetMailMessage = strEmailMess
Catch ex As Exception
'just return an error message if we fell over
GetMailMessage =




"No Email was Retrived"

EndTry

EndFunction
PublicFunction ParseHeader(ByVal strHeader AsString, ByVal strType AsString) AsString
Dim intLenToStart AsInteger
Dim intLenToLineEnd AsInteger
Dim strTmp AsString
intLenToStart = (InStr(strHeader, strType) - 1)
intLenToLineEnd = InStr(Mid(strHeader, intLenToStart), vbCrLf)
strTmp = strHeader.Substring(intLenToStart, intLenToLineEnd)
ParseHeader = strTmp
EndFunction
PublicFunction ParseAttachment(ByVal strBody AsString) AsString
Dim str, sTemp AsString
Dim intStart, intEnd AsInteger
Dim Buffer(1024) AsByte
If InStr(strBody, "Content-Disposition: attachment;") <> 0 Then
intStart = (InStr(strBody,




"Content-Disposition: attachment;") + 51)

intEnd = InStr(strBody, vbCrLf & "0")

str = strBody.Substring(intStart, intEnd)
str = Replace(str, vbCrLf, "")
EndIf
Return str
EndFunction
PublicFunction ParseBody(ByVal strBody AsString) AsString
Dim str AsString
Dim intLenStart, intLenEnd AsInteger
Dim Buffer(1024) AsByte
Try
If InStr(strBody, "Content-Transfer-Encoding: base64") = 0 Then
intLenStart = (InStr(strBody,




"Content-Type: text/html") + 100)

intLenEnd = InStr(Mid(strBody, intLenStart), vbCrLf & "------=_Part")

str = strBody.Substring(intLenStart, intLenEnd)
Else
intLenStart = (InStr(strBody,




"Content-Type: text/html") + 102)

intLenEnd = InStr(Mid(strBody, intLenStart), vbCrLf & "------=_Part")

str = strBody.Substring(intLenStart, intLenEnd)
Buffer = Convert.FromBase64String(str)
str = Encoding.UTF8.GetString(Buffer)
EndIf
Catch ex As Exception
str = "Cannot read the message"
EndTry
Return str
EndFunction
PublicSub Quit()
Dim message AsString
message =




"Quit" + vbCrLf

SendData(message)

TCP.Close()
EndSub
EndClass

mpmsoft
یک شنبه 12 آبان 1387, 10:39 صبح
دوست عزیز سوالتو تو بخش VB.net بپرس