发送电子邮件源码(支持ESTMP协议,超文本电子邮件格式)

原创 专栏收录该内容
1 篇文章 0 订阅

经常在论坛看到网友提问如何发送电子邮件(SMTP服务器认证),其实只要知道客户端与邮件服务器之间会话,用户
名和密码是通过base64编码加密后传送,问题就很容易被解决.协议使用方面和smtp协议使用是一模一样的,值得
注意的就是esmtp协议在用户名和密码验证时,服务器返回一个354的消息代号.本代码没有针对错误提示信息处理,
但保证能正常发送邮件,大家可以自己完善它,本代码还使用IME编码,这样就可以发送超文本电子邮件了。用过
Foxmail4.1朋友应该知道,它实现的图文并茂声音邮件是它的一个亮点。本代码同样可以实现超文本邮件,要发
送mid做为背景音东的邮件,只要对ime代码部分稍加修改就可以实现,歌曲也须用base64编码后发送。目前流行
邮件病毒的原理和此一样,通过IE在IME中的漏洞实现,解决方法可以在注册表中删除IME编码格式,或者安装Winamp
同样也可以解决它(需做的只要在winamp支持的文件格式全选上),怎么越讲越跑题了,大家不要向我扔臭鸡蛋呀,
hehe,我也不多废话了,下附本软件所有代码.全整代码也可以在我的个人主页上下载,欢迎各位VB爱好与我联系,交
流编程技术.
主页:http://www.dapha.net
Msn:dapha@msn.com
VERSION 5.00
Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
Begin VB.Form Form1
   BorderStyle     =   1  'Fixed Single
   Caption         =   "邮件发送程序(支持smtp服务器验证)"
   ClientHeight    =   5550
   ClientLeft      =   45
   ClientTop       =   330
   ClientWidth     =   5805
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   ScaleHeight     =   5550
   ScaleWidth      =   5805
   StartUpPosition =   3  'Windows Default
   Begin MSWinsockLib.Winsock Winsock1
      Left            =   2640
      Top             =   2520
      _ExtentX        =   741
      _ExtentY        =   741
   End
   Begin VB.TextBox txtmessage1
      Height          =   1695
      Left            =   0
      MultiLine       =   -1  'True
      TabIndex        =   16
      ToolTipText     =   "这里为超文本信件内容"
      Top             =   3480
      Width           =   5775
   End
   Begin VB.TextBox user
      Height          =   270
      Left            =   3960
      TabIndex        =   15
      Top             =   840
      Width           =   1575
   End
   Begin VB.TextBox subject
      Height          =   270
      Left            =   960
      TabIndex        =   12
      Text            =   "你好"
      Top             =   1320
      Width           =   2295
   End
   Begin VB.TextBox txtserver
      Height          =   270
      Left            =   960
      TabIndex        =   9
      Text            =   "smtp.21cn.com"
      Top             =   960
      Width           =   2295
   End
   Begin VB.TextBox txtpwa
      Height          =   270
      IMEMode         =   3  'DISABLE
      Left            =   3960
      MaxLength       =   8
      PasswordChar    =   "*"
      TabIndex        =   7
      Top             =   1200
      Width           =   1575
   End
   Begin VB.TextBox getaddress
      Height          =   300
      Left            =   960
      TabIndex        =   5
      Top             =   600
      Width           =   2295
   End
   Begin VB.TextBox txtfrom
      Height          =   300
      Left            =   960
      TabIndex        =   3
      Top             =   240
      Width           =   2295
   End
   Begin VB.CommandButton cmdExit
      Caption         =   "退出"
      Height          =   375
      Left            =   4680
      TabIndex        =   2
      Top             =   240
      Width           =   975
   End
   Begin VB.CommandButton CmdSend
      Caption         =   "发送"
      Height          =   375
      Left            =   3360
      TabIndex        =   1
      Top             =   240
      Width           =   975
   End
   Begin VB.TextBox txtMessage
      Height          =   1815
      Left            =   0
      MultiLine       =   -1  'True
      TabIndex        =   0
      ToolTipText     =   "信件内容"
      Top             =   1680
      Width           =   5775
   End
   Begin VB.Label Label6
      AutoSize        =   -1  'True
      Caption         =   "用户名"
      Height          =   180
      Left            =   3360
      TabIndex        =   14
      Top             =   840
      Width           =   540
   End
   Begin VB.Label StatusTxt
      AutoSize        =   -1  'True
      BackStyle       =   0  'Transparent
      BorderStyle     =   1  'Fixed Single
      Height          =   285
      Left            =   960
      TabIndex        =   13
      Top             =   5200
      Width           =   3375
   End
   Begin VB.Label Label5
      AutoSize        =   -1  'True
      Caption         =   "主题:"
      Height          =   180
      Left            =   240
      TabIndex        =   11
      Top             =   1320
      Width           =   450
   End
   Begin VB.Label Label4
      AutoSize        =   -1  'True
      Caption         =   "SMTP服务器"
      Height          =   180
      Left            =   0
      TabIndex        =   10
      Top             =   960
      Width           =   900
   End
   Begin VB.Label Label3
      AutoSize        =   -1  'True
      Caption         =   "密码"
      Height          =   180
      Left            =   3360
      TabIndex        =   8
      Top             =   1200
      Width           =   360
   End
   Begin VB.Label Label2
      AutoSize        =   -1  'True
      Caption         =   "收信人地址"
      Height          =   180
      Left            =   0
      TabIndex        =   6
      Top             =   600
      Width           =   900
   End
   Begin VB.Label Label1
      AutoSize        =   -1  'True
      Caption         =   "发信人地址"
      Height          =   180
      Left            =   0
      TabIndex        =   4
      Top             =   240
      Width           =   900
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'程序组合:dapha(汪锋)
'下载http://www.dapha.net
'我是一名VB爱好者,希望得到大家的帮助,共同学习,进步
Private Enum SMTP_State
    MAIL_CONNECT
    MAIL_HELO
    MAIL_from
    MAIL_RCPTTO
    MAIL_DATA
    MAIL_DOT
    MAIL_QUIT
    MAIL_USER
    MAIL_PASS
    mail_login
