Anyone have any recommendations?
I need to read e-mails, parse attachments. That's about it. Looks like System.Net.Mail focuses on SMTP-related functions.
I'd much rather get a tip from a Niner than take the word of the "testimonials" on some component website.
Thanks!
-
-
When I needed POP3, I went through several projects, but found Indy Project to be the best - http://www.indyproject.org/ It's not very easy to work with it, but it certainly gets the job done
If you need any help with indyproject+attachment parsing,
don't hesitate to ask, cause I did just that in a project. Regards, Tadej edit: and it's free!
edit2: sorry for the bad formatting, breaks apparently don't work in opera :/ -
I'd really appreciate some example code, showing how you did it.
You can either post here or send via e-mail. Thanks! -
Here's some code. It's in VB.net, but you'll probably be able to read it anyway

MailItem class is a custom class, which is only used for storing the info on the msg.
Dim pop3 As New Indy.Sockets.POP3
pop3.Username = "username"
pop3.Password = "password"
pop3.Host = "hostname"
pop3.Connect()
Dim nrOfMsgs As Integer = pop3.CheckMessages
For i As Integer = 1 To nrOfMsgs
Dim msg As New Indy.Sockets.Message
pop3.Retrieve(i, msg)
Dim mailItem As New MailItem(msg.Subject, msg.From.Address, CType(allowedEmails.Item(index), AllowedEmail).Delimiter, CType(allowedEmails.Item(index), AllowedEmail).User_id)
For x As Integer = 1 To msg.MessageParts.Count - 1
Dim att As Indy.Sockets.Attachment
Dim isAtt As Boolean = False
Try
att = CType(msg.MessageParts.Items(x), Indy.Sockets.Attachment)
isAtt = True
Catch
End Try
Dim ct As String = msg.MessageParts.Items(x).ContentType
If isAtt Then 'ct = "application/octet-stream" Then
Dim stream As New Indy.Sockets.TIdNetMemoryStream
att.SaveToStream(stream)
Dim data As String
If ct = "text/plain" Then
Dim tmp As String = System.Text.ASCIIEncoding.Default.GetString(stream.Memory).Trim.Trim(Chr(0))
data = System.Text.UTF8Encoding.UTF8.GetString(System.Text.UTF8Encoding.Convert(System.Text.Encoding.GetEncoding("Windows-1250"), System.Text.UTF8Encoding.UTF8, Convert.FromBase64String(tmp)))
Else
data = System.Text.UTF8Encoding.UTF8.GetString(System.Text.UTF8Encoding.Convert(System.Text.Encoding.GetEncoding("Windows-1250"), System.Text.UTF8Encoding.UTF8, stream.Memory))
End If
Next
mailItems.Add(mailItem)
pop3.Delete(i)
End If
Next
pop3.Disconnect()
pop3.Dispose()Regards,
Tadej -
I like this one.
http://www.developerfusion.com/show/4071/1/
It's quite extendable too. -
Hi, your code was very helpful. I am doing a similar project where i need to read incoming emails from a pop3 server and download attachments to a local file. I am using Indy sockets to do this.But, i have a couple of questions. 1. If the email message is a plain text file, i am able to read the body of the message using msg.body.text. But, if the content type is multipart/mixed, how would i read the body of the message. 2. Is it possible to mark the messages as read or something or download only unread messages each time? I greatly appreciate your inputs. Thanks
-
1. Well, you could use the MessageParts property of Indy.Sockets.Message .. you just iterate through it and determine the course of action based on the ContentType. 2. There is the Delete method of Indy.Sockets.POP3. You could use that. Sorry for the late response
Regards, Tadej ps: I know this text will be horribly formatted, cause I'm posting from Opera -
How do you recommend interating through the MessageParts property to obtain the message body?
-
Try this:
<code>
Dim pop3 As New Indy.Sockets.POP3
pop3.Username = "username"
pop3.Password = "password"
pop3.Host = "hostname"
pop3.Connect()
Dim nrOfMsgs As Integer = pop3.CheckMessages
For i As Integer = 1 To nrOfMsgs
Dim msg As New Indy.Sockets.Message
pop3.Retrieve(i, msg)
For x As Integer = 1 To msg.MessageParts.Count - 1
Dim ct As String = msg.MessageParts.Items(x).ContentType
Next
Next
pop3.Disconnect()
</code>
With this, you should get the content type of all parts of all messages in ct ..
Now, the body will most probably have the ct equal (or similar) to "text/plain" or "text/html" or sth. Do a test
Hope this helps.
Regards,
Tadej -
TadejK wrote:Try this:
Now, the body will most probably have the ct equal (or similar) to "text/plain" or "text/html" or sth. Do a test
Hope this helps.
Regards,
Tadej
Tadej -
For us "programatically challenged" people, ok I'm just lost and frustrated being very new to indy.sockets but loving what its done for me so far. Can you please elaborate further on accessing the actual "body" content?
Your example was invaluable to me moving forward with using indy.sockets, but the one thing that I can't seem to find a short and simple example of is how you acutally get the body information into a string that can be displayed or saved to a database in a form that "humans" can read.
The ct that I'm showing for the email messages is text/html and it is in stored in a mutipart MIME format.
Thank you for your time and assistance!
-
<code>
Dim pop3 As New Indy.Sockets.POP3
pop3.Username = "username"
pop3.Password = "password"
pop3.Host = "host"
pop3.Connect()
Dim nrOfMsgs As Integer = pop3.CheckMessages
For i As Integer = 1 To nrOfMsgs
Dim msg As New Indy.Sockets.Message
pop3.Retrieve(i, msg)
Dim from As String = msg.From.Address
Dim subject As String = msg.Subject
Dim body As String = ""
If (msg.MessageParts.Count = 0) Then
body = msg.Body.GetText()
Else
For x As Integer = 1 To msg.MessageParts.Count - 1
If msg.MessageParts(x).ContentType = "text/html" Then
body = CType(msg.MessageParts.Items(x), Indy.Sockets.Text).Body.GetText()
End If
Next
End If
MsgBox("From: " & from & vbCrLf & "Subject: " & subject & vbCrLf & "Body: " & body)
Next
pop3.Disconnect()
</code>
I think this is it
So, what does this do .. it gets all the messages, it stores from and subject into their respective variables, and it also stores the body in the body variable - if the message is MIME multipart message, then it stores the part which has the ContentType set to text/html, otherwise it stores the Body value.
This example also leaves the messages on the server.
Don't hesitate to ask if I wasn't clear enough
Regards,
Tadej -
TadejK wrote:
Don't hesitate to ask if I wasn't clear enough
Regards,
Tadej
This was kind of helpful but next time do you think you could use my variables and put it into my codebehind document for me?
I'M KIDDING!
This was EXACTLY the example I was looking for, you are AWESOME! I had decided that I guess I needed to go with a commercial product to do what I needed to since I wasn't getting anywhere with this indy stuff. But you've saved me from that fate!
MAHALO!
-R.
-
Happy to help

