Socket发送邮件(SMTP)简单例子!(源代码)

'请自觉尊重著作权!自由奔腾(wgscd)2004-12

Imports Microsoft.VisualBasic
Imports Microsoft.Win32
Imports System.Net
Imports System.IO
Imports System.Threading
Imports System.Net.Sockets
'VideoCampture using VB.NET
' author:自由奔腾 wgscd@126.com
'website:http://www.shizhu.net
'QQ:153964481
' 2004 11-26
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "

    Public Sub New()
        MyBase.New()

        '该调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        '在 InitializeComponent() 调用之后添加任何初始化

    End Sub

    '窗体重写 dispose 以清理组件列表。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer

    '注意: 以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改此过程。
    '不要使用代码编辑器修改它。
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Button3 As System.Windows.Forms.Button
    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents RichTextBox2 As System.Windows.Forms.RichTextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.RichTextBox1 = New System.Windows.Forms.RichTextBox
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.Button2 = New System.Windows.Forms.Button
        Me.Button3 = New System.Windows.Forms.Button
        Me.TextBox2 = New System.Windows.Forms.TextBox
        Me.Label1 = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.RichTextBox2 = New System.Windows.Forms.RichTextBox
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(288, 32)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(75, 32)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "发送"
        '
        'RichTextBox1
        '
        Me.RichTextBox1.Location = New System.Drawing.Point(24, 112)
        Me.RichTextBox1.Name = "RichTextBox1"
        Me.RichTextBox1.Size = New System.Drawing.Size(208, 160)
        Me.RichTextBox1.TabIndex = 2
        Me.RichTextBox1.Text = "RichTextBox1"
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(64, 16)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.TabIndex = 3
        Me.TextBox1.Text = "smtp.126.com"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(384, 72)
        Me.Button2.Name = "Button2"
        Me.Button2.TabIndex = 4
        Me.Button2.Text = "Button2"
        '
        'Button3
        '
        Me.Button3.Location = New System.Drawing.Point(448, 40)
        Me.Button3.Name = "Button3"
        Me.Button3.TabIndex = 5
        Me.Button3.Text = "Button3"
        '
        'TextBox2
        '
        Me.TextBox2.Location = New System.Drawing.Point(64, 64)
        Me.TextBox2.Name = "TextBox2"
        Me.TextBox2.TabIndex = 6
        Me.TextBox2.Text = "25"
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(8, 16)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(40, 23)
        Me.Label1.TabIndex = 7
        Me.Label1.Text = "Label1"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(16, 64)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(24, 23)
        Me.Label2.TabIndex = 8
        Me.Label2.Text = "Label2"
        '
        'RichTextBox2
        '
        Me.RichTextBox2.Location = New System.Drawing.Point(280, 120)
        Me.RichTextBox2.Name = "RichTextBox2"
        Me.RichTextBox2.Size = New System.Drawing.Size(168, 152)
        Me.RichTextBox2.TabIndex = 9
        Me.RichTextBox2.Text = "这是信的内容"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(592, 286)
        Me.Controls.Add(Me.RichTextBox2)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.TextBox2)
        Me.Controls.Add(Me.Button3)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.RichTextBox1)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "VideoCampture"
        Me.ResumeLayout(False)

    End Sub

