100分求高人路过驻足:winsock发附件,大致是怎么个步骤呢?是不是先base64加密,然后才发数据?不能直接发?

j88198188 2012-02-21 03:05:36
类似这样的数据?:看不懂
        wsk.SendData "--Unique-Boundary" & vbCrLf
wsk.SendData "Content-Type: multipart/parallel; boundary=Unique-Boundary-2" & vbCrLf & vbCrLf
wsk.SendData "--Unique-Boundary-2" & vbCrLf
wsk.SendData "Content-Type: application/octet-stream;" & vbCrLf
wsk.SendData " name=" & strAttachName & vbCrLf'strAttachName是附件含路径名
wsk.SendData "Content-Transfer-Encoding: base64" & vbCrLf
wsk.SendData "Content-Disposition: inline;" & vbCrLf
wsk.SendData " filename=" & strAttachName & vbCrLf & vbCrLf'strAttachName是附件含路径名
wsk.SendData txtEncodedFile.Text & "==" & vbCrLf'这个是干嘛的? 更多代码详见下面
wsk.SendData "--Unique-Boundary-2----Unique-Boundary--"


完整代码:包括一个窗体和一个模块:太长了,模块在2 3楼.或者劳驾移步下载:http://download.csdn.net/detail/j88198188/4078600
窗体:

Option Explicit

Private Sub cmdAbout_Click()
frmAbout.Show vbModal
End Sub

Private Sub cmdClose_Click()
Unload Me
End Sub

Private Sub cmdSelect_Click()

cmdDialog.ShowOpen
txtAttach = cmdDialog.FileName

End Sub

Private Sub cmdSend_Click()

cmdSend.Enabled = False

If ValidateEntry = False Then MsgBox "Either the server name or to address were left empty.", vbCritical + vbOKOnly, Me.Caption: cmdSend.Enabled = True: Exit Sub

If txtAttach.Text <> "" Then
lblStatus = "Encoding file attachment"
Base64EncodeFile txtAttach.Text, rtfAttach, txtOutput
End If

lblStatus = "Connecting to POP Server"
ConnectToServer txtServer.Text, Winsock1

End Sub

Private Sub Form_Load()

txtAttach = ""
txtBody = ""
txtFromAddress = ""
txtServer = ""
txtSubject = ""
txtToAddress = ""


End Sub

Private Sub Winsock1_Connect()

lblStatus = "Connected to POP Server"

Wait 0.5

lblStatus = "Sending mail"

If txtAttach.Text = "" Then

SendMail txtFromAddress, txtToAddress, txtSubject, txtBody, Winsock1

Else

SendMail txtFromAddress, txtToAddress, txtSubject, txtBody, Winsock1, txtAttach, txtOutput

End If

lblStatus = "Mail sent"

cmdSend.Enabled = True

lblStatus = "Status:"

End Sub

Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)

MsgBox "Error Number: " & Number & vbCrLf & Description & vbCrLf & Source, vbCritical + vbOKOnly, Me.Caption

End Sub

Private Function ValidateEntry() As Boolean

ValidateEntry = True

If txtServer.Text = "" Or txtToAddress = "" Then ValidateEntry = False

End Function

...全文
188 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
xayzmb 2012-02-23
  • 打赏
  • 举报
回复
转成一个文件
用二进制发送
j88198188 2012-02-23
  • 打赏
  • 举报
回复
http://download.csdn.net/detail/j88198188/4082303
哈了 2012-02-22
  • 打赏
  • 举报
回复
每天回帖即可获得10分可用分
j88198188 2012-02-22
  • 打赏
  • 举报
回复
都搞不清他代码里哪几行是跟base64加密附件相关的代码
j88198188 2012-02-22
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 veron_04 的回复:]

发送电子邮件附件


与简单电子邮件发送程序相比,本示例程序多了框架控件及其中的内容。框架中的列表框用来显示附件的路径,另外两个按钮的作用相信你一看就知道。真正发送附件的代码在Send message按钮的Click事件中。在该事件中多了一小段代码:

