Hi everyone,
I hope someone is going to be able to help me.
I need to make a small application using VB6 (enterprise Edition) to make a small email program.
On the form are the following:
textboxes x
1. txtmailfrom
2. txtmailto
3. txtmessage
4. txtport
5. txtserver
Command buttons x2
1. Exit Program (done and coded this one)
2. cmdsend
The way I want it to function is thus;
the user enters details in the text boxes and presses send the code behind cmdsend then prosesses and sends the email using telnet and the server and ports listed in the relevant boxes.
Is this possible and can someone guide me through the coding of it please ?
Comments
you probably want to use Winsock instead of Telnet. right click on your controls toolbar (where the textbox, command button, etc are) and click "Components...", then find "Microsoft Winsock Control" in the list and check it. Winsock allows you to send data over the internet pretty easily.
to send mail you need to know SMTP (Simple Mail Transfer Protocol), but you're in luck, SMTP is very easy. (you didn't indicate whether you knew it or not in your post, so i dont know if you know it, you may already).
Anyway, here's an example that should do what you want with a Winsock control named "ws". you will also need a listbox named "lstOut" to see the mail server's output (for debugging), and a textbox named "txtSubject" for the subject of the message.
[code]
Private Sub cmdSend_Click()
ws.Close
ws.Connect txtServer.Text, Val(txtPort.Text)
lstOut.AddItem "Connecting to " & txtServer.Text & " on port " & txtPort.Text & "..."
End Sub
Private Sub ws_Close()
ws.Close
MsgBox "Communication complete, check lstOut to see whether mail was successfully sent."
End Sub
Private Sub ws_Connect()
lstOut.AddItem "Connected"
ws.SendData "HELO localhost" & vbCrLf
ws.SendData "MAIL FROM:<" & txtFrom.Text & ">" & vbCrLf
ws.SendData "RCPT TO:<" & txtTo.Text & ">" & vbCrLf
ws.SendData "DATA" & vbCrLf
ws.SendData "From:" & txtFrom.Text & vbCrLf
ws.SendData "To:" & txtTo.Text & vbCrLf
ws.SendData "Date:" & Date & vbCrLf
ws.SendData "Subject:" & txtSubject.Text & vbCrLf
ws.SendData "" & vbCrLf
ws.SendData txtMessage.Text & vbCrLf
ws.SendData "" & vbCrLf
ws.SendData "." & vbCrLf
ws.SendData "quit" & vbCrLf
lstOut.AddItem "Sending Mail Request..."
End Sub
Private Sub ws_DataArrival(ByVal bytesTotal As Long)
Dim gs As String, xArr() As String, xItem As Variant
ws.GetData gs
If InStr(gs, vbCrLf) Then
xArr = Split(gs, vbCrLf)
For Each xItem In xArr
If Not xItem = "" Then lstOut.AddItem xItem
Next
Else
lstOut.AddItem gs
End If
End Sub
Private Sub ws_Error(ByVal Number As Integer, Description As String, _
ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, _
ByVal HelpContext As Long, CancelDisplay As Boolean)
MsgBox Description, vbCritical, "Error"
End Sub
[/code]
also, mail servers usually won't relay messages so you'll need to either use an SMTP server that you know works or use the SMTP server of the person you're sending it to. most smtp servers are set up on port 25.