Click here to Skip to main content
15,886,652 members
Please Sign up or sign in to vote.
2.60/5 (2 votes)
See more:
i want to limit the total size of attachments upto 2mb in email.
for e.g : 1st attachment is of 1.5mb and
2nd attachment is of 400 kb

then it should not exceed total i.e , more than 2mb

----------------------------------------------------------------------------
VB
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        OpenFileDialog1.ShowDialog()
        OpenFileDialog1.CheckFileExists = True
        OpenFileDialog1.Filter = "All Files (*.*)|*.*"
        Dim size As System.IO.FileInfo
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            For Each File As String In OpenFileDialog1.FileNames
                Dim attch As New Net.Mail.Attachment(File)
                size = My.Computer.FileSystem.GetFileInfo(File)
                ListBox1.Items.Add(Path.GetFileName(File & "(" & Convert.ToInt32(size.Length()) & "bytes)"))

                mail.Attachments.Add(attch)

            Next

        End If
    End Sub
Posted
Comments
Ian A Davidson 28-Apr-13 4:37am    
Looking at this code, don't you even have an idea how to do it?
I know it's not as simple as it looks if you are trying to limit the final size of the e-mail (because attachments will be encoded - e.g. base-64 encoded, which will increase their size significantly) but even so, you must have tried keeping track of the total size of the raw files and e.g. not adding the attachment/s if it/they would exceed the size limit, displaying a message to the user instead? Are you after some code to try to calculate the exact final message size?
Regards,
Ian.
AP900 28-Apr-13 4:48am    
i tried a lot of stuff. i displayed message that "attachment size exceeded" but it was for only one file each. now if i attach a file of 1.5 MB it will not display the message bcoz it it just 1.5 MB of 2 MB, but when I again attach another file of 1.5 MB again the total will be 3.0 MB
i.e, the total size will exceed 2 MB. So,I want code that can limit the attachment size upto 2 MB
i.e, how many the files may be, the total should not exceed more than 2 MB
Akinmade Bond 28-Apr-13 5:18am    
What you do is that, everytime the user adds a new file, you check the total size of files he has already uploaded and the size of the new file just uploaoded. If the total exceeds 2mb, you discard the new file and show a message. Isn't that what you want?
AP900 28-Apr-13 5:22am    
i will try it but can u modify the above code ?
Mike Meinz 28-Apr-13 6:56am    
If you're using Microsoft Exchange as your mail server, there is a feature in Exchange to limit the size of message. See How to set size limits for messages in Exchange Server

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900