For i = 0 To lstAttachments.ListCount - 1
lstAttachments.ListIndex……
[/Quote]
这个UUEncodeFile不是base64加密?但是用这种加密也可以,并不一定要base64加密是吗?
还有个效率问题呵,不知道前面代码里这个base64加密效率怎么样
j88198188 2012-02-22
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 vinjent 的回复:]

試試看在.net之下,外寄郵件會省事許多
Dim mailMessage As New System.Net.Mail.MailMessage()
mailMessage.To.Add(xxx)'寄至郵件地址
mailMessage.From = New System.Net.Mail.MailAddress(xxx)'寄出郵件地址
……
[/Quote]
这个是叫"mailMessage类"呵? 这个就一定要SMTP账号了?
我想同时实现要不要通不通过SMTP都行,我看到例子Winsock可以.
winsock好像是很麻烦,还要自己加密.附件多几个的话麻烦死
多谢!
j88198188 2012-02-22
  • 打赏
  • 举报
回复
在我上传的资源里了.
晕死,还不显示出来,我本来想贴个地址出来.
j88198188 2012-02-22
  • 打赏
  • 举报
回复
终于搞清了头绪,我照猫画虎完全按他的方法,自己做了个,可是结果是没有附件,而是把转码后的东西全部给放到正文里了,并没有显示附件,这是什么状况?

它的实质只有两句:
txtAttach = cmdDialog.FileName ' 获得附件名!!
Base64EncodeFile txtAttach.Text, rtfAttach, txtOutput ''''''''这个实质
其他就是那个lala过程了.两个函数我都直接拷贝过去的.

正文里这个样子:我贴不出来,csdn说又非法字符了
-------------
--Unique-Boundary
Content-Type: multipart/parallel; boundary=Unique-Boundary-2

--Unique-Boundary-2
lyserver 2012-02-22
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 j88198188 的回复:]

谢谢of123勋章美女!!

为什么别的部分比如from subject 之类的又不编码呢?它们也要路过7-bit 传输网段
如果是传输原因,这样的话那互联网上走的一切东西都应该编码过?不然有些窄轨过不去?

不懂ing,就是觉得挺麻烦.多谢!!
[/Quote]
1、BASE64编码的目的是为了能够发送二进制内容
2、subject其实也可以编码的。
3、from、to等邮件头字段一般不需要编码,但个别ESMTP服务器要求必须加上名称,比如:to:xxx@163.com<xxx@163.com>。
j88198188 2012-02-22
  • 打赏
  • 举报
回复
谢谢of123勋章美女!!

为什么别的部分比如from subject 之类的又不编码呢?它们也要路过7-bit 传输网段
如果是传输原因,这样的话那互联网上走的一切东西都应该编码过?不然有些窄轨过不去?

不懂ing,就是觉得挺麻烦.多谢!!
of123 2012-02-22
  • 打赏
  • 举报
回复
严格地说,Base64 是一种编码方法,而不是加密。

因为历史原因,互联网的有些网段可能是采用 7-bit 传输的。就好像铁路网中间,有些路段是窄轨。为了保证 Email 传输的数据能够通过所有网段,因此要将数据采用 Base64 编码,使所有的数据都以最高位为 0 的 ASCII 编码来表示。显然,由于每字节的信息量小了,就必然要增加字节数,以保证足够的信息量。

因此,凡是需要传递到收件人的信息,都必须采用 Base64 编码。
X-i-n 2012-02-22
  • 打赏
  • 举报
回复
Private Sub cmdSend_Click()    
cmdSend.Enabled = False
If ValidateEntry = False Then MsgBox "Either the server name or to address were left empty.", vbCritical + vbOKOnly, Me.Caption: cmdSend.Enabled = True: Exit Sub
If txtAttach.Text <> "" Then
lblStatus = "Encoding file attachment"
Base64EncodeFile txtAttach.Text, rtfAttach, txtOutput '加密txtAttach.Text
End If