-
Hi TadejK,
Have you tried using Indy with gmail account.
I tried and I could not connect. Everytime I tried I got a "Connection closed Gracefully error".
Gmail has port as 995 and pop3 as pop.gmail.com.
Gmail only supports secure connection. So that could be the reason? Have you tried connecting to SSL pop3 server?
Bhavesh -
No, I haven't tried, but I will try as soon as I have a few minutes to spare

And, of course, I'll post my findings here
Regards,
Tadej -
Thanks for your quick response.
I am trying with UseTLS but it gives an exception "SSL IOHandler is required for this setting" and I don't understand what this means.
Bhavesh -
Well, it seems that the first step *would* be to set UseTLS ..
But the problem is, that there is, apparently, no OpenSSL IOHandler for .NET.
These IOHandlers have become pluggable in Indy 10, so the solution would be to find one for SSL and .NET .. but, as mentioned before, there aren't any available
The closest I was able to come was by finding this - http://cc.borland.com/Item.aspx?id=21906
I believe this is an IOHandler, and it also says it supports .NET .. so what's the problem? Apparently, it's not a compiled .NET assembly, but just source code for a component
If you can find someone to compile this, then this just might work
(but you will need OpenSSL libraries for Indy .. try here - http://indy.fulgan.com/SSL/)
I'm still going to try and find a (working) solution to this problem, so stay tuned
Regards,
Tadej -
Hello,
I succesfully receive Email in HTML or plain/text, with attachment or not but I have a problem with special characters.
"é" "à" ... appear "i" or "'" when I received the mail.
How to solve this problem ?
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.