Click here to Skip to main content
13,505,493 members
Rate this:
Please Sign up or sign in to vote.
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

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        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)"))



        End If
    End Sub
Posted 27-Apr-13 21:30pm
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?
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
GeekBond 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.180417.1 | Last Updated 28 Apr 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100