Per prima cosa si devono disegnare gli oggetti all’interno della Form, quindi:
Ovviamente dovete denominare le TextBox ed il tasto così come ho indicato nella figura. Per quanto riguarda la txtContent si deve selezionare la casella MultiLine in modo da poter inserire più righe in quella TextBox.
Per prima cosa si devono importare alcune Referenze all’interno del codice, quindi:
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.IO
Imports System.Threading
Imports System.Windows.Forms
Dopo averle importate, dichiarare le variabili quindi (le dichiarazioni vanno inserite dopo Public Class Form1):
Dim myTcpClient As New TcpClient()
Dim myNetworkStream As NetworkStream
Dim myThread As Thread
Facendo doppio click sul pulsante, quindi entrando nell’evento click di btnSend inserite:
myThread = New Thread(AddressOf SMTPMailProcess)
myThread.Start()
Qui si farà partire la procedura di invio dell’e-mail. In seguito si inserisce la procedura 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
Infine si inserisce l’ultima procedura che lancia i comandi per inviare l’email (Es. associare il tutto ad un socie, ecc…):
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
1 commento:
non funzia
Posta un commento