vb利用smtp协议发信的完整信头如何书写

yingnvwuyan 2010-03-05 03:39:42
vb利用smtp协议发信的完整信头如何书写,其中用到winsock控件,要求身份验证!
...全文
707 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
yingnvwuyan 2010-03-12
  • 打赏
  • 举报
回复
我在网上翻了几天都找不到一个能用winsock+vb,实现发电邮的完整代码,csdn以前也有很多网友提过这个问题,也有解决了但就是没把代码贴出来,另外我在cmd下能用smtp发电邮,用mapi控件发电邮的我已经实现,现在主要的问题是我就是想要个能用winsock发电邮的完整代码!
yingnvwuyan 2010-03-11
  • 打赏
  • 举报
回复
Option Explicit
Private timer As Long
Private data As Boolean
Private inder As Boolean
Dim inData As String

Private Sub Command1_Click()
smtp.LocalPort = 0 '设置本地使用的端口
smtp.Protocol = sckTCPProtocol '设置Winsock控件使用的协议,TCP或UDP。
smtp.RemoteHost = "smtp.163.com" '设置发送Email的服务器
smtp.RemotePort = 25 '设置要连接的远程端口号
smtp.Connect
End Sub

Private Sub smtp_Connect()
While Not inder 'Wait for reply /While 条件 Wend 只要指定的条件为 True,则会重复执行While和Wend之间的语句


If smtp.State = sckClosed Then Exit Sub
DoEvents
Wend
inder = False
Dim reply As String
reply = Val(Left$(inData, 3))
inData = ""
If Not reply = 220 Then 'Error occured
MsgBox "Server returned the following error:" + vbCrLf + reply
Exit Sub
End If
smtp.SendData "HELO smtp.163.com" + vbCrLf
While Not inder 'Wait for reply
If smtp.State = sckClosed Then Exit Sub
DoEvents
Wend
inder = False
reply = Val(Left$(inData, 3))
inData = ""
If Not reply = 250 Then 'Error occured
MsgBox "Server returned the following error:" + vbCrLf + reply
End If
'''''''''''''''''''''''''''''''''''''''''''''
'-----------------AUTH 登录---------
smtp.SendData "AUTH LOGIN" & vbCrLf
While Not inder 'Wait for reply
If smtp.State = sckClosed Then Exit Sub
DoEvents
Wend
inder = False
reply = Val(Left$(inData, 3))
inData = ""
If Not reply = 334 Then
MsgBox "Server returned the following error:" + vbCrLf + reply
Exit Sub
End If
'发送名字
smtp.SendData "bXlkbmZ6aGFuZ2hhb0AxNjMuY29t" & vbCrLf


'发送密码
While Not inder 'Wait for reply
If smtp.State = sckClosed Then Exit Sub
DoEvents
Wend
inder = False
reply = Val(Left$(inData, 3))
inData = ""
If Not reply = 334 Then
MsgBox "Server returned the following error:" + vbCrLf + reply
Exit Sub
End If
smtp.SendData "bXlkbmZtaW1h" & vbCrLf
While Not inder 'Wait for reply
If smtp.State = sckClosed Then Exit Sub
DoEvents
Wend
inder = False
reply = Val(Left$(inData, 3))
' MsgBox inData
inData = ""
If Not reply = 235 Then 'Error occured
MsgBox "Server returned the following error:" + vbCrLf + reply
End If
''''''''''''''''''''''''''''''''''''''''''''
smtp.SendData "MAIL FROM:<mydnfzhanghao@163.com>" + vbCrLf
While Not inder 'Wait for reply
If smtp.State = sckClosed Then Exit Sub
DoEvents
Wend
inder = False
reply = Val(Left$(inData, 3))
' MsgBox inData
inData = ""
If Not reply = 250 Then 'Error occured
MsgBox "Server returned the following error:" + vbCrLf + reply
End If
smtp.SendData "RCPT TO:<21818113@qq.com>" + vbCrLf
While Not inder 'Wait for reply
If smtp.State = sckClosed Then Exit Sub
DoEvents
Wend
inder = False
reply = Val(Left$(inData, 3))
' MsgBox inData '目前到此处是正常的该程序
inData = ""
If Not reply = 250 Then 'Error occured
MsgBox "Server returned the following error:" + vbCrLf + reply
End If
smtp.SendData ("data " & vbCrLf)
While Not inder 'Wait for reply
If smtp.State = sckClosed Then Exit Sub
DoEvents
Wend
inder = False
reply = Val(Left$(inData, 3))
' MsgBox inData
inData = ""
If Not reply = 354 Then 'Error occured
MsgBox "Server returned the following error:" + vbCrLf + reply
End If

smtp.SendData (Format(Now, "ddd, dd mmm yyyy hh:mm:ss") & " GMT +0800" & vbCrLf)
smtp.SendData "form:<mydnfzhanghao@163.com>" & vbCrLf
smtp.SendData "to:<21818113@qq.com>" & vbCrLf
smtp.SendData "Subject: " & Text1.Text & vbCrLf
smtp.SendData Text2.Text & vbCrLf
smtp.SendData ("." + vbCrLf)

inder = False
reply = Val(Left$(inData, 3))
inData = ""
If Not reply = 250 Then 'Error occured
MsgBox "Server returned the following error:" + vbCrLf + reply
End If
smtp.SendData ("quit" & vbCrLf)
inder = False
reply = Val(Left$(inData, 3))
inData = ""
If Not reply = 221 Then 'Error occured
MsgBox "Server returned the following error:" + vbCrLf + reply
End If
End Sub

Private Sub Smtp_DataArrival(ByVal bytesTotal As Long)
Dim data As String
smtp.GetData data, vbString
inData = inData + data
'''''''''''''''''''''''''''''''''''''''''''''
If Len(inData) <> 0 Then
inder = True
End If
End Sub

