This tutorial allows you to create a small application that allows the sending of an e-mail, sending simulating via Telnet, but using a graphical interface.
First you have to draw the objects in the Form, then:
Obviously you have to call the TextBox and button as I have shown. As for the txtContent must check the box MultiLine so you can insert multiple rows in the TextBox.
First you must import some references in the code, then:
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.IO Imports
System.Threading Imports System.Windows.Forms
After you import them, then declare the variables (statements should be added after Public Class Form1 ):
Dim myTcpClient As New TcpClient () Dim
myNetworkStream As NetworkStream Dim
myThread As Thread
Double-clicking on the button, then entering the Click event of btnSend entered:
myThread = New Thread (AddressOf SMTPMailProcess)
myThread.Start ()
Here we will start the process of sending e-mail. Following is part of the procedure SMTPMailProcess :
Private Sub SMTPMailProcess()
Dim strTo As String = txtTo.Text
Dim strSubject As String = txtSubject.Text
Dim strContent As String = txtContent.Text
Dim strCommand As String
Dim lngSize As Long
Try
myTcpClient.Connect(txtServer.Text, 465)
myNetworkStream = myTcpClient.GetStream
lngSize = myTcpClient.ReceiveBufferSize
ReDim dteSendData(lngSize)
myNetworkStream.Read(dteSendData, 0, lngSize)
SMTPCommandSend(strCommand)
strCommand = "MAIL FROM: <" + strFrom + ">"
SMTPCommandSend(strCommand)
strCommand = "RCPT TO: <" + strTo + ">"
SMTPCommandSend(strCommand)
strCommand = "DATA "
SMTPCommandSend(strCommand)
strCommand = "DATE: " + Date .Now.ToString + vbCrLf + _
"FROM: " + strFrom + vbCrLf + _
"TO: " + strTo + vbCrLf + _
"SUBJECT: " + strSubject + vbCrLf + vbCrLf + _
strContent + vbCrLf + "."
SMTPCommandSend(strCommand)
strCommand = "QUIT "
SMTPCommandSend(strCommand)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Finally, the last part of procedure to run command to send the email (Es . all at an associate members, etc ...):
Private Sub SMTPCommandSend (ByVal strSend As String )
Dim bteResponse() As Byte
Try
myNetworkStream.Write(bteSend, 0, bteSend.Length)
ReDim bteResponse(myTcpClient.ReceiveBufferSize)
myNetworkStream.Read(bteResponse, 0, bteResponse.Length)
Catch ex As
Exception MessageBox.Show (ex.Message)
End Try
End Sub
0 comments:
Post a Comment