高分求教,大哥大姐救命啊,我目前通过rainstormmaster(暴风雨 v2.0)online(龙卷风V2.0--再战江湖) 给的代码实现了通过SMTP发邮件的功能,但是当我想发送带有附件的内容时出现了问题。我运行龙卷风的发附件的代码,出现的问题如下:
当我选择附件时候,可以实现发送,但是当我不选择附件时却出现了
执行阶段错误“55”
档案已经开启然后错误的代码定位在
Open App.Path & "\mail.tmp" For Input As #fnum
这行代码上其前后代码如下'發送消息體
Wsock.SendData "DATA" & vbCrLf
If Not WaitForResponse("354", 10) Then
    txtMsg.Text = txtMsg.Text & "無法發送消息體" & vbCrLf
    Exit Sub
  
End If
Dim fnum As Integer
fnum = FreeFile
Open App.Path & "\mail.tmp" For Input As #fnum
'Wsock.SendData mData & vbCrLf
While Not EOF(fnum)
    Line Input #fnum, strContent
    Wsock.SendData strContent & vbCrLf
Wend
Close #fnum
Wsock.SendData "." & vbCrLf
If Not WaitForResponse("250", 20) Then
    txtMsg.Text = txtMsg.Text & "消息體發送不成功" & vbCrLf
    Exit Sub
End If
'結束郵件發送對話
Wsock.SendData "QUIT" & vbCrLf
If Not WaitForResponse("221", 10) Then
    Exit Sub
End If
Wsock.Close
txtMsg.Text = txtMsg.Text & "郵件發送成功"
End Sub请问
1, 为什么我不选择附件会出问题,如何解决2,Open App.Path & "\mail.tmp" For Input As #fnum
中mail.tmp是什么文件,为什么要打开它,谢谢