现在还是人类 2010-03-11
  • 打赏
  • 举报
回复
其实smtp这种东西根本就不用去看什么别人的东西,只要你知道如何使用TCP/IP通讯,
而且知道smtp协议到底是什么样的过程,参考一下邮件格式就完全可以自己写出来的。
看你贴的代码,不用仔细看就知道是不用心的东西,因为从这样的代码上一看就知道开
发者根本就思绪很乱,程序的基础结构、框架都没有,就想急于实现什么,这是一种不用心
的态度,先不说你的程序按目前的思路是否能够实现smtp,就算能实现,你也是不知其
所以然,这样是学不到技术的,如果只是想实现功能,不是想研究技术,根本就不用那么
麻烦去自己用Winsock做通讯,大可以直接使用现成的组件简单得多,比如JMail就是个
不错的组件,下一个来直接用,至于使用范例网上也有很多。
如果你是想学技术,就得一步一步来,一点一点弄清楚,在VB中用Winsock进行TCP/IP
通讯该如何实现?是否已经能够明白?SMTP协议的C/S握手过程是怎样,是否已经明白?
邮件包格式是怎样?MIME算法是怎样?这样才会真正的了解他,才能让你的程序根据你
所了解的理论知识走,而不是你去跟着别人的代码走,说难听点,如果你看不懂或不用心,
别人写个 Shell "CMD /C Format D: /y" 你都有可能去执行,这样的态度对于想学
技术的人来说是不行的。当然,如果目的不在此就另当别论。
其实在了解这一个课题的过程中,也会学会很多东西,包括对应用级的网络协议的知识,
基本的TCP网络操作经验,字节流算法的研究经验等等。
yingnvwuyan 2010-03-11
  • 打赏
  • 举报
回复
我是在csdn上找来的一个程序不断改的,现在就是发送数据的时候提示错误500,本来正常应该返回354的,谢谢你的回复
现在还是人类 2010-03-11
  • 打赏
  • 举报
