不負責實驗室之 SMTP

摘要:不負責實驗室之 SMTP

在.Net Framework中有提供了SmtpClient 類別,可以來做SMTP相關的功能,但在裝置上沒有沒有支援..但是又想可以在裝置上面用..怎麼辦?只好向google大神求救;找了些相關資料,測試了一下,是可以動作的,不過有點慢~測試嘛~要再補強了。
下面這邊有些參考資料可以看


下面是測試的程式碼,沒有經過詳細的測試與除錯(所以說是不負責實驗室嘛~),要靠各位看官發揚光大了,別忘了給我一份喔~


類別程式碼

Imports System.Net
Imports System.Text
Imports System.Net.Sockets

Public Class cSMTP
    Private _HOSTNAME As String = ""
    Private _HOSTPort As Integer
    Private _btySend() As Byte
    Private _btyRecv(255) As Byte
    Private _TCP As TcpClient
    Private _strTmp As String
    Private _SleepTime As Integer = 300
    Private _ConnectTimeOut As Integer = 1
    Private _dateTmp As DateTime
    Private _intReturn As Integer = 0
    Private _MailInfo As myMail

    Private Structure myMail
        Public MailDate As DateTime
        Public MailFrom As String
        Public MailTo As String
        Public Subject As String
        Public MailBody As String
    End Structure

    ''' 
''' 建立執行個體
'''
''' Public Sub New() _HOSTNAME = "stmp.hostname.tw" _HOSTPort = 25 _TCP = New TcpClient End Sub '''
''' SMTP主機位置
'''
''' String,Domain Name or IP address ''' ''' Public Property HostName() As String Get Return _HOSTNAME End Get Set(ByVal value As String) _HOSTNAME = value End Set End Property '''
''' SMTP主機Port Number
'''
''' Integer,Port number ''' ''' Public Property Port() As Integer Get Return _HOSTPort End Get Set(ByVal value As Integer) _HOSTPort = value End Set End Property '''
''' Connection timeout
'''
''' Integer,unit:second ''' ''' Public Property ConnectTimeOut() As Integer Get Return _ConnectTimeOut End Get Set(ByVal value As Integer) _ConnectTimeOut = value End Set End Property Public Property MailTo() As String Get Return _MailInfo.MailTo End Get Set(ByVal value As String) _MailInfo.MailTo = value End Set End Property Public Property MailFrom() As String Get Return _MailInfo.MailFrom End Get Set(ByVal value As String) _MailInfo.MailFrom = value End Set End Property Public Property MailBody() As String Get Return _MailInfo.MailBody End Get Set(ByVal value As String) _MailInfo.MailBody = value End Set End Property Public Property Subject() As String Get Return _MailInfo.Subject End Get Set(ByVal value As String) _MailInfo.Subject = value End Set End Property Public Function SendMail() As Boolean Try _TCP = New TcpClient _TCP.Connect(_HOSTNAME, _HOSTPort) _dateTmp = Now Do If _TCP.Client.Connected Then Exit Do End If If Math.Abs(DateDiff(DateInterval.Second, Now, _dateTmp)) > _ConnectTimeOut Then _TCP.Client.Close() _TCP.Close() Return False End If Loop _btySend = Encoding.ASCII.GetBytes("helo smpt.hostname.tw" & vbCrLf) _intReturn = _TCP.Client.Send(_btySend, 0, _btySend.Length, SocketFlags.None) Threading.Thread.Sleep(_SleepTime) _intReturn = _TCP.Client.Receive(_btyRecv) If _intReturn <= 0 Then Return False End If _btySend = Encoding.ASCII.GetBytes("mail from:" & _MailInfo.MailFrom & vbCrLf) _TCP.Client.Send(_btySend, 0, _btySend.Length, SocketFlags.None) Threading.Thread.Sleep(_SleepTime) Array.Clear(_btyRecv, 0, _btyRecv.Length) _intReturn = _TCP.Client.Receive(_btyRecv, 0, _btyRecv.Length, SocketFlags.None) _strTmp = Trim(Encoding.ASCII.GetString(_btyRecv, 0, _intReturn)) If _strTmp.Substring(0, 3) <> "250" Then Return False End If _btySend = Encoding.ASCII.GetBytes("rcpt to:" & _MailInfo.MailTo & vbCrLf) _TCP.Client.Send(_btySend, 0, _btySend.Length, SocketFlags.None) Threading.Thread.Sleep(_SleepTime) Array.Clear(_btyRecv, 0, _btyRecv.Length) _intReturn = _TCP.Client.Receive(_btyRecv, 0, _btyRecv.Length, SocketFlags.None) _strTmp = Trim(Encoding.ASCII.GetString(_btyRecv, 0, _intReturn)) If _strTmp.Substring(0, 3) <> "250" Then Return False End If _btySend = Encoding.ASCII.GetBytes("DATA" & vbCrLf) _TCP.Client.Send(_btySend, 0, _btySend.Length, SocketFlags.None) Threading.Thread.Sleep(_SleepTime) Array.Clear(_btyRecv, 0, _btyRecv.Length) _intReturn = _TCP.Client.Receive(_btyRecv, 0, _btyRecv.Length, SocketFlags.None) _strTmp = Trim(Encoding.ASCII.GetString(_btyRecv, 0, _intReturn)) If _strTmp.Substring(0, 3) <> "354" Then Return False End If _btySend = Encoding.Default.GetBytes( _ "Date:" & Format(Now, "yyyy/MM/dd HH:mm:ss") & vbCrLf & _ "From:" & _MailInfo.MailFrom & vbCrLf & _ "To:" & _MailInfo.MailTo & vbCrLf & _ "Subject:" & _MailInfo.Subject & vbCrLf & _ _MailInfo.MailBody & vbCrLf) _TCP.Client.Send(_btySend, 0, _btySend.Length, SocketFlags.None) Threading.Thread.Sleep(_SleepTime) _btySend = Encoding.ASCII.GetBytes("." & vbCrLf) _TCP.Client.Send(_btySend, 0, _btySend.Length, SocketFlags.None) Threading.Thread.Sleep(_SleepTime) Array.Clear(_btyRecv, 0, _btyRecv.Length) _intReturn = _TCP.Client.Receive(_btyRecv, 0, _btyRecv.Length, SocketFlags.None) _strTmp = Trim(Encoding.ASCII.GetString(_btyRecv, 0, _intReturn)) _strTmp = Encoding.ASCII.GetString(_btyRecv, 0, _btyRecv.Length) If _strTmp.Substring(0, 3) <> "250" Then Return False End If _btySend = Encoding.ASCII.GetBytes("quit" & vbCrLf) _TCP.Client.Send(_btySend, 0, _btySend.Length, SocketFlags.None) Threading.Thread.Sleep(_SleepTime) Array.Clear(_btyRecv, 0, _btyRecv.Length) _intReturn = _TCP.Client.Receive(_btyRecv, 0, _btyRecv.Length, SocketFlags.None) _strTmp = Trim(Encoding.ASCII.GetString(_btyRecv, 0, _intReturn)) If _strTmp.Substring(0, 3) <> "221" Then Return False End If If _TCP IsNot Nothing Then _TCP.Close() End If Return True Catch ex As Exception MessageBox.Show(ex.Message) End Try End Function Protected Overrides Sub Finalize() MyBase.Finalize() End Sub End Class

叫用範例

Dim mySmtp As New cSMTP

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        With mySmtp
            .MailFrom = "myMail@MyHost.com.tw"
            .MailTo = "SomeOne@SomeOne.com"
            .Subject = "Test Mail"
            .MailBody = "test test 這是中文"
            MessageBox.Show(.SendMail())
        End With
    End Sub