解决方案 »

  1.   

    源代码如下:再线等待
    Option Explicit
    Public ServerIp As String 'SMTP服務器地址
    Public ServerPort As Long 'SMTP服務器端口Dim strSendName As String '發送人姓名
    Dim strReceiveName As String '接收人姓名
    Dim strFromMail As String '發送人地址
    Dim strToMail As String '諉彶華硊
    Dim m_Date As String '發送日
    Dim strSubject As String '主題
    Dim strContent As String '正文
    Dim Information As String '從服務器接收響應消息Private Sub cmdAtt_Click()
    Dim i As Integer
    For i = 0 To cobAtt.ListCount - 1
        frmAtt.LstAtt.AddItem cobAtt.List(i)
    Next i
    cobAtt.Clear
    frmAtt.Show vbModal
    End SubPrivate Sub cmdSend_Click()
    If cobAtt.ListCount > 0 Then
        GenMail True
    Else
        GenMail False
    End If
    '設置Winsock
    Wsock.Close
    Wsock.RemoteHost = ServerIp
    Wsock.RemotePort = ServerPort
    '連接SMTP服務器
    Wsock.Connect
    If Not WaitForResponse("220", 10) Then
        txtMsg.Text = "郵件服務器連接不上......"
        Exit Sub
    End If
    '打開對話
    Wsock.SendData "HELO" & " " & Wsock.LocalHostName & vbCrLf
    If Not WaitForResponse("250", 10) Then
        txtMsg.Text = txtMsg.Text & "無法打開郵件發送對話" & vbCrLf
        Exit Sub
    End If
    '發送發送方地址
    Wsock.SendData "MAIL FROM:" & " " & strFromMail & vbCrLf
    If Not WaitForResponse("250", 10) Then
        txtMsg.Text = txtMsg.Text & "無法發送發送方地址" & vbCrLf
        Exit Sub
    End If
    '發送接收方地址
    Wsock.SendData "RCPT TO:" & " " & strToMail & vbCrLf
    If Not WaitForResponse("250", 10) Then
        txtMsg.Text = txtMsg.Text & "無法發送接收方地址" & vbCrLf
        Exit Sub
    End If
    '發送消息體
    Wsock.SendData "DATA" & vbCrLf
    If Not WaitForResponse("354", 10) Then
        txtMsg.Text = txtMsg.Text & "無法發送消息體" & vbCrLf
        Exit Sub
      
    End If
    Dim fnum As Integer
    fnum = FreeFile
    Open App.Path & "\mail.tmp" For Input As #fnum
    'Wsock.SendData mData & vbCrLf
    While Not EOF(fnum)
        Line Input #fnum, strContent
        Wsock.SendData strContent & vbCrLf
    Wend
    Close #fnum
    Wsock.SendData "." & vbCrLf
    If Not WaitForResponse("250", 20) Then
        txtMsg.Text = txtMsg.Text & "消息體發送不成功" & vbCrLf
        Exit Sub
    End If
    '結束郵件發送對話
    Wsock.SendData "QUIT" & vbCrLf
    If Not WaitForResponse("221", 10) Then
        Exit Sub
    End If
    Wsock.Close
    txtMsg.Text = txtMsg.Text & "郵件發送成功"
    End Sub'該按扭事件過程用于設置smtp服務
    Private Sub cmdSetUp_Click()
    frmSetup.Show
    End Sub'程序加載時讀出上次的設置
    Private Sub Form_Load()
    ServerIp = GetSetting("email", "smtpserver", "serverip", "")
    ServerPort = GetSetting("email", "smtpserver", "serverport", 25)
    Wsock.Protocol = sckTCPProtocol
    End Sub'程序退出時保存設置
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    SaveSetting "email", "smtpserver", "serverip", ServerIp
    SaveSetting "email", "smtpserver", "serverport", ServerPort
    End Sub'接收服務器的響應消息
    Private Sub Wsock_DataArrival(ByVal bytesTotal As Long)
    Wsock.GetData Information
    txtMsg.Text = txtMsg.Text & Information & vbCrLf
    End Sub'該函數用于等待服務器響應碼
    Private Function WaitForResponse(strResponse As String, WaitTime As Integer) As Boolean
    Dim WaitSt As Date
    WaitSt = Now()
    While InStr(1, Information, strResponse, vbTextCompare) < 1
        DoEvents
        If DateDiff("s", WaitSt, Now) > WaitTime Then
           Information = ""
           WaitForResponse = False
           Exit Function
        End If
    Wend
    Information = ""
    WaitForResponse = True
    End Function'該函數用于构造信件內容
    Private Sub GenMail(bAttachment As Boolean)
    Dim fnum As Integer, FAttin As Integer
    Dim strLine As String
    strSendName = txtSName.Text
    strReceiveName = txtRName.Text
    strFromMail = txtFrom.Text
    strToMail = txtTo.Text
    m_Date = Format(Date, "Ddd") & ", " & Format(Date, "dd Mmm YYYY") & " " & Format(Time, "hh:mm:ss") & "" & " -0600"
    strSubject = txtSubject.Text
    strContent = txtContent.Text
    fnum = FreeFile()
    Open App.Path & "\mail.tmp" For Output As fnum
    '构造信件標題字段
    Print #fnum, "From:" & Chr(32) & strSendName
    Print #fnum, "Date:" & Chr(32) & m_Date
    Print #fnum, "X-Mailer: BigAnt Smtp Mailer V1.0"
    Print #fnum, "To:" & Chr(32) & strReceiveName
    Print #fnum, "Subject:" & Chr(32) & strSubject
    If bAttachment = False Then
        Print #fnum, ""
        Print #fnum, strContent
        Exit Sub
    End If
    Print #fnum, "MIME-Version: 1.0"
    Print #fnum, "Content-type:multipart/mixed;"
    Print #fnum, " boundary =""----=_NextPart_000_000A_01BF9F1A"""
    Print #fnum, ""
    '書寫信件的正文內容
    Print #fnum, "--" & "----=_NextPart_000_000A_01BF9F1A"
    Print #fnum, "Content-Type: text/plain;"
    Print #fnum, "    Charset = ""gb2312"""
    Print #fnum, "Content-Transfer-Encoding: 8bit"
    Print #fnum, ""
    Print #fnum, strContent
    '附件內&#63527;
    Dim i As Integer
    For i = 0 To cobAtt.ListCount - 1
        Base64Encode cobAtt.List(i), App.Path & "\attachment" & i & ".tmp"
        Print #fnum, "--" & "----=_NextPart_000_000A_01BF9F1A"
        Print #fnum, "Content-Type: Application/octet-stream"
        Print #fnum, "  name=" & cobAtt.List(i)
        Print #fnum, "Content-Transfer-Encoding: base64"
        Print #fnum, "Content-Disposition: attachment;"
        Print #fnum, "  FileName=" & cobAtt.List(i)
        Print #fnum, ""
        FAttin = FreeFile
        Open App.Path & "\attachment" & i & ".tmp" For Input As #FAttin
        While Not EOF(FAttin)
            Line Input #FAttin, strLine
            Print #fnum, strLine
        Wend
        Close FAttin
    Next i
    Print #fnum, "--" & "----=_NextPart_000_000A_01BF9F1A" & "--"
    Close fnum
    End Sub
      

  2.   

    那个代码效率太低,它是先编码再发送,但编码的速度实在是....我打开个1m的文件,程序死机。加了doevents更惨,编码一个100K的文件都要等1分种我再用outlook发,发现outlook是一边发送一边编码的,速度很快求极速的base64编码/解码的算法模块
      

  3.   

    我们公司走自己的SMTP服务器端口,Pop3被封了
      

  4.   

    另外請問
    fnum = FreeFile()                  
      
    Open App.Path & "\mail.tmp" For Output As fnum  和
    Dim fnum As Integer
    fnum = FreeFile
    Open App.Path & "\mail.tmp" For Input As #fnum这两锻分别起到什么作用,谢谢
      

  5.   

    我把mail.tmp文件改为mail.txt 发现里面的内容就是Mail的内容
    包括附件的编码,请问为什么要把内容写入mail.tmp内,tmp是什么文件
      

  6.   

    当我选择附件时候,可以实现发送,但是当我不选择附件时却出现了
    执行阶段错误“55”
    档案已经开启这一段是发送附件的代码,如果你没有选择附件,跟踪看一下这封信属于rfc822信件格式
    '该函数用于构造信件内容Private Sub GenMail(bAttachment As Boolean)将tmp文件base64加密发送
    tmp是临时文件
      

  7.   

    请问代码里面:
    'Print #fnum, "Content-type:multipart/mixed;"
    'Print #fnum, " boundary =""----=_NextPart_000_000A_01BF9F1A"""
    表示什么信息啊
      

  8.   

    請問
    Print #fnum, "--" & "----=_NextPart_000_000A_01BF9F1A" & "--"
    表示什么意思
      

  9.   

    今天下午有再看這段代碼,這代碼寫的有問題,因為習慣用mapi,一直沒有時間看smtp。
    其實在vb中間winsock傳送smtp的mail比較慢,還有一種控件叫做dssock傳輸比較好。今天下午整理了關於這個問題的一些資料,但是下班的時候還沒有寫完,包括找到以前自己做的這個使用smtp發附件的程序,但是因為丟失dssock沒有辦法運行。我現在要上課去了。我會把相關資料mail 到樓主信箱。
    可能今天晚上可能明天晚上,因為我也知道課上完的時候能否上網。另:樓主必須自己去查msdn,昨天沒有看,今天白天看你問關於open,freefile的問題才發現你不懂得這簡單的問題,而且winsock.close的運用根本順序就不對。
      

  10.   

    请问代码里面:
    'Print #fnum, "Content-type:multipart/mixed;"
    'Print #fnum, " boundary =""----=_NextPart_000_000A_01BF9F1A"""
    表示什么信息啊
    mime扩展了信件规范,对rfc822提供的字段进行了扩充,补充了一些信头字段去google找这方面的资料
      

  11.   

    在GenMail方法中:
    If bAttachment = False Then
        Print #fnum, ""
        Print #fnum, strContent
        Close fnum        '楼主出错的原因在此,文件开了未关闭
        Exit Sub
    End If
      

  12.   

    楼主的这个邮件例子,我也看过。有2点不太好的地方。
    1.在WaitForResponse方法中每次与 服务器交互时,只有10秒种的延迟时间。实际应用中,是远远不够的。特别是在发大附件时,延迟时间可能很长。
    2.在附件处理方面不是很好。在bAttachment方法中,把每个附件都读出到临时文件中,之后再从临时文件中读出并写入到同一文件mail.tmp中。
    这样做且不说消耗内存及CPU资源问题,单说如果文件多几个,尺寸大些,分分钟就死机了。呵呵.
    我个人觉得应该不采用临时文件的做法。直接边读一行文件内容,就及时发送。这样资源就不会占太大。不过做为发邮件入门例子,已经足够了。希望楼主早日写出更好的例子与大家分享。
      

  13.   

    所以不建議發附件用 winsock還得明天,今天開一整天的會了
      

  14.   

    请问daisy8675(莫依) 怎么发邮件好些呢?
    winsock只是提供了网络通信功能,怎么写出个强壮能干的软件还得看个人怎么利用。