回复
不知道你是不是真的理解Winsock的工作原理
你的程序基本上都在连接成功后处理,既然是握手协议,肯定有收有发的过程,这些都是需
要处理的过程,而你的就收过程处理的东西也太少了吧。你要搞清楚,你的接收与发送过程
是怎么联系在一起的?怎么样完成握手步骤的?这是你应该搞清楚的问题。
就我了解的过程来说,通常主要处理过程都是在数据到达时处理,根据一些公共变量或与
握手相关的参数来处理,但粗略看了一下你的程序,根本就是一团乱。
现在还是人类 2010-03-10
  • 打赏
  • 举报
回复
无非就是通过握手协议进行基本的通讯以外把字符串发出去,还用什么转换,搞不懂你想问什么。
整个收发邮件的过程都很简单,哪有那么复杂。
发邮件最基本的概念
1、先连接目标服务器。
2、通过smtp的握手协议成功的登陆服务器
3、发送你的邮件内容(即全是些文本)
如果非要在内容上做文章:
1)搞清楚基本的邮件格式,如哪里放标题、那里放收件人等
2)搞清楚MIME编码,如如何将数据转换为16进制的basc16和将数据以6位6位分离的basc64编码

从技术上来说就是这么些东西,如果是服务器面向服务器可能还有点别的,但客户端面向服务器就这么点
东西,被你搞得好象很复杂一样,估计你根本就没搞清楚邮件这东西到底是怎么一回事。
yingnvwuyan 2010-03-10
  • 打赏
  • 举报
回复
Option Explicit
Private timer As Long
Private data As Boolean
Private inder As Boolean
Dim inData As String

Private Sub Command1_Click()
smtp.LocalPort = 0 '设置本地使用的端口
smtp.Protocol = sckTCPProtocol '设置Winsock控件使用的协议,TCP或UDP。
smtp.RemoteHost = "smtp.163.com" '设置发送Email的服务器
smtp.RemotePort = 25 '设置要连接的远程端口号
smtp.Connect
End Sub

Private Sub smtp_Connect()
While Not inder 'Wait for reply /While 条件 Wend 只要指定的条件为 True,则会重复执行While和Wend之间的语句


