求助:怎样用Winsock(SMTP)发送.txt格式以外的附件?

lye 2003-08-22 05:07:33
我自己写了发,收邮件程序(带附件),可是我的收邮件的代码只能打开.txt格式的附件。
请各位帮帮忙,我是才接触SMTP的。。。。
...全文
120 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lye 2003-08-29
  • 打赏
  • 举报
回复
这本书我看过(发收邮件那部分),不过里面讲的不彻底,点到为止。
dchengwang 2003-08-27
  • 打赏
  • 举报
回复
visual basic 网络通信协议分析与应用实现
人民邮电出版社,这本书上有例子
lye 2003-08-23
  • 打赏
  • 举报
回复
To : "since1990(level)" and "lihonggen0(李洪根,用.NET,标准答案来了) "
难道用SMTP 发送附件时一定要编码吗(UUEncode或Base64Encode)?
小弟发送文本格式的附件时就没用编码,也能打开附件。但是如果用两位说的函数进行编码,打开附件(在网站上)时就是乱码了?????
since1990 2003-08-22
  • 打赏
  • 举报
回复
发送电子邮件附件

与简单电子邮件发送程序相比,本示例程序多了框架控件及其中的内容。框架中的列表框用来显示附件的路径,另外两个按钮的作用相信你一看就知道。真正发送附件的代码在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开发的库或组件。
lihonggen0 2003-08-22
  • 打赏
  • 举报
回复
参考:

http://www.jsj92.com/cxg/VB/VB/net/net8.html
在PB中使用WINSOCK.OCX做双向通信的简单例子----PowerBuilder 一、在窗口中添加WINSOCK控件:   在应用中新开一个窗口,在窗口画板中点击controls-->OLE菜单项,弹出 Insert object窗口,单击Insert control标签,从列表框中双击选定 Microsoft Winsock control,将winsock的图标贴在窗口上。   在程序中该控件名称定为winsock_a(甲方)和winsock_b(乙方)。   二、设置信息输入输出文本框:   在窗口中增加一个按钮cb_1,两个单行文本框sle_1,sle_2,分别用于输入 要发送的字符串和接受对方发送的字符串。   三、设置通讯协议:   WINSOCK控件允许用户以UDP和TCP两种协议中任选一种进行通讯。   1.UDP协议设置:UDP协议是一种无连接的通讯协议,在通讯之前,需要绑 定remotehost和remoteport属性,如果需要双向通讯,还要设置localport属性 。   在甲方(本机地址为:134.1.1.1)窗口的Open事件中加入如下语句: winsock_a.object.protocol=1 //winsock通讯协议设为UDP协议 winsock_a.object.remotehost="134.1.1.2" //对方的ip地址 winsock_a.object.remoteport=6000 //对方的winsock通讯端口号 winsock_a.object.localport=6001 //本机的winsock通讯端口号 winsock_a.object.bind //绑定通讯协议   在乙方(本机地址为:134.1.1.2)窗口的Open事件中加入如下语句: winsock_b.object.protocol=1 //winsock通讯协议设为UDP协议 winsock_b.object.remotehost="134.1.1.1" //对方的ip地址 winsock_b.object.remoteport=6001 //对方的winsock通讯端口号 winsock_b.object.localport=6000 //本机的winsock通讯端口号 winsock_b.object.bin //绑定通讯协议   2.TCP协议设置:TCP协议在通讯前需要进行连接。   在甲方(作为服务器端)窗口的Open事件中加入如下语句: winsock_a.object.protocol=0 //winsock通讯协议设为TCP协议 winsock_a.object.localport=6001 //本机的winsock通讯端口号 winsock_a.listen() //启动监听   在甲方winsock_a控件的Connectionrequest事件中加入如下语句: //接受到对方的连接请求后 if winsock_a.object.state0 then winsock_a.close() end if winsock_a.accept(requestID) //建立直接连接 //requestID是Connectionrequest事件自己的参数   在乙方(作为客户端)窗口的Open事件中加入如下语句: winsock_b.object.protocol=0 //winsock通讯协议设为TCP协议 winsock_b.object.remotehost="134.1.1.2" //对方的ip地址 winsock_b.object.remoteport=6000 //对方的winsock通讯端口号 winsock_b.connect() //发出连接请求   3.无论采用哪种协议,都要在窗口的Close事件中加入如下语句: if winsock_a/*或winsock_b*/.object.state0 then winsock_a.close() end if   否则可能第二次使用时发生异常问题   四、开始通讯   在按钮cb_1(caption属性设为‘发送’)的click事件中加入如下语句: winsock_a/*或winsock_b*/.object.send (sle_1.text)   在winsock_a/*或winsock_b*/控件的dataarrival事件中加入如下语句: //接受到对方数据后 string datastr1 winsock_a/*或winsock_b*/.object.getdata (def datastr1) sle_2.text=datastr1 //将数据字符串显示在文本框中   以上程序实际上体现了聊天器的底层工作原理,稍加修改扩充就可以做成

7,763

社区成员

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

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