End Enum
Private m_State As SMTP_State
Private m_strEncodedFiles As String
Private Function Base64_Encode(strSource) As String 'base6加密算法
    Const BASE64_TABLE As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
    Dim strTempLine As String
    Dim j As Integer
    For j = 1 To (Len(strSource) - Len(strSource) Mod 3) Step 3
        strTempLine = strTempLine + Mid(BASE64_TABLE, (Asc(Mid(strSource, j, 1)) / 4) + 1, 1)
        strTempLine = strTempLine + Mid(BASE64_TABLE, ((Asc(Mid(strSource, j, 1)) Mod 4) * 16 _
                      + Asc(Mid(strSource, j + 1, 1)) / 16) + 1, 1)
        strTempLine = strTempLine + Mid(BASE64_TABLE, ((Asc(Mid(strSource, j + 1, 1)) Mod 16) * 4 _
                      + Asc(Mid(strSource, j + 2, 1)) / 64) + 1, 1)
        strTempLine = strTempLine + Mid(BASE64_TABLE, (Asc(Mid(strSource, j + 2, 1)) Mod 64) + 1, 1)
    Next j
    If Not (Len(strSource) Mod 3) = 0 Then
         If (Len(strSource) Mod 3) = 2 Then
            strTempLine = strTempLine + Mid(BASE64_TABLE, (Asc(Mid(strSource, j, 1)) / 4) + 1, 1)
            strTempLine = strTempLine + Mid(BASE64_TABLE, (Asc(Mid(strSource, j, 1)) Mod 4) * 16 _
                      + Asc(Mid(strSource, j + 1, 1)) / 16 + 1, 1)
             strTempLine = strTempLine + Mid(BASE64_TABLE, (Asc(Mid(strSource, j + 1, 1)) Mod 16) * 4 + 1, 1)
            strTempLine = strTempLine & "="
        ElseIf (Len(strSource) Mod 3) = 1 Then
            strTempLine = strTempLine + Mid(BASE64_TABLE, Asc(Mid(strSource, j, 1)) / 4 + 1, 1)
            strTempLine = strTempLine + Mid(BASE64_TABLE, (Asc(Mid(strSource, j, 1)) Mod 4) * 16 + 1, 1)
             strTempLine = strTempLine & "=="
        End If
     End If
    Base64_Encode = strTempLine