lblStatus = "Connecting to POP Server"
ConnectToServer txtServer.Text, Winsock1
End Sub
'=====================================
wsk.SendData " filename=" & strAttachName & vbCrLf & vbCrLf'strAttachName是附件含路径名
wsk.SendData txtEncodedFile.Text & "==" & vbCrLf'这个是干嘛的? 这就是发送附件啊 txtEncodedFile里的就是加密过的结果!!!!!!!!!!!!!!……
vinjent 2012-02-21
  • 打赏
  • 举报
回复
試試看在.net之下,外寄郵件會省事許多
Dim mailMessage As New System.Net.Mail.MailMessage()
mailMessage.To.Add(xxx)'寄至郵件地址
mailMessage.From = New System.Net.Mail.MailAddress(xxx)'寄出郵件地址
mailMessage.Subject = xxx'郵件主題
mailMessage.Body = xxx'郵件內容
Dim client As New System.Net.Mail.SmtpClient()
client.Credentials = New System.Net.NetworkCredential(xxx, xxx)'寄出郵件地址,密碼
client.Port = 25
client.Host = "smtp.mail.yahoo.com"
client.EnableSsl = True'是否使用ssl加密
client.Send(mailMessage)

短短幾行就完成了,但您若一定堅持使用winsock可參加這個api說明網址
http://pinvoke.net/default.aspx/ws2_32.WSASocket
贝隆 2012-02-21
  • 打赏
  • 举报
回复
发送电子邮件附件


与简单电子邮件发送程序相比,本示例程序多了框架控件及其中的内容。框架中的列表框用来显示附件的路径,另外两个按钮的作用相信你一看就知道。真正发送附件的代码在Send message按钮的Click事件中。在该事件中多了一小段代码:

For i = 0 To lstAttachments.ListCount - 1
lstAttachments.ListIndex = i
m_strEncodedFiles = m_strEncodedFiles & _
UUEncodeFile(lstAttachments.Text) & vbCrLf
Next i
上面的代码将附件的路径作为参数传递给UUEncodeFile函数。该函数的作用是按照我们前面所讲的算法对字符进行编码。编码后的数据被保存在一个模块级变量m_strEncodedFile中。然后该变量的内容被添加到邮件正文中:

'Add atacchments
strMessage = txtMessage & vbCrLf & vbCrLf & m_strEncodedFiles

剩下的事情就再清楚不过了。编码后的数据作为邮件的一部分发送出却,你不需编写特别的代码处理SMTP服务器。下面的函数UUEncodeFile的代码:

Public Function UUEncodeFile(strFilePath As String) As String

Dim intFile As Integer 'file handler
Dim intTempFile As Integer 'temp file
Dim lFileSize As Long 'size of the file
Dim strFileName As String 'name of the file
Dim strFileData As String 'file data chunk
Dim lEncodedLines As Long 'number of encoded lines
Dim strTempLine As String 'temporary string
Dim i As Long 'loop counter
Dim j As Integer 'loop counter

