【问题标题】:asp.net vb form attachment issueasp.net vb表单附件问题
【发布时间】:2012-03-30 20:08:00
【问题描述】:

我制作了一个表格,允许客户下载多个 (4) 附件。 问题是当表格发送到我的电子邮件时,我收到了 4 个邮件附件 即使客户端只上传了 1 个附件。如果他上传了一个附件 我把其余的作为“无名”文件。 我该如何解决?

这是我的后台代码(VB):

Imports System.Net.Mail

Partial Class SendAttachment

    Inherits System.Web.UI.Page


    Protected Sub DL1_SelectedIndexChanged(sender As Object, e As EventArgs)
    If DropDownList1.SelectedItem.Text = "אחר" Then
        q1.Visible = True  
        Else
                q1.Visible= False  
    End If
If DropDownList1.SelectedItem.Text = "אחר" Then
       q22.Visible = True
        Else
          q22.Visible= False 
    End If
End Sub

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        'Make sure a file has been uploaded



     If String.IsNullOrEmpty(AttachmentFile.FileName) OrElse AttachmentFile.PostedFile Is Nothing Then
            Throw New ApplicationException("לא העלאת קבצים.")
        End If

if AttachmentFile.PostedFile.ContentLength >600000 then

'display error file size should be less than 100kb

end if







        '!!! UPDATE THIS VALUE TO YOUR EMAIL ADDRESS
        Const ToAddress As String = "pelleg@shakuff.co.il"
        dim textMsg as String
   textMsg ="שאלות סינון" &
 (vbCrLf)  & (vbCrLf)  & (vbCrLf)  & question1.text & (vbCrLf)  & q1.text & 
(vbCrLf) & (vbCrLf) & question2.text & (vbCrLf) & q2.text & 
(vbCrLf) & (vbCrLf) & question3.text & (vbCrLf) &q3.text & 
(vbCrLf) & (vbCrLf) & "ממליצים" & (vbCrLf) & c1.text & (Space(5)) & c2.text & (Space(5)) & c3.text & (vbCrLf) &
 (vbCrLf) & question4.text & (vbCrLf) & q5.text & (vbCrLf) &
 (vbCrLf) & question5.text & (vbCrLf) & q6.text & (vbCrLf) &
"------------------------------------------------------------------------------------------------" &
 (vbCrLf) & "פרטים אישיים" & (vbCrLf) & 
(vbCrLf) & "שם מלא" & (vbCrLf) & pi1.text & (vbCrLf) &
 (vbCrLf) & "כתובת מייל" & (vbCrLf) & UsersEmail.Text & (vbCrLf) &
 (vbCrLf) & "טלפון" & (vbCrLf) & pi2.text & (vbCrLf) & 
(vbCrLf) &"שעות נוחות" & (vbCrLf) & pi3.text & (vbCrLf) & 
(vbCrLf) & (vbCrLf) & "הערות"  & 
(vbCrLf)  & body.text & (vbCrLf) & 
(vbCrLf) & "מעוניין בהצעות נוספות" & (vbCrLf) & q4.text 
 '



        '(1) Create the MailMessage instance
        Dim mm As New MailMessage(UsersEmail.Text, ToAddress)

        '(2) Assign the MailMessage's properties
        mm.Subject = Subject.text &  "-" & pi1.text
        mm.Body = textMsg

        mm.IsBodyHtml = false

        'Attach the file
        AttachmentFile.PostedFile.InputStream.Position = 0  

        mm.Attachments.Add(New Attachment(AttachmentFile.PostedFile.InputStream, AttachmentFile.FileName))
        mm.Attachments.Add(New Attachment(AttachmentFile2.PostedFile.InputStream, AttachmentFile2.FileName))



        '(3) Create the SmtpClient object
        Dim smtp As New SmtpClient

        '(4) Send the MailMessage (will use the Web.config settings)
        smtp.Send(mm)

        'Show the EmailSentForm Panel and hide the EmailForm Panel
        EmailSentForm.Visible = True
        EmailForm.Visible = False
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            'On the first page load, hide the EmailSentForm Panel
            EmailSentForm.Visible = False

        End If
    End Sub
End Class

提前致谢!

【问题讨论】:

    标签: asp.net vb.net email attachment


    【解决方案1】:

    这是因为您正在添加附件,即使它不是使用 mm.Attachments.Add 上传的。

    您应该在附加到电子邮件之前检查每个上传的文件

    可以按如下方式完成 如果(附件文件2.Hasfile) mm.Attachments.Add(新附件 (AttachmentFile2.PostedFile.InputStream, AttachmentFile2.FileName))

    【讨论】:

    • 你能指导我怎么做吗?
    猜你喜欢
    • 1970-01-01
    • 2016-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-27
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    相关资源
    最近更新 更多