#End Region
    Dim ipe As IPEndPoint
    Dim ip As IPAddress
    'ip = Dns.GetHostByName("smtp.126.com").AddressList(0)
    Dim oNetworkStream As NetworkStream
    Dim oStreamReader As StreamReader
    Dim oStreamWriter As StreamWriter
    Dim bytes(43888) As Byte
    Dim b As Byte()
    Dim client As TcpClient
    Dim mail As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

   

    Function ReadFromNetStream(ByRef NetStream As NetworkStream) As String

        Dim bb As Byte() = New Byte(521) {}
        NetStream.Read(bb, 0, bb.Length)

        Dim read As String = System.Text.Encoding.UTF8.GetString(bb)
        Return read

    End Function

    Function WriteToNetStream(ByRef NetStream As NetworkStream, ByVal command As String) As String
        Dim stringToSend As String = command & vbCrLf
        Dim arrayToSend As Byte() = System.Text.Encoding.UTF8.GetBytes(stringToSend.ToCharArray)
        NetStream.Write(arrayToSend, 0, arrayToSend.Length)
    End Function

    Sub sendmail(ByRef NetStream As NetworkStream, ByVal message As String)
        Dim arrayToSend As Byte() = System.Text.Encoding.UTF8.GetBytes(message.ToCharArray)
        NetStream.Write(arrayToSend, 0, arrayToSend.Length)
    End Sub
    Function checkForError(ByVal strMessage As String, ByVal check As String) As String
        If strMessage.IndexOf(check) = -1 Then

            Return "error"
        Else : Return "correct"

        End If

    End Function

    '/===================================================================
    '/-------------

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'ip = Dns.GetHostByName("smtp.126.com").AddressList(0)
        'Dim i As Integer
        ' For i = 0 To 4
        client = New TcpClient(TextBox1.Text, Int32.Parse(TextBox2.Text))
        Dim stream As NetworkStream = client.GetStream()
        Dim come As String = ReadFromNetStream(stream)

        RichTextBox1.AppendText("连接答应:" & come & vbCrLf)
        Dim check As String = checkForError(come, "220")

        While check = "error"

            client.Connect("smtp.126.com", 25)
            come = ReadFromNetStream(stream)
            Me.RichTextBox1.AppendText("连接答应:" & come & vbCrLf)
            check = checkForError(come, "220")

        End While


        Dim hostName As String = Dns.GetHostName

        WriteToNetStream(stream, "EHLO " & hostName)

        come = ReadFromNetStream(stream)
        RichTextBox1.AppendText("EHLO 答应:" & come & vbCrLf)
        check = checkForError(come, "250")
        Dim round As Integer = 0

        While check = "erro" 'And round < 5

            round += 1

            WriteToNetStream(stream, "EHLO " & Command())

            come = ReadFromNetStream(stream)
            RichTextBox1.AppendText("EHLO 答应:" & come & vbCrLf)
            check = checkForError(come, "250")

        End While
        '-----------------------------------------
        WriteToNetStream(stream, "AUTH LOGIN  ")

        come = ReadFromNetStream(stream)
        RichTextBox1.AppendText("AUTH LOGIN 答应:" & come & vbCrLf)
        check = checkForError(come, "334")

        '---------------------------
        WriteToNetStream(stream, "wgscd2008")'注意:必须是BASE64编码的用户名
        come = ReadFromNetStream(stream)
        RichTextBox1.AppendText("用户名答应:" & come & vbCrLf)
        check = checkForError(come, "334")

        '--------------------------
        WriteToNetStream(stream, "********")'注意:必须是BASE64编码的密码
        come = ReadFromNetStream(stream)
        RichTextBox1.AppendText("密码答应:" & come & vbCrLf)
        check = checkForError(come, "235")

        '----------------------------------------------
        WriteToNetStream(stream, "MAIL FROM:wgscd2008@126.com")
        come = ReadFromNetStream(stream)
        RichTextBox1.AppendText("MAIL答应:" & come & vbCrLf)
        check = checkForError(come, "250")

        '------------------------------------------------------------------
        WriteToNetStream(stream, "RCPT TO:wgscd@126.com")
        come = ReadFromNetStream(stream)
        RichTextBox1.AppendText("RCPT TO答应:" & come & vbCrLf)
        check = checkForError(come, "250")
        '-----------------------------------------------------------------------
        WriteToNetStream(stream, "DATA")
        come = ReadFromNetStream(stream)
        RichTextBox1.AppendText("DATA答应:" & come & vbCrLf)
        check = checkForError(come, "354")

        '---------------------------------------------------------------------
        WriteToNetStream(stream, "Subject: how are you wgscd ? ")
        ' WriteToNetStream(stream, "hjhjhjhjhk" & vbCr & "test ok ? 爱你一万年")
        Dim b As Byte() = System.Text.Encoding.UTF8.GetBytes(RichTextBox2.Text.ToCharArray)
        Dim strmsg As String = System.Text.Encoding.UTF8.GetString(b)
  
        WriteToNetStream(stream, vbCr & "test ok44 " & strmsg & vbCrLf & "." & vbCrLf)

        ' WriteToNetStream(stream, vbCr & "test ok44爱你一万年 ? " & vbCrLf & "." & vbCrLf)
        ' come = ReadFromNetStream(stream)
        'RichTextBox1.AppendText("DATA答应:" & come & vbCrLf)
        ' check = checkForError(come, "354")


        '--------------------------------------------
        'WriteToNetStream(stream, "这是实验!!!" & vbCrLf & "." & vbCrLf)
        'come = ReadFromNetStream(stream)
        'RichTextBox1.AppendText("DATA答应:" & come & vbCrLf)
        ' check = checkForError(come, "354")

        '---------------------------

        WriteToNetStream(stream, "QUIT")
        come = ReadFromNetStream(stream)
        RichTextBox1.AppendText("发送完毕,服务器答应:" & come & vbCrLf)
        check = checkForError(come, "250")

        '  Next
    End Sub

    '///


    Sub sender1()

        Dim come As String = ""

        Dim check As String = ""

        Dim stream As NetworkStream = client.GetStream

        Dim hostName As String = Dns.GetHostName

        WriteToNetStream(stream, "EHLO " & hostName)

        come = ReadFromNetStream(stream)
        RichTextBox1.AppendText("EHLO 答应:" & come & vbCrLf)
        check = checkForError(come, "250")
        Dim round As Integer = 0

        While check = "erro" Or round < 5

            round += 1

            WriteToNetStream(stream, "EHLO " & Command())

            come = ReadFromNetStream(stream)
            RichTextBox1.AppendText("EHLO 答应:" & come & vbCrLf)
            check = checkForError(come, "250")

        End While


    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim aa As shapBse64.SBase64
        Dim en As System.Text.Encoding

        aa = New shapBse64.SBase64
        Me.Text = aa.EncodingForString("00000000000", en.Default)

 

 

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        sender1()
    End Sub
End Class

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值