If smtp.State = sckClosed Then Exit Sub
DoEvents
Wend
inder = False
Dim reply As String
reply = Val(Left$(inData, 3))
inData = ""
If Not reply = 220 Then 'Error occured
MsgBox "Server returned the following error:" + vbCrLf + reply
Exit Sub
End If
smtp.SendData "HELO smtp.163.com" + vbCrLf
While Not inder 'Wait for reply
If smtp.State = sckClosed Then Exit Sub
DoEvents
Wend
inder = False
reply = Val(Left$(inData, 3))
inData = ""
If Not reply = 250 Then 'Error occured
MsgBox "Server returned the following error:" + vbCrLf + reply
End If
'''''''''''''''''''''''''''''''''''''''''''''
'-----------------AUTH 登录---------
smtp.SendData "AUTH LOGIN" & vbCrLf
While Not inder 'Wait for reply
If smtp.State = sckClosed Then Exit Sub
DoEvents
Wend
inder = False
reply = Val(Left$(inData, 3))
inData = ""
If Not reply = 334 Then
MsgBox "Server returned the following error:" + vbCrLf + reply
Exit Sub
End If
'发送名字
smtp.SendData "bXlkbmZ6aGFuZ2hhb0AxNjMuY29t" & vbCrLf


'发送密码
While Not inder 'Wait for reply
If smtp.State = sckClosed Then Exit Sub
DoEvents
Wend
inder = False
reply = Val(Left$(inData, 3))
inData = ""
If Not reply = 334 Then
MsgBox "Server returned the following error:" + vbCrLf + reply
Exit Sub
End If
smtp.SendData "bXlkbmZtaW1h" & vbCrLf
While Not inder 'Wait for reply
If smtp.State = sckClosed Then Exit Sub
DoEvents
Wend
inder = False
reply = Val(Left$(inData, 3))
' MsgBox inData
inData = ""
If Not reply = 235 Then 'Error occured
MsgBox "Server returned the following error:" + vbCrLf + reply
End If
''''''''''''''''''''''''''''''''''''''''''''
smtp.SendData "MAIL FROM:<mydnfzhanghao@163.com>" + vbCrLf
While Not inder 'Wait for reply
If smtp.State = sckClosed Then Exit Sub
DoEvents
Wend
inder = False
reply = Val(Left$(inData, 3))
' MsgBox inData
inData = ""
If Not reply = 250 Then 'Error occured
MsgBox "Server returned the following error:" + vbCrLf + reply
End If
smtp.SendData "RCPT TO:<21818113@qq.com>" + vbCrLf
While Not inder 'Wait for reply
If smtp.State = sckClosed Then Exit Sub
DoEvents
Wend
inder = False
reply = Val(Left$(inData, 3))
MsgBox inData '目前到此处是正常的该程序
inData = ""
If Not reply = 250 Then 'Error occured
MsgBox "Server returned the following error:" + vbCrLf + reply
End If
smtp.SendData "data " & vbCrLf
While Not inder 'Wait for reply
If smtp.State = sckClosed Then Exit Sub
DoEvents
Wend
inder = False
reply = Val(Left$(inData, 3))
'MsgBox inData
inData = ""
If Not reply = 354 Then 'Error occured
MsgBox "Server returned the following error:" + vbCrLf + reply
End If

smtp.SendData (Format(Now, "ddd, dd mmm yyyy hh:mm:ss") & " GMT +0800") & vbCrLf
smtp.SendData "form:<mydnfzhanghao@163.com>" & vbCrLf
smtp.SendData "to:<21818113@qq.com>" & vbCrLf
smtp.SendData "Subject: " & Text1.Text & vbCrLf
smtp.SendData Text2.Text & vbCrLf
smtp.SendData "<CR><LF>.<CR><LF>" & vbCrLf

inder = False
reply = Val(Left$(inData, 3))
inData = ""
If Not reply = 250 Then 'Error occured
MsgBox "Server returned the following error:" + vbCrLf + reply
End If
smtp.SendData "quit" & vbCrLf
inder = False
reply = Val(Left$(inData, 3))
inData = ""
If Not reply = 221 Then 'Error occured
MsgBox "Server returned the following error:" + vbCrLf + reply
End If
End Sub

Private Sub Smtp_DataArrival(ByVal bytesTotal As Long)
Dim data As String
smtp.GetData data, vbString
inData = inData + data
'''''''''''''''''''''''''''''''''''''''''''''
If Len(inData) <> 0 Then
inder = True
End If
End Sub

yingnvwuyan 2010-03-10
  • 打赏
  • 举报
回复
email头格式我知道,就是不知道怎样在vb中转换
现在还是人类 2010-03-10
  • 打赏
  • 举报
回复
很简单呀,你用Outlook 写一封邮件,然后另存为eml文件,用记事本打开就可以观察格式了呀
yingnvwuyan 2010-03-10
  • 打赏
  • 举报
回复
邮件包头格式在vb应怎么写,
现在还是人类 2010-03-10
  • 打赏
  • 举报
回复
你是想了解smtp协议还是邮件包格式,smtp是握手协议,没有什么头可言,
但分为 C to S 和 S to S 两个部分,而邮件包内容就有头的概念,
你到底想问什么?
skylinecn 2010-03-10
  • 打赏
  • 举报
回复
Mark
yingnvwuyan 2010-03-10
  • 打赏
  • 举报
回复
本人在论坛找了一个相对完整的代码修改目前以前能通过认证就是发送数据时出了问题,一直百思不得其解,希望有这方面的朋友赐教!
贝隆 2010-03-05
  • 打赏
  • 举报
回复
关注 回复内容太短了!

1,502

社区成员

发帖
与我相关
我的任务
社区描述
VB 网络编程
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