End Function
Private Sub cmdExit_Click()
Unload Me
End Sub
Private Sub CmdSend_Click()
    Winsock1.Close
    Winsock1.LocalPort = 0
    strserver = txtserver
    ColonPos = InStr(strserver, ":")
    If ColonPos = 0 Then
        Winsock1.Connect strserver, 25
    Else
        lngPort = CLng(Right$(strserver, Len(strserver) - ColonPos))
        strserver = Left$(strserver, ColonPos - 1)
        Winsock1.Connect strserver, lngPort
    End If
    m_State = MAIL_CONNECT    '
    StatusTxt = "试图与服务器连接"
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    Dim strServerResponse   As String
    Dim strResponseCode     As String
    Dim strDataToSend       As String    '
    Const RandString As String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_"
    Dim Globalstr As String
    For jd = 1 To 24
        uniquey = Int(Rnd * Len(RandString)) + 1
        Globalstr = Globalstr + Mid(RandString, uniquey, 1)
    Next jd
    strime1 = "Subject:" + Chr(32) + subject + vbCrLf ' Subject of E-Mail
    strime = txtMessage + vbCrLf ' E-mail message body
    strime2 = "X-Mailer:程序太平洋:邮件发送软件V1.0" + vbCrLf ' What program sent the e-mail, customize this
    'MULTI-PART Edit
    strime = "------=_NextPart_" + Globalstr + vbCrLf + "Content-type: text/plain; charset=gb2312" + vbCrLf + vbCrLf + strime
    strime = strime + "------=_NextPart_" + Globalstr + vbCrLf + "Content-type: text/HTML" + vbCrLf + vbCrLf + txtmessage1 + vbCrLf + vbCrLf
    strime = strime + "------=_NextPart_" + Globalstr + "--" + vbCrLf
    strime1 = strime1 + "MIME-Version: 1.0" + vbCrLf + "Content-Type: multipart/alternative; " + vbCrLf + Chr(9) + "boundary=""----=_NextPart_" + Globalstr + """" + vbCrLf + vbCrLf + "This mail is In MIME format. Your mail interface does Not appear To support this format." + vbCrLf + vbCrLf
    strimeall = strime2 + strime1
    Winsock1.GetData strServerResponse
    strResponseCode = Left(strServerResponse, 3)
    If strResponseCode = "250" Or _
       strResponseCode = "220" Or _
       strResponseCode = "354" Or _
       strResponseCode = "334" Or _
       strResponseCode = "235" Then
        Select Case m_State
            Case MAIL_CONNECT
                m_State = MAIL_HELO
                strDataToSend = Trim$(txtfrom)
                'strDataToSend = Left$(strDataToSend, _
                                InStr(1, strDataToSend, "@") - 1)
                 Winsock1.SendData "HELO " & strDataToSend & vbCrLf
                 StatusTxt = "登陆服务器"
            Case MAIL_HELO
                 m_State = MAIL_USER
                 Winsock1.SendData "AUTH LOGIN" & vbCrLf
                 StatusTxt = "正在校验用户名"
            Case MAIL_USER
                 m_State = MAIL_PASS
                 Winsock1.SendData (Base64_Encode(Trim(user.Text))) & vbCrLf
                 StatusTxt = "校验用户密码"
            Case MAIL_PASS
                 m_State = mail_login
                 Winsock1.SendData (Base64_Encode(txtpwa)) & vbCrLf
                 StatusTxt = "发送人邮件地址"
            Case mail_login
                 m_State = MAIL_from
                 Winsock1.SendData "MAIL FROM:" & Trim$(txtfrom) & vbCrLf
                 StatusTxt = "接收人邮件地址"
            Case MAIL_from
                 m_State = MAIL_RCPTTO
                 Winsock1.SendData "RCPT TO:" & Trim$(getaddress) & vbCrLf
                 StatusTxt = "邮件发送之中..."
            Case MAIL_RCPTTO
                 m_State = MAIL_DATA
                 Winsock1.SendData "DATA" & vbCrLf
                 StatusTxt = "获取邮件内容"
            Case MAIL_DATA
                m_State = MAIL_DOT
                Winsock1.SendData "From:" & user.Text & " <" & txtfrom & ">" & vbCrLf
                Winsock1.SendData "To:" & toname & " <" & getaddress & ">" & vbCrLf
                Winsock1.SendData strimeall & vbCrLf
                Winsock1.SendData strime & vbCrLf
                Winsock1.SendData "." & vbCrLf
                StatusTxt = "邮件送完毕"
            Case MAIL_DOT
                m_State = MAIL_QUIT
                Winsock1.SendData "QUIT" & vbCrLf
                StatusTxt = "邮件成功发送!!!"
              Case MAIL_QUIT
                 Winsock1.Close
                 StatusTxt = "待命之中..."
         End Select
    Else
         Winsock1.Close
    End If
Debug.Print strServerResponse
End Sub
全文完...

  • 0
    点赞
  • 1
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值