Dim strResult As String
'
'Get file name
strFileName = Mid$(strFilePath, InStrRev(strFilePath, "\") + 1)
'
'Insert first marker: "begin 664 ..."
strResult = "begin 664 " + strFileName + vbLf
'
'Get file size
lFileSize = FileLen(strFilePath)
lEncodedLines = lFileSize \ 45 + 1
'
'Prepare buffer to retrieve data from
'the file by 45 symbols chunks
strFileData = Space(45)
'
intFile = FreeFile
'
Open strFilePath For Binary As intFile
For i = 1 To lEncodedLines
'Read file data by 45-bytes cnunks
'
If i = lEncodedLines Then
'Last line of encoded data often is not
'equal to 45, therefore we need to change
'size of the buffer
strFileData = Space(lFileSize Mod 45)
End If
'Retrieve data chunk from file to the buffer
Get intFile, , strFileData
'Add first symbol to encoded string that informs
'about quantity of symbols in encoded string.
'More often "M" symbol is used.
strTempLine = Chr(Len(strFileData) + 32)
'
If i = lEncodedLines And (Len(strFileData) Mod 3) Then
'If the last line is processed and length of
'source data is not a number divisible by 3,
'add one or two blankspace symbols
strFileData = strFileData + Space(3 - (Len(strFileData) Mod 3))
End If

For j = 1 To Len(strFileData) Step 3
'Breake each 3 (8-bits) bytes to 4 (6-bits) bytes
'
'1 byte
strTempLine = strTempLine + Chr(Asc(Mid(strFileData, j, 1)) \ 4 + 32)
'2 byte
strTempLine = strTempLine + _
Chr((Asc(Mid(strFileData, j, 1)) Mod 4) * 16 + Asc(Mid(strFileData, j + 1, 1)) \ 16 + 32)
'3 byte
strTempLine = strTempLine + _
Chr((Asc(Mid(strFileData, j + 1, 1)) Mod 16) * 4 + Asc(Mid(strFileData, j + 2, 1)) \ 64 + 32)
'4 byte
strTempLine = strTempLine + _
Chr(Asc(Mid(strFileData, j + 2, 1)) Mod 64 + 32)
Next j
'add encoded line to result buffer
strResult = strResult + strTempLine + vbLf
'reset line buffer
strTempLine = ""
Next i
Close intFile
'add the end marker
strResult = strResult & "'" & vbLf + "end" + vbLf
'asign return value
UUEncodeFile = strResult

End Function

我不敢说上面的代码的运行速度是最快的,但却是我试验多次达到的最快速度。VB处理字符并不是它的特长,所以如果速度对你来讲至关重要的话,请尝试用C++或Delphi开发的库或组件。



X-i-n 2012-02-21
  • 打赏
  • 举报
回复
附件不管二进制还是文本,都要用base64加密,这是死规定。
所以这个程序里有 Base64EncodeFile (用来加密文件)和Base64Encode(用来加密字符串)两个函数
X-i-n 2012-02-21
  • 打赏
  • 举报
回复
两个=跟收发邮件无关,它是base64加密的时候起后缀作用,凑位数用的。
j88198188 2012-02-21
  • 打赏
  • 举报
回复
这个加密大致是什么意思呢?是因为附件是二进制文件,不是纯文本,要转码?
转码后其实等于是和正文一样(/包含在正文中)发过去?
X-i-n 2012-02-21
  • 打赏
  • 举报
回复
前面我说得不准确
在收邮件的时候,除了附件显示是BASE64密文外,登陆、服务器响应,都是明文的
发邮件的时候,登陆时的用户名密码需要用BASE64密文,附件需要BASE64密文,其它都是明文(包括邮件正文,邮箱地址,主题等)
"=="可能充当的是“数据尾”的功能,与回车换行一起表示数据发送完毕
我再研究一下
j88198188 2012-02-21
  • 打赏
  • 举报
回复
怀念CDO,直接add attachment就行,一句话的事情
j88198188 2012-02-21
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 runnerchin 的回复:]

不管是附件也好,邮件正文也罢,都是用BASE64加密的,你向服务器发送,以及接收到的服务器的响应内容,都是如此,包括你在登录的时候使用的用户名和密码,也需要使用BASE64加密。这种通信方法是规定好了的
[/Quote]
谢谢!但是它这一句: wsk.SendData strBody.Text & vbCrLf & vbCrLf,是发送正文,好像没有调用它里面那个Base64Encode( )函数专门加密.是直接发送的?
是不是附件才要自己加密呢?我看他这几句好像都是执行加密的代码: Base64EncodeFile txtAttach.Text, rtfAttach, txtOutput
wsk.SendData txtEncodedFile.Text & "==" & vbCrLf'这个是干嘛的?
加载更多回复(3)

1,502

社区成员

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

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