Email Socket Question

G

googlegroups

I am learning vb.net and trying to create a simple program to watch
port 25 and receive email. I do not need any advanced features. I
just need to connect, watch and grab the emails as they come. This
seems like it should be a pretty simple but I can not seem to find any
examples in vb.net 8. Any help is appreciated. TIA
 
S

Stephany Young

Well you may not need any advanced features, but you, at least, need the
basic features of an SMTP server.

I get the impression that you think an 'email' arriving at an SMTP is just a
stream of data, but it is not.

An SMTP server and a client that is sending it an 'email' carry out a
complex conversation.

As you are learning VB.NET I suggest that you avoid such a project until you
have read and are able to implement all of the core functionality as set out
in the SMTP and related RFC's.
 
I

iMaiden

I am hoping to find someone who has already written the "complex
conversation" code and to study it. Any help is appreciated.
 
S

Stephany Young

Those that have, sell their products for (in some cases) several thousand
dollars. They generally do not make the source code available.
 
I

iMaiden

I'm not here for a discussion and if you don't know how to do what I am
trying to do that is fine but perhaps you do or someone else in this
group does so here goes. How would I use sockets to

1) open port 25
2) receive data from this port
3) send data through this port
4) close the port.
 
G

Guest

I am hoping to find someone who has already written the "complex
conversation" code and to study it. Any help is appreciated.

Start off with something simple - like a hello world socket code.

A SMTP server is not that trival :)
 
T

Tom Shelton

I am learning vb.net and trying to create a simple program to watch
port 25 and receive email. I do not need any advanced features. I
just need to connect, watch and grab the emails as they come. This
seems like it should be a pretty simple but I can not seem to find any
examples in vb.net 8. Any help is appreciated. TIA

Ok... First things first - you need to understand how to write a server in
VB.NET. For an SMTP server, I think the first thing you should look at is how
to write a basic windows service. You should be able to find examples of that
in the documentation - Look at System.ServiceProcess.ServiceBase.

Once you have that down, you should probably read and study the documentation
on implementing an ayncronous server socket. I encourage you to use the
asyncronous model, even though it is slightly more complicated then the normal
syncronous socket model. It will provide you with much better overall
performance because of the way the async sockets are implemented. There is a
example of a simple server in the documentation. If you don't have the docs,
start here:

http://msdn2.microsoft.com/en-us/library/4as0wz7t.aspx

Once you understand the basic concepts, then you should go to an rfc database
(I usually go to http://www.rfc-editor.org) and look up the rfc's related to
smtp. You should probably start with rfc2821:

ftp://ftp.rfc-editor.org/in-notes/rfc2821.txt

Then you will probably want to expand from there, like the rfc2554 which
covers service extensions for authentication, etc.

Once you begin writing code and playing with it - post back if you have any
questions. It's much easier to help people with specific issues then the
general "how do i write an smtp server" :)

HTH
 
B

Branco Medeiros

I am learning vb.net and trying to create a simple program to watch
port 25 and receive email. I do not need any advanced features. I
just need to connect, watch and grab the emails as they come. This
seems like it should be a pretty simple but I can not seem to find any
examples in vb.net 8. Any help is appreciated. TIA

TCPListener is your friend (It's in System.Net.Sockets).

HTH.

Regards,

Branco.
 
A

al jones

iMaiden (and I'm not sure you are one) if you're not here for discussion
(of programming topics), then you're really in the wrong place. I'm more
new than not, and I've gotten a lot of help from the people here -
sometimes they surprise me with code, most of the time I get suggestions
about where to go look. That's better for me since I have the opportunity
to see how to use the tools I have at hand rather than having someone hand
it to me on a silver platter.

If you need it so badly that you can't be polite (and 'I'm not here for
discussion...' is *not* polite) then maybe you'd better take Stephanies
inferred suggestion and go buy one ....

.... and, no, I don't know how to do what you want either; but I do know
that your approach isn't the way *I'd* handle it.

//al
 
C

Cor Ligthert [MVP]

Al,

A big mistake in your message, you will be punished for many years.

It is Stephany, not Stephanie.

:))))

Cor
 
S

Stephany Young

Thank you Cor. I was wondering how long it would take someone to spot that.

But really, I'm a big girl and I can spit my dummy quite well without any
help.

;)
 
A

al jones

OOps, < chuckle > sorry, Stephany - but at least I didn't get called on the
carpet for the message .... :)

//al
 
I

iMaiden

Thanks Branco

Dim Listener As New TcpListener("25")
Listening = True
Listener.Start()
Do Until Listening = False
Dim sb As New SocketAndBuffer()
sb.Socket = Listener.AcceptSocket()
sb.Socket.BeginReceive(sb.Buffer, 0, sb.Buffer.Length,
SocketFlags.None, AddressOf ReceiveCallBack, sb)
Loop
 
I

iMaiden

Completed simple server to receive mail. Thanks for you help.

Sub ListenToServer()
'Try

Dim LISTENING As Boolean
Dim localhostAddress As IPAddress =
ipAddress.Parse(ipAddress.ToString)
Dim port As Integer = 25 '' PORT ADDRESS
''''''''''' making socket tcpList ''''''''''''''''
Dim tcpList As New TcpListener(localhostAddress, port)
Try
tcpList.Start()
LISTENING = True

Do While LISTENING

Do While tcpList.Pending = False And LISTENING = True
' Yield the CPU for a while.
Thread.Sleep(10)
Loop
If Not LISTENING Then Exit Do

Dim tcpCli As TcpClient = tcpList.AcceptTcpClient()
Dim ns As NetworkStream = tcpCli.GetStream
Dim sr As New StreamReader(ns)

Dim returnedData As String = "220 smtp.sample.com SMTP
server ready"
Dim sw As New StreamWriter(ns)
sw.WriteLine(returnedData)
sw.Flush()
Dim receivedData As String = sr.ReadLine()
sw.Flush()


returnedData = "250 smtp.sample.com Hello
[71.105.78.90]"
sw.WriteLine(returnedData)
sw.Flush()
receivedData = sr.ReadLine()
receivedData = Trim(Replace(LCase(receivedData), "mail
from:", ""))

returnedData = "250 " + receivedData + "....Sender OK"
sw.WriteLine(returnedData)
sw.Flush()
receivedData = sr.ReadLine()


returnedData = "250 " +
Trim(Replace(LCase(receivedData), "rcpt to:", ""))
sw.WriteLine(returnedData)
sw.Flush()
receivedData = sr.ReadLine()


returnedData = "354 Start mail input; end with
<CRLF>.<CRLF>"
sw.WriteLine(returnedData)
sw.Flush()

Do While receivedData <> "."

receivedData = sr.ReadLine()
sw.Flush()
MsgBox(receivedData)

Loop
MsgBox("OVER")
returnedData = "250 Queued mail for delivery"
sw.WriteLine(returnedData)
sw.Flush()

sr.Close()
sw.Close()
ns.Close()
tcpCli.Close()
Loop
tcpList.Stop()
Catch ex As Exception
'error
LISTENING = False